Question

ArrayQueue

Implement the ArrayQueue class

In the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.

ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.

Many classes are given to you

Download and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in BlueJ and see Tester::main().  This shows the methods you have to implement.  You must write your code in the ArrayQueue class, that implements the circular array.  In ArrayQueue, you must use only the instance variables given to you, you are not allowed to add more.  Other than adding your new methods to ArrayQueue, and adding a new QueueOverflowException class, the code provided must not be altered in any way.

Hints

Note that the array implementation does not require our Item class.  Instead the array contains elements of the generic data type T.

The syntax in the ArrayQueue constructor to create an array of the generic data type T is awkward, so has been given to you.  See how the code creates an array of Object, which is then typecast to data type T.  Cool!  Also, see here how I’ve changed the usual initializations of front and rear, because I want to force wrapping to happen early.

Queue overflow was not a problem for our previous linked list implementation, but is obviously a problem for an array implementation.  So you will have to add a new QueueOverflowException class.

Run the program to test your completed ArrayQueue class.  When correct, save the output as an output.txt text file, saved in the BlueJ project folder.

Required

Your program must be correct, simple and clear:

  • clear, consistent layout and indentation

  • you must Javadoc comment every class, method and anything that is not simple and clear. (‘How to write Javadoc comments’ has been added to Course information)

  • clear, meaningful variable and method names


Given Code

public class ArrayQueue<T> implements QueueInterface<T>

{

    public static final int MAX = 10;


    //use only these 3 instance variables!

    //you are NOT ALLOWED to add more instance variables

    private T elements[];

    private int front;

    private int rear;


    public ArrayQueue()

    {

        //awkward syntax!

        elements = (T[]) new Object[MAX];

        /* the usual initialisations

        front = MAX - 1;

        rear = MAX - 1;

        */

        //tweaked here to deliberately force early wrapping!

        front = MAX - 3;

        rear = MAX - 3;

    }

    

    //implement your methods with Javadoc comments here

    //again, you are NOT ALLOWED to add more instance variables

    

}


public class Item<T>

{

    protected T info;

    protected Item<T> next;


    public Item()

    {

        info = null;

        next = null;

    }


    public Item(T info)

    {

        this.info = info;

        next = null;

    }

}


public interface QueueInterface<T>

{

    void insert(T element);


    T remove() throws QueueUnderflowException;


    Boolean isEmpty();

}


public class QueueOverflowException extends RuntimeException

{

    public QueueOverflowException(){

        super();

    }

    

    public QueueOverflowException(String message){

        super(message);

    }

}


public class QueueUnderflowException extends RuntimeException

{

    public QueueUnderflowException()

    {

        super();    

    }


    public QueueUnderflowException(String message)

    {

        super(message);    

    }

}


public class Tester

{

    public static void main()

    {

        QueueInterface<Character> q = new ArrayQueue<Character>();


        //insert into the queue, with wrapping

        for (Character ch = 'a'; ch <= 'h'; ++ch)

            q.insert(ch);


        //remove the first 5 items, with wrapping

        for (int i = 1; i <= 5; ++i)

            System.out.print(q.remove());

        System.out.println();


        //now fill the queue

        for (Character ch = 'A'; ch <= 'F'; ++ch)

            q.insert(ch);


        //test the new QueueOverflow exception

        try {

            q.insert('!');

        }

        catch (QueueOverflowException overflow) {

        System.out.println("Exception: " + overflow.getMessage());

        System.err.println("Exception: " + overflow.getMessage());

        //System.exit(2);

        }


        //empty the queue

        while (!q.isEmpty())

            System.out.print(q.remove());

        System.out.println();


        System.out.println("done");

    }

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
ArrayQueue
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • List the unique classes/ADTs in the following code segment: (hint: there are 6) public class LinkedQueueclass...

    List the unique classes/ADTs in the following code segment: (hint: there are 6) public class LinkedQueueclass protected class QueueNode int info; QueueNode link; private QueueNode queue Front; private QueueNode queue Rear; public class QueueOverflowException extends Queue Exception public QueueOverflowException() super("Queue Overflow"); public QueueOverflowException (String msg) super (msg); System.out.println("An exception occurred");

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

  • use intellij idea main java wp the professor. Please make sure to only implement what is...

    use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...

  • this is a data structures course 21. (20 points) We want to define a generic class...

    this is a data structures course 21. (20 points) We want to define a generic class ArrayQueue, which uses a circular array. public class Array Queue< II (1) List all the instance variables needed for the class 11 (2) define a constructor with a parameter specifying the initial capacity // (3) define the remove method

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT