Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your second programming assignment, modify your first programming assignment to store your Customer objects in a priority queue instead of a regular queue. An

For your second programming assignment, modify your first programming assignment to store your Customer objects in a priority queue instead of a regular queue.

An updated PriorityCustomer class is provided for you (download from Canvas).You must use that class, without alternations, for the creation of your PriorityCustomer objects.You must analyze the class and use the provided methods to achieve the desired functionality of the program.You will also need to create two additional classes.The first will be a PriorityQueue class that will represent the data structure for holding your Customer objects.In your PriorityQueue class, you will create the actual heap that will act as the priority queue.You must use an array representation of a heap.No ArrayLists or linked structures! The second class you will need to create is a driver where your store simulation will take place.

Customers with a priority value higher than other existing customers should be placed in front of them.This is simulated by utilizing a Max Heap to implement your priority queue.The only exception to this is for the customer in the front of the line (the one currently being serviced).If a new customer is added to the line with a higher priority than the front customer, the new customer should not be put in front of the customer being serviced - only those that are also waiting in line. The store simulation with regards to the service time of customers, probability that new customers show up, and program output will be the same as it was for the first program.

The program (driver) should simulate 60 minutes of activity at the store. Each iteration of your program should represent one minute.At each iteration (minute), your program should do the following:

Check to see if new customers are added to the queue.There is a 25% chance that new customers show up (need to be added to the queue) every minute.This does not mean you should add a customer every four iterations, but rather each iteration should have its own 25% chance.

Update the customer object currently being serviced (if one exists).This will be the customer object at the front of the queue.If the customer has been completely serviced, remove them from the queue.

During execution, your program should output the following information:

When a new customer is added to the queue, output, "New customer added!Queue length is now X" where X is the size of the queue after the new customer has been added.

When a customer has been completely serviced, output, "Customer serviced and removed from the queue.Quest length is now X" where X is the size of the queue after the customer has been removed.

At the end of each iteration (minute), output, "---------------------------------------------------"to visually identify the passing of time.

When your simulation ends, your program should also output the following information:

Total number of customers serviced

Maximum line length during the simulation

First Class

/*Program: CustomerQueue ~ PriorityCustomer Class

* Professor: D. Jugan

* Date:Summary: Creates a Customer object to be used in the line queue. Holds the serviceTime

* for each customer.

* Functionality:

*Constructor: Random ServiceTime (1-5)

*Public Methods: getServiceTime, newMinute

*/

import java.util.Random;

public class PriorityCustomer {

private int serviceTime;// ServiceTime for this Customer

private int priority;// Priority for this Customer

/// Constructor

public PriorityCustomer() {

serviceTime = new Random().nextInt(5) + 1;// Randomly assign required service time 1-5

priority = new Random().nextInt(5) + 1;// Randomly assign priority 1-5}

public int getPriority() {

return priority;

}

/// Getter for ServiceTime

public int getServiceTime() {

return serviceTime;

}

/// Decrement ServiceTime by 1

public void decServiceTime() {

serviceTime--;

}

}

}

Classes from previous assignment I had done

*********LinkedListQueue***********

/* Linked list Queue class

* This class is to create the line that is held in a queue

*/

public class LinkedListQueue {

//feilds for creating the queue

private Customer top;

private Customer botm;

private int length;

//creating default elements in the queue

public LinkedListQueue(){

top = null;

botm = null;

length = 0;

}

//method to check if the queue is empty

public boolean isEmpty() {

return top == null;

}

//method to add elements into the queue

public void enqueue(Customer person) {

length++;

if (isEmpty()) {

top = person;

}

else {

top.setNext(person);

}

botm = person;

}

//method that removes element from the queue

public Customer dequeue() {

if(isEmpty())

return null;

length--;

Customer object = top;

if(top == botm) {

top = null;

}

top = botm.getNext();

return object;

}

//first customer object

public Customer getFirst() {

if( isEmpty())

return null;

Customer object = top;

return object;

}

//method that gets the length of the queue

public int getLength() {

return length;

}

}

******* DRIVER *******

import java.util.Random;

//Driver Class with the main method simulates the store

public class Driver {

public static void main (String[] args) {

//creating the queue line for the customers

LinkedListQueue Customers = new LinkedListQueue();

//creating random number generator for 25% of customer being added

Random rand = new Random();

//creates new customer object

Customer customers = new Customer();

//simulating 60 minutes and determines the chances of 25% of a new customer entering the store

for (int i = 0; i<60; i++) {

int chance = rand.nextInt(4) + 1;

//determine if customer is added to the queue if equal to 1 then enqueue the new customer

if (chance == 1) {

Customers.enqueue(new Customer());

//displays to the user that a customer was added to the queue

System.out.println("New customer added!Queue length is now " + Customers.getLength());

Customer c = new Customer();

System.out.println("initial: " + c.getServiceTime());

c.decServiceTime();

System.out.println("after: " + c.getServiceTime());

}

//if the chance is not equal to 1 then service time is decremented

else {

customers.decServiceTime();

if (customers.getServiceTime() == 0) {

//Dequeue the line if the service time is 0

Customers.dequeue();

//Display a message to the user that a customer was serviced and the length of the line

System.out.println("Customer serviced and removed from the queue.Queue length is now " + Customers.getLength());

}

}

System.out.println("---------------------------------------------------");

}

System.out.println("Total number of customers serviced " + Customers);

System.out.println("Maximum line length during the simulation" + Customers.getLength());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions