Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Time Event Waiting Time 1 Arrival 1 Departure 0 2 Arrival 3 Arrival 4 Departure 0 4 Departure 1 4 Arrival 5 Departure 0 5

image text in transcribed

image text in transcribed

image text in transcribed

Time

Event

Waiting Time

1

Arrival

1

Departure

0

2

Arrival

3

Arrival

4

Departure

0

4

Departure

1

4

Arrival

5

Departure

0

5

Arrival

6

Departure

0

6

Arrival

8

Arrival

8

Arrival

9

Arrival

10

Arrival

11

Departure

0

11

Arrival

11

Arrival

11

Arrival (Overflow)

12

Arrival (Overflow)

12

Arrival (Overflow)

13

Departure

3

13

Arrival

14

Departure

5

14

Departure

5

14

Departure

4

14

Arrival

15

Departure

3

15

Departure

4

15

Arrival

16

Departure

2

16

Departure

2

18

Arrival

19

Departure

1

20

Departure

1

22

Arrival

22

Departure

0

22

Arrival

23

Arrival

23

Arrival

24

Departure

0

24

Departure

1

24

Departure

1

image text in transcribed

Arrival Time

Time Dequeued

Waiting Time

5

0

5

15

10

7

25

18

12

35

23

12

45

33

13

55

42

14

-

Overflow

18

65

47

19

-

Overflow

25

75

50

image text in transcribed

Time

Event

Waiting Time

5

Arrival

5

Arrival

7

Arrival

12

Arrival

12

Arrival

13

Arrival

14

Arrival (Overflow)

15

Departure

0

18

Arrival

19

Arrival (Overflow)

25

Departure

10

25

Arrival

35

Departure

18

45

Departure

23

55

Departure

33

65

Departure

42

75

Departure

47

85

Departure

50

image text in transcribed

image text in transcribed

=====================================================================================

Textbook Solution to Original Problem which needs expanding:

CarWash Class

import java.util.LinkedList; import java.util.Queue;

