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

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...
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,...
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()...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a web site. The application should have labeled text fields for the Full Name, User Name, Password, Student Id, and a TextAreafor About Me. Include a button labeled Send. When the Send button is clicked, your program should print the contents of all fields (with labels) to standard output using println()statements.
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT