Question

In: Computer Science

Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in...

Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList.

Deliverables:

  • EmptyQueueException.java (given in Lab_7_StartUp folder)
  • QueueInterface.java (given in Lab_7_StartUp folder)
  • ArrayListQueue.java (need to create)
  • Lab7.java (need to create)

QueInterface:

@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
  
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the operation. */
public T dequeue();
  
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
  
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
  
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface

EmptyQueueException:

/**
A class of runtime exceptions thrown by methods to
indicate that a queue is empty.
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public class EmptyQueueException extends RuntimeException
{
public EmptyQueueException()
{
this(null);
} // end default constructor

public EmptyQueueException(String message)
{
super(message);
} // end constructor
} // end EmptyQueueException

Solutions

Expert Solution

//QueInterface.java

/**
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
  
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the operation. */
public T dequeue();
  
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
  
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
  
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface


//EmptyQueueException.java

/**
A class of runtime exceptions thrown by methods to
indicate that a queue is empty.
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/

public class EmptyQueueException extends RuntimeException
{
public EmptyQueueException()
{
this(null);
} // end default constructor

public EmptyQueueException(String message)
{
super(message);
} // end constructor
} // end EmptyQueueException

//ArrayListQueue.java

import java.util.ArrayList;


public class ArrayListQueue<T> implements QueueInterface<T>
{
   ArrayList<T> arr;
//constructor
public ArrayListQueue()
   {
       arr = new ArrayList<>();
   }
  
   /** Adds a new entry to the back of this queue.
   @param newEntry An object to be added. */
   public void enqueue(T newEntry)
   {
       arr.add(newEntry);
   }
  
   /** Removes and returns the entry at the front of this queue.
   @return The object at the front of the queue.
   @throws EmptyQueueException if the queue is empty before the operation. */
   public T dequeue()
   {
       if(isEmpty())
           throw new EmptyQueueException();
       T entry = arr.get(0);
       arr.remove(0);
       return entry;
   }
  
   /** Retrieves the entry at the front of this queue.
   @return The object at the front of the queue.
   @throws EmptyQueueException if the queue is empty. */
   public T getFront()
   {
       if(isEmpty())
           throw new EmptyQueueException();
       return arr.get(0);
   }
  
   /** Detects whether this queue is empty.
   @return True if the queue is empty, or false otherwise. */
   public boolean isEmpty()
   {
       return arr.size()==0;
   }
  
   /** Removes all entries from this queue. */
   public void clear()
   {
       arr.clear();
   }
}

//Lab7.java

public class Lab7
{
   //main method
   public static void main (String[] args) throws EmptyQueueException
   {
       //create object of ArrayListQueue of integers
       ArrayListQueue<Integer> aq = new ArrayListQueue<>();
       //enqueue 5 items
       for(int i=1; i<=5; i++)
       {
           int x = i*i;
           aq.enqueue(x);
           System.out.println ("Enqueue: " + x);
       }
       //check if queue is empty
       System.out.println ("Is Queue empty? " + aq.isEmpty());
       ///dequeue 3 items
       for(int i=1; i<=3; i++)
       {
           System.out.println ("Dequeue: " + aq.dequeue());
       }
       //check if queue is empty
       System.out.println ("Is Queue empty? " + aq.isEmpty());
       //clear the queue
       System.out.println ("Clear the queue.");
       aq.clear();
       //check if queue is empty
       System.out.println ("Is Queue empty? " + aq.isEmpty());
   }
}

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
Declare and define a class for a fraction number. A fraction in mathematics is defined as...
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator. Requirements Task1    Define a fraction class that has the following member functions: constructor that initializes the fraction by default arguments. set function that sets the numerator of the fraction. set function that sets the denominator of the fraction. get function that returns the numerator of the fraction. get function that returns...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT...
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT BagInterface by using a doubly linked chain, as shown in Figure 4-10 of your textbook. You will also need to define the class Node described in Excercise 10 of Chapter 4. Your solution to this problem requires the creation/development of four files: Node.h, Node.cpp, DoublyLinkedBag.h and DoublyLinkedBag.cpp. This repository already contains BagInterface.h that contains the declaration of the BagInterface needed by this problem. Convert...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements Comparable<VolleyPlayer> { // instance variables - private String name; private int height; private boolean female; public VolleyPlayer(String name, int height, boolean female) { this.name = name; this.height = height; this.female = female; } public String toString() {    return (female ?"Female":"Male")+": "+name+" ("+height+" cm)"; } public boolean isFemale() {    return female; } public boolean isMale() {    return !female; } public int getHeight()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT