Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the question: Here is some of the code that is needed to be used. class

ID: 3830696 • Letter: H

Question

Here is the question:

Here is some of the code that is needed to be used.

class ListNode< T >

{

   // package access members; List can access these directly

   T data; // data for this node

   ListNode< T > nextNode; // reference to the next node in the list

   // constructor creates a ListNode that refers to object

   ListNode( T object )

   {

this( object, null );

   } // end ListNode one-argument constructor

   // constructor creates ListNode that refers to the specified

   // object and to the next ListNode

   ListNode( T object, ListNode< T > node )

   {

data = object;

nextNode = node;

   } // end ListNode two-argument constructor

   // return reference to data in node

   T getData()

   {

return data; // return item in this node

   } // end method getData

   // return reference to next node in list

   ListNode< T > getNext()

   {

return nextNode; // get next node

   } // end method getNext

} // end class ListNode< T >

// class List definition

public class List< T >

{

   private ListNode< T > firstNode;

   private ListNode< T > lastNode;

   private String name; // string like "list" used in printing

   private int size; // number of elelments in list

   // 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;

size = 0;

   } // end List one-argument constructor

   // insert item at front of List

   public void insertAtFront( T insertItem )

   {

if ( isEmpty() ) // firstNode and lastNode refer to same object

   firstNode = lastNode = new ListNode< T >( insertItem );

else // firstNode refers to new node

   firstNode = new ListNode< T >( insertItem, firstNode );

size++;

   } // end method insertAtFront

   // insert item at end of List

   public void insertAtBack( T insertItem )

   {

if ( isEmpty() ) // firstNode and lastNode refer to same object

   firstNode = lastNode = new ListNode< T >( insertItem );

else // lastNode's nextNode refers to new node

   lastNode = lastNode.nextNode = new ListNode< T >( insertItem );

size++;

   } // end method insertAtBack

   // remove first node from List

   public T removeFromFront() throws EmptyListException

   {

if ( isEmpty() ) // throw exception if List is empty

   throw new EmptyListException( name );

T removedItem = firstNode.data; // retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

   firstNode = lastNode = null;

else

   firstNode = firstNode.nextNode;

size--;

return removedItem; // return removed node data

   } // end method removeFromFront

   // remove last node from List

   public T removeFromBack() throws EmptyListException

   {

if ( isEmpty() ) // throw exception if List is empty

   throw new EmptyListException( name );

T removedItem = lastNode.data; // retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

   firstNode = lastNode = null;

else // locate new last node

{

   ListNode< T > 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

size--;

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

   // return number of elements in list

   public int size()

   {

return size; // return number of elements

   } // end method size

   // output list contents

   public void print()

   {

if ( isEmpty() )

{

   System.out.printf( "Empty %s ", name );

   return;

} // end if

System.out.printf( "The %s is: ", name );

ListNode< T > current = firstNode;

// while not at end of list, output current node's data

while ( current != null )

{

   System.out.printf( "%s ", current.data );

   current = current.nextNode;

} // end while

System.out.println();

   } // end method print

} // end class List< T >

* *

(Supermarket Simulation) Write a program that simulates a checkout line at a supermarket. The line is a queue object. Customers (i.e., customer objects) arrive in random integer intervals of from 1 to 4 minutes. Also, each customer is serviced in random integer intervals of from 1 to 4 minutes. Obviously, the rates need to be balanced. If the average arrival rate is larger than the average service rate, the queue will grow infinitely. Even with "balanced" rates, randomness can still cause long lines. Run the supermarket simulation for a 12-hour day (720 minutes), using the following algorithm a) Choose a random integer between 1 and 4 to determine the minute at which the first customer arrives b) At the first customer's arrival time, do the following: Determine customer's service time (random integer from 1 to 4). Begin servicing the customer. Schedule arrival time of next customer (random integer 1 to 4 added to the current time) c) For each simulated minute of the day, consider the following: If the next customer arrives, proceed as follows: Say so Enqueue the customer. Schedule the arrival time of the next customer. If service was completed for the last customer, do the following: Say so Dequeue next customer to be serviced. Determine customer's service completion time (random integer from 1 to 4 added to the current time) Now run your simulation for 720 minutes and answer each of the following: a) What is the maximum number of customers in the queue at any time? b) What is the longest wait any one customer experiences? c) What happens if the arrival interval is changed from 1 to 4 minutes to 1 to 3 minutes?

Explanation / Answer

Following is the code that runs the simulation for 720 minutes and prints out the maximum customer in queue at any time and longest wait time of any customer.

class ListNode< T >
{
// package access members; List can access these directly
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list

// constructor creates a ListNode that refers to object
ListNode( T object )
{
this( object, null );
} // end ListNode one-argument constructor

// constructor creates ListNode that refers to the specified
// object and to the next ListNode
ListNode( T object, ListNode< T > node )
{
data = object;
nextNode = node;
} // end ListNode two-argument constructor

// return reference to data in node
T getData()
{
return data; // return item in this node
} // end method getData

// return reference to next node in list
ListNode< T > getNext()
{
return nextNode; // get next node
} // end method getNext
} // end class ListNode< T >

// class List definition
public class List< T >
{
private ListNode< T > firstNode;
private ListNode< T > lastNode;
private String name; // string like "list" used in printing
private int size; // number of elelments in list

// 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;
size = 0;
} // end List one-argument constructor

// insert item at front of List
public void insertAtFront( T insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode< T >( insertItem );
else // firstNode refers to new node
firstNode = new ListNode< T >( insertItem, firstNode );
size++;
} // end method insertAtFront

// insert item at end of List
public void insertAtBack( T insertItem )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode< T >( insertItem );
else // lastNode's nextNode refers to new node
lastNode = lastNode.nextNode = new ListNode< T >( insertItem );
size++;
} // end method insertAtBack


// remove first node from List
public T removeFromFront() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );

T removedItem = firstNode.data; // retrieve data being removed

// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;

size--;
return removedItem; // return removed node data
} // end method removeFromFront

// remove last node from List
public T removeFromBack() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );

T removedItem = lastNode.data; // retrieve data being removed

// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else // locate new last node
{
ListNode< T > 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

size--;
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

// return number of elements in list
public int size()
{
return size; // return number of elements
} // end method size

// output list contents
public void print()
{
if ( isEmpty() )
{
System.out.printf( "Empty %s ", name );
return;
} // end if

System.out.printf( "The %s is: ", name );
ListNode< T > current = firstNode;

// while not at end of list, output current node's data
while ( current != null )
{
System.out.printf( "%s ", current.data );
current = current.nextNode;
} // end while

System.out.println();
} // end method print
} // end class List< T >


import java.security.SecureRandom;

public class SuperMarket {
private static SIMULATION_MINUTES = 720;
private static SecureRandom sr = new SecureRandom();
private static Queue<Customer> line = new Queue();
private static int maxCustomers = 0;
private static int maxWaitTime = 0;
  
public static void main(String[] args) {
// beginning of time
int time = 0;
// arrival time of first customer
int nextCustArrivalTime = getRandomNumber(4);
// first customer arrived
time = nextCustArrivalTime;
Customer currentCustomer = new Customer(nextCustArrivalTime);
// service time for first customer
int serviceTime = getRandomNumber(4);
currentCustomer.setServiceCompletionTime(time + serviceTime);
// arrival time of next customer
nextCustArrivalTime = getRandomNumber(4);
  
// for each tick of the time
for (int time = 0; time < SIMULATION_MINUTES; time++) {
// if the next customer arrives
if (time == nextCustArrivalTime) {
line.enqueue(new Customer(time));
nextCustArrivalTime = time + getRandomNumber(4);

// update maximum customers in the queue
if (maxCustomers < line.size) {
maxCustomers = line.size;
}
}

// if service was complete for the current/last customer.
if (currentCustomer.getServiceCompletionTime == time) {
// update maximum wait time of customer
if (maxWaitTime < currentCustomer.getWaitTime) {
maxWaitTime = currentCustomer.getWaitTime;
}
  
currentCustomer = line.dequeue();
serviceTime = getRandomNumber(4);
currentCustomer.setServiceCompletionTime(time + serviceTime);
}
  
} // end of 720 minutes simulation

// print maxCustomers and maxWaitTime
System.out.println("Maximum number of customers in the queue at any time: " + maxCustomers);
System.out.println("Longest wait any one customer experiences: " + maxWaitTime);
}

// return random number between 1 and end (inclusive)
public static getRandomNumber(int end) {
return sr.nextInt(end) + 1;
}
}

public class Customer {
int arrivalTime;
int serviceCompletionTime;

// one-argument constructor
public Customer(int time) {
arrivalTime = time;
} // end one-argument constructor

// return arrivalTime of the customer
public void getArrivalTime() {
return arrivalTime;
} // end method getArrivalTime

// set serviceCompletionTime of the customer
public void setServiceCompletionTime(int time) {
serviceCompletionTime = time;
} // end method setServiceCompletionTime

// return serviceCompletionTime of the Customer
public int getServiceCompletionTime() {
return serviceCompletionTime;
} // end method getServiceCompletionTime

// return wait time of the customer
public int getWaitTime() {
return serviceCompletionTime - arrivalTime;
} // end method getWaitTime
}

public class Queue< T >
{
private List< T > queueList;

// no-argument constructor
public Queue()
{
queueList = new List<T>("queue");
} // end Queue no-argument constructor

// add object to queue
public void enqueue(T object)
{
queueList.insertAtBack(object);
} // end method enqueue

// remove object from queue
public T dequeue() throws EmptyListException
{
return queue.removeFromFront();
} // end method dequeue

// determine if queue is empty
public boolean isEmpty()
{
return queueList.isEmpty();
} // end method isEmpty

// returns number of elements in queue
public int size()
{
return queueList.size();
} // end method size

// output queue contents
public void print()
{
queueList.print();
} // end method print
} // end class Queue

public class EmptyListException extends RuntimeException
{
// no-argument constructor
public EmptyListException()
{
this("list"); // call other EmptyListException constructor
} // end EmptyListException no-argument constructor

// one-argument constructor
public EmptyListException(String name)
{
super(name + " is empty"); // call superclass constructor
} // end EmptyListException one-argument constructor
} // End class EmptyListException

For question c) change the interval from 4 to 3 i.e, change the argument passed to getRandomNumber from 4 to 3 as getRandomNumber(3) from getRandomNumber(4) and run the program/simulation again and check the maximum customer in queue at any time and longest wait time of any customer.