Question

Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT:

3) Queue ADT that uses an array internally (call it AQueue)

Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics.

The Queue ADT The Queue ADT stores arbitrary objects Auxiliary queue operations: object front(): returns the element at the fQueue Interface in Java public interface Queue{ Java interface public int size(); corresponding to our Queue ADT public boole

The Queue ADT The Queue ADT stores arbitrary objects Auxiliary queue operations: object front(): returns the element at the front without Insertions and deletions follow removing it the first-in first-out (FIFO) integer size(): returns the number of elements stored scheme Insertions are at the rear of the boolean isEmpty(): indicates whether no elements are stored queue and removals are at the front of the queue Exceptions Main queue operations: Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front Attempting to enqueue an element on a queue that is full can be signaled with a FullQueueException of the queue
Queue Interface in Java public interface Queue{ Java interface public int size(); corresponding to our Queue ADT public boolean isEmpty(); public Object front() throws EmptyQueueException; Requires the definition of class public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; EmptyQueueException NB: The java.util.Queue interface uses quite different signatures, and does not insist that the queue be FIFO.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Queue.java
//Queue interface
public interface Queue
{
   public int size();
   public boolean isEmpty();
   public Object front() throws EmptyQueueException;
   public void enqueue(Object o) throws FullQueueException;
   public Object dequeue() throws EmptyQueueException;
}

//EmptyQueueException.java
class EmptyQueueException extends Exception
{
   public EmptyQueueException(String s)
   {
       super(s);
   }
}

//FullQueueException.java
class FullQueueException extends Exception
{
   public FullQueueException(String s)
   {
       super(s);
   }
}

//AQueue.java
//Implementation of the Queue class
public class AQueue implements Queue
{
   //array of objects
   private Object[] queue;
   //front, rear, capacity and size of the queue
   private int front;
   private int rear;
   private int capacity;
   private int size;
   //constructor with capacity of queue
   AQueue(int length)
   {
       //initialize all data fields
       queue = new Object[length];
       capacity = length;
       front = 0;
       size = 0;
       rear = capacity - 1;
   }
   //function to get size of queue
   public int size()
   {
       return size;
   }
   //function to check queue is empty or not
   public boolean isEmpty()
   {
       return size() == 0;
   }
   //function that returns front value in the queue
   public Object front() throws EmptyQueueException
   {
       if(isEmpty())
       {
           throw new EmptyQueueException("Queue is Empty");
       }
       else
       {
           return queue[front];
       }
   }
   //function to insert in queue
   public void enqueue(Object o) throws FullQueueException
   {
       //if capcity and size are equal then queue is full
       if(capacity == size)
       {
           throw new FullQueueException("Queue is Full");
       }
       else
       {
           //else increment rear value and insert in the queue
           rear = (rear + 1) % capacity;
           queue[rear] = o;
           size ++;
       }
   }
   //function to dequeue from queue
   public Object dequeue() throws EmptyQueueException
   {
       //if queue is empty throw empty queue exception
       if(isEmpty())
       {
           throw new EmptyQueueException("Queue is Empty");
       }
       else
       {
           //or else store the front and incremenet the front index
           Object o = queue[front];
           front = (front + 1) % capacity;
           size --;
           return o;
       }
   }
   //function to print queue
   public void printQueue()
   {
       if(isEmpty())
       {
           System.out.println("Queue is Empty");
       }
       else
       {
           for(int i = front;i<rear+1;i++)
           {
               System.out.print(queue[i]+" ");
           }
           System.out.println();
       }
      
      
   }
}

//Tester.java
class Tester
{
   public static void main(String args[])
   {
       AQueue myQueue = new AQueue(5);
       System.out.println("Before inserting values into Queue:");
       myQueue.printQueue();
       System.out.println("\nEnqueue 3 values into Queue");
       for(int i = 0;i< 13;i++)
       {
           try
           {
               myQueue.enqueue(i);
           }
           catch(FullQueueException e)
           {
               System.out.println("Exception Caught:"+e.getMessage());
           }
       }
       System.out.println("\nAffter Insertion:");
       myQueue.printQueue();
       Object ob;
       System.out.println("\nDequeue 5 values from Queue.It should caught exception");
       for(int i = 0;i<5;i++)
       {
           try
           {
               ob = myQueue.dequeue();
               System.out.println("Value Dequeued from Queue: "+ob);
           }
           catch(EmptyQueueException e)
           {
               System.out.println("Exception Caught:"+e.getMessage());
           }
          
       }
       System.out.println("\nEnqueue 12");
       try
       {
           myQueue.enqueue(12);
       }
       catch(FullQueueException e)
       {
           System.out.println("Exception Caught:"+e.getMessage());
       }
       try
       {
           System.out.println("\nFront in Queue: "+myQueue.front());
       }
       catch(EmptyQueueException e)
       {
           System.out.println("Exception Caught:"+e.getMessage());
       }
   }
}
//sample outputC\Windows\SYSTEM32\cmd.exe c:. Before inserting values into Queue Queue is Empty A Enqueue 3 values into Queue Affter Inserti

Add a comment
Know the answer?
Add Answer to:
Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

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

    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 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: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

  • 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...

  • 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...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • 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...

  • 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...

  • what is wrong with my code. what should I do with this kind of error 61...

    what is wrong with my code. what should I do with this kind of error 61 QueueDemo java - Files - RaProjects/one- A Queue Demoava Queueinterface Java inport java.util.LinkedList; import java.util.Queue public class QueueDemo public static void main(String[] args) { 10 11 12 13 Queue String myQueue new LinkedlistString();//Create a reference to a queue Interface myqueue.add("A");//Call the enqueue method on myQueue passing the string value of "A" myQueue.add("B");//call the enqueue method on nyQueue passing the String value of "3" myQueue.add("C");//Call...

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