Question: package edu.saintpaul.wk7.examples.Assignment6; // FILE: Carwash.java // This program illustrates the use of the carWashSimulate method which uses // a simple queue to simulate cars waiting

package edu.saintpaul.wk7.examples.Assignment6;

// FILE: Carwash.java // This program illustrates the use of the carWashSimulate method which uses // a simple queue to simulate cars waiting at a car wash.

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

/****************************************************************************** * The CarWash Java application illustrates the use of * the carWashSimulate method. * The illustration uses the following values: * * washTime = 240 * arrivalTime = 0.0025 * totalTime = 6000 * * *

Java Source Code for this class:
* * http://www.cs.colorado.edu/~main/applications/CarWash.java * * * @author Michael Main * (main@colorado.edu) * * @version * Jun 12, 1998 ******************************************************************************/ public class CarWash { /** * The main method activates carWashSimulate with the values: * * washTime = 240 * arrivalTime = 0.0025 * totalTime = 6000 * *
The String argument (args) is not used in * this implementation. **/ public static void main(String[ ] args) { final int WASHTIME = 240; final double ARRIVALPROB = 0.0025; final int TOTALTIME = 6000; carWashSimulate(WASHTIME, ARRIVALPROB, TOTALTIME); } /** * Simulate the running of a car washer for a specified amount of time. * @param washTime * the number of seconds required to wash one car * @param arrivalProb * the probability of a customer arriving in any second, for example * 0.1 is 10% * @param totalTime * the total number of seconds for the simulation *
Precondition:
* washTime and totalTime are positive; * arrivalProb lies in the range 0 to 1. *
Postcondition:
* The method has simulated a car wash where washTime is the * number of seconds needed to wash one car, arrivalProb is * the probability of a customer arriving in any second, and * totalTime is the total number of seconds for the * simulation. Before the simulation, the method has written its three * parameters to System.out. After the simulation, the method * has written two pieces of information to System.out: * (1) The number of cars washed, and (2) The average waiting time for * customers that had their cars washed. (Customers that are still in the * queue are not included in this average). * @exception java.lang.IllegalArgumentException * Indicates that one of the arguments violates the precondition. **/ public static void carWashSimulate (int washTime, double arrivalProb, int totalTime) { Queue arrivalTimes = new LinkedList( ); int next; BooleanSource arrival = new BooleanSource(arrivalProb); Washer machine = new Washer(washTime); Averager waitTimes = new Averager( ); int currentSecond; // Write the parameters to System.out. System.out.println("Seconds to wash one car: " + washTime); System.out.print("Probability of customer arrival during a second: "); System.out.println(arrivalProb); System.out.println("Total simulation seconds: " + totalTime); // Check the precondition: if (washTime <= 0 || arrivalProb < 0 || arrivalProb > 1 || totalTime < 0) throw new IllegalArgumentException("Values out of range");

for (currentSecond = 0; currentSecond < totalTime; currentSecond++) { // Simulate the passage of one second of time.

// Check whether a new customer has arrived. if (arrival.query( )) arrivalTimes.add(currentSecond); // Check whether we can start washing another car. if ((!machine.isBusy( )) && (!arrivalTimes.isEmpty( ))) { next = arrivalTimes.remove( ); waitTimes.addNumber(currentSecond - next); machine.startWashing( ); }

// Subtract one second from the remaining time in the current wash cycle. machine.reduceRemainingTime( ); }

// Write the summary information about the simulation. System.out.println("Customers served: " + waitTimes.howManyNumbers( )); if (waitTimes.howManyNumbers( ) > 0) System.out.println("Average wait: " + waitTimes.average( ) + " sec"); } }

// File: Washer.java from the package edu.colorado.simulations // Additional javadoc documentation is available from the Washer link at // http://www.cs.colorado.edu/~main/docs/

package edu.saintpaul.wk7.examples.Assignment6;

/****************************************************************************** * An Washer simulates a simple washing machine. * *

Java Source Code for this class:
* * http://www.cs.colorado.edu/~main/edu/colorado/simulations/Washer.java * * * @author Michael Main * (main@colorado.edu) * * @version * Jun 12, 1998 ******************************************************************************/ public class Washer { private int secondsForWash; // Seconds for a single wash private int washTimeLeft; // Seconds until this washer is no longer busy

/** * Initialize a washer. * param s * the number of seconds required for one wash cyle of this washer *

Postcondition:
* This washer has been initialized so that it takes s

/** * Determine whether this washer is currently busy. * @param - none * @return * true if this washer is busy (in a wash cycle); * otherwise false **/ public boolean isBusy( ) { return (washTimeLeft > 0); }

/** * Reduce the remaining time in the current wash cycle by one second. * @param - none *

Postcondition:
* If a car is being washed, then the remaining time in the current * wash cycle has been reduced by one second. **/ public void reduceRemainingTime( ) { if (washTimeLeft > 0) washTimeLeft--; }

/** * Start a wash cycle going for this washer. * @param - none *

Precondition:
* isBusy() is false. *
Postcondition:
* This washer has started simulating one wash cycle. Therefore, * isBusy() will return true until the required * number of simulated seonds have passed. * @exception IllegalStateException * Indicates that this washer is busy. **/ public void startWashing( ) { if (washTimeLeft > 0) throw new IllegalStateException("Washer is already busy."); washTimeLeft = secondsForWash; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!