Question
With the help of our QueueList class, write a simple Java program to simulate priority queues to contain different job codes to be performed by
With the help of our QueueList class, write a simple Java program to simulate priority queues to contain different job codes to be performed by the Operating System on a fist-come first-served basis under three diffrent priorities. Steps to follow to accomplish the task:
A) Modify the QueueList class:
- Add a new instant variable (int count) for keeping track of the current count of nodes in QueueList
- Initialize count to 0 in the constructor; increment and decrement count by 1, for each enqueue() and dequeue() method call, inside these methods respectively
- Modify the default constructor to be a constructor with one argument to accept a name from the calling program as a pareameter instead of using the default name "queue"
- Modify the print() method to include the count number of job code(s) found in the queue.
B) Use the PriorityQueues.java file to finish the following steps:
- create three priority queue objects, namely Q1, Q2, and Q3 using QueueList class with queue names as "LowPriorityQ", "NormalPriorityQ" respectively
- Use a loop to generate 30 random integers ranging from 1 to 200. Each of these random itegeers represents a job code to be performed by the Operating System. If the remainder of the expression for each random number (i.e (randnumber % 3)) is equal to 0, 1, or 2, then place the random number into Q1, Q2 and Q3 respectively.
- Show the status of each priority queue using its print() mtehod.
A sample run of the program when finished would look like this, of course numbers could be different:
9 job code(s) found.
Low priorityQ 195 21 192 168 108 147 165
10 job code(s) found.
NormalPriorityQ: 103 160 58 196 13 115 169 187 91 82
11 job code(s) found.
HighPriorityQ: 95 86 107 125 29 32 92 119 68 44 182
Use the following programs to complete the task:
QueueList.java:
// QueueList.java
//
// Class QueueList definition with composed List object.
package MyQueue;
public class QueueList
{
private List a_queue;
public QueueList()
{
a_queue = new List("queue");
}
public void enqueue( Object object )
{
a_queue.insertAtBack( object );
}
public Object dequeue() throws EmptyListException
{
return a_queue.removeFromFront();
}
public boolean empty()
{
return a_queue.isEmpty();
}
public Object peek() throws EmptyListException
{
if (a_queue.isEmpty())
return null;
else
return a_queue.getFirstObject();
}
public void print()
{
a_queue.print();
}
}
PriorityQueues.java:
/**
* @(#)PriorityQueues.java
*
*
*/
import MyQueue.QueueList;
import java.util.*;
public class PriorityQueues {
public static void main(String[] args) {
int randnumber; // store a random integer value
// create three priority queue objects, namely Q1, Q2 and Q3 using QueueList class
// with queue names as "LowPriorityQ", "NormalPriorityQ" and "HighPriorityQ" respectively
// Use a loop to generate 30 random integers ranging from 1 to 200. Each of these random integers
// represents a job code to be performed by the Operating System. If the remainder
// of the expression of each randon number(i.e. (number % 3)is equal to 0,1, or 2, then
// place the random number into Q1, Q2 and Q3 respectively.
//show the status of each priority queue using its print() method.
}
}
List.java:
// List.java
// class List definition
package MyQueue;
public class List
{
private ListNode firstNode;
private ListNode lastNode;
private String name; // string like "list" used in printing
// constructor creates empty List with "list" as the name
public List()
{
this( "list" );
} // end List no-argument constructor
// constructor creates an empty List with a name
public List( String listName )
{
name = listName;
firstNode = lastNode = null;
} // end List one-argument constructor
public Object getFirstObject(){
return firstNode.getData();
}
public Object getLastObject() {
return lastNode.getData();
}
// insert Object at front of List
public void insertAtFront( Object insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode( insertItem );
else // firstNode refers to new node
firstNode = new ListNode( insertItem, firstNode );
} // end method insertAtFront
// insert Object at end of List
public void insertAtBack( Object insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same Object
firstNode = lastNode = new ListNode( insertItem );
else // lastNode's nextNode refers to new node
lastNode = lastNode.nextNode = new ListNode( insertItem );
} // end method insertAtBack
// remove first node from List
public Object removeFromFront() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
Object removedItem = firstNode.data; // retrieve data being removed
// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem; // return removed node data
} // end method removeFromFront
// remove last node from List
public Object removeFromBack() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
Object removedItem = lastNode.data; // retrieve data being removed
// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else // locate new last node
{
ListNode current = firstNode;
// loop while current node does not refer to lastNode
while ( current.nextNode != lastNode )
current = current.nextNode;
lastNode = current; // current is new lastNode
current.nextNode = null;
} // end else
return removedItem; // return removed node data
} // end method removeFromBack
// determine whether list is empty
public boolean isEmpty()
{
return firstNode == null; // return true if list is empty
} // end method isEmpty
// output list contents
public void print()
{
if ( isEmpty() )
{
System.out.println( "Empty " + name );
return;
} // end if
System.out.print(name + ":");
ListNode current = firstNode;
// while not at end of list, output current node's data
while ( current != null )
{
System.out.print(current.data + " " );
current = current.nextNode;
} // end while
System.out.println( " " );
} // end method print
// ListNode inner class definition.
// class to represent one node in a list
private class ListNode
{
//List can access these directly
public Object data; // data for this node
public ListNode nextNode; // reference to the next node in the list
// constructor creates a ListNode that refers to object
public ListNode( Object object )
{
this( object, null );
} // end ListNode one-argument constructor
// constructor creates ListNode that refers to
// Object and to next ListNode
public ListNode( Object object, ListNode node )
{
data = object;
nextNode = node;
} // end ListNode two-argument constructor
// return reference to data in node
public Object getData()
{
return data; // return Object in this node
} // end method getObject
// return reference to next node in list
public ListNode getNext()
{
return nextNode; // get next node
} // end method getNext
} // end class ListNode
} // end class List
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started