public class CarWash {

public final static String OVERFLOW = " (Overflow) "; public final static String HEADING = " Time\tEvent\tWaiting Time "; public final static int MAX_SIZE = 5; public final static int WASH_TIME = 10; public final static int INFINITY = 10000; private Queue carQueue; private LinkedList results; //put results of wait time here private int waitingTime; private int sumOfWaitingTimes; private int numberOfCars; private int currentTime; private int nextDepartureTime; //when car currently being washed ends public CarWash(){ carQueue = new LinkedList(); results = new LinkedList(); results.add(HEADING); currentTime = 0; numberOfCars = 0; waitingTime = 0; sumOfWaitingTimes = 0; nextDepartureTime = INFINITY; } // public LinkedList process(int nextArrivalTime){ if (nextArrivalTime = nextDepartureTime) processDeparture(); return processArrival(nextArrivalTime); } public LinkedList processArrival(int nextArrivalTime){ currentTime = nextArrivalTime; //keep time moving forward if(carQueue.size() == MAX_SIZE) results.add(Integer.toString(currentTime) + "\tArrival" + OVERFLOW); else // not in overflow { results.add(Integer.toString(currentTime) + "\tArrival"); numberOfCars++; // add car to the queue if (nextDepartureTime == INFINITY) // no car being washed nextDepartureTime = currentTime + WASH_TIME; else carQueue.add(new Car(nextArrivalTime)); //results.add(" "); } return results; } // processArrival public LinkedList processDeparture(){ final String DEPARTURE = "\tDeparture\t\t"; int arrivalTime; currentTime = nextDepartureTime; results.add(Integer.toString(currentTime) + DEPARTURE + Integer.toString(waitingTime) ); //+ " "); if (!carQueue.isEmpty()){ Car car = carQueue.remove(); // take car from queue arrivalTime = car.getArrivalTime(); //when did it arrive? waitingTime = currentTime - arrivalTime; // how long waiting? sumOfWaitingTimes += waitingTime; //sum all waiting times nextDepartureTime = currentTime + WASH_TIME; //when will car be done? } else { waitingTime = 0; nextDepartureTime = INFINITY; } return results; } // processDeparture public LinkedList finishUp() { while (nextDepartureTime getResults() { if (numberOfCars == 0) results.add("There were no cars in the car wash."); else results.add(" The average waiting time in minutes was " + Double.toString((double) sumOfWaitingTimes / numberOfCars)); return results; } }

--------------------------------------------------------------------------------------------------------------

CarWashTest Class

import static org.junit.Assert.*;

public class CarWashTest extends CarWash{ @Test public void twoArrivalTest() { carWash.processArrival (5); results = carWash.processArrival (7); assertTrue (results.indexOf ("5\tArrival") != -1); assertTrue (results.indexOf ("7\tArrival") > results.indexOf ("5\tArrival")); } //method twoArrivalTest @Test public void overflowTest() { carWash.processArrival (5); carWash.processArrival (7); carWash.processArrival (8); carWash.processArrival (12); carWash.processArrival (12); assertTrue (carWash.processArrival (13).toString().indexOf (CarWash.OVERFLOW) == -1); /o overflow for arrival at 13 assertTrue (carWash.processArrival (14).toString().indexOf (CarWash.OVERFLOW) > 0); //overflow for arrival at 14 } //method overflowTest

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Car Class

public class Car { private int arrivalTime; public Car() { } // just in case public Car(int a){ arrivalTime = a; } // constructor public void setArrivalTime(int a) { arrivalTime = a; } //setArrivalTime public int getArrivalTime(){ return arrivalTime; } //getArrivalTime

} // Car

--------------------------------------------------------------------------------------------------------------

CarWashUser Class

import java.util.LinkedList; import java.util.Scanner;

public class CarWashUser {

public static void main(String[] args) { CarWash carWash = new CarWash(); Scanner in = new Scanner(System.in); int nextArrivalTime; while (true){ System.out.println("Please enter the next arrival time" + " or 999 to quit"); nextArrivalTime = in.nextInt(); if (nextArrivalTime == 999) break; carWash.process(nextArrivalTime); } carWash.finishUp(); System.out.println(" Here are the results of the simulation: "); LinkedList results = carWash.getResults(); for (String s: results) System.out.println(s); } }

----------------------------------------------------------------------------------------------------------------

Thank You!

*Original Car Wash Simulation Question and textbook solution posted below question Making the Speed o's Car Wash Simulation More Realistic Problem Expand the Car Wash Simulation Question with random arrival and service times Use unit testing JUnit Testing) of new methods after you have specified those methods Analysis The arrival times-with a Poisson distribution- should be generatedrandomly from the mean arrival time. Speedo has added a new feature: The service time is not necessarily 10 minutes, but depends on what the customer wants done, such as wash only, wash and wax, wash and vacuum, and so on. The service time for a car should be calculated just before the car enters the wash station- that's when the customer knows how much time will be taken until the customer leaves the car wash. The service times, also with a Poisson distribution, should be generator used for arrival times The input consists of three positive integers: the mean arrival time, the mean service time and the maximum arrival time. Repeatedly re prompt until each value is a positive integer Calculate the average waiting time and the average queue line) length, both to one fractional digits. The average waiting time is the sum of the waiting times divided by the number of customers The average queue length is the sum of the queue lengths for each minute of the simulation divided by the number of minutes until the last customer departs. To calculate the sum of the queue lengths, we add, for each minute of the simulation, the total number of customers on the queue during that minute. We can calculate this sum another way: we add, for each customer, the total number of minutes that customer was on the queue. But this is the sum of the waiting times! So we can calculate the average queue length as the sum of the waiting times divided by the number of minutes of the simulation until the last cu stomer departs. And we already calculated the sum of the waiting times for the averag e waiting time Also calculate the number of overflows. Use a seed of 100 for the random number generator, so the output you get should have the same values as the output from the following tests System Test l: the input is in boldface) Please enter the mean arrival time: 3 Please enter the mean service time: 5 Please enter the maximum arrival time: 25

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

More Books

Students also viewed these Databases questions