//Queue.java
import java.util.*;
public class Queue {
protected LinkedList list;
// Postcondition: this Queue object has been initialized.
public Queue() {
list = new LinkedList();
} // default constructor
// Postcondition: the number of elements in this Queue object has been
// returned.
public int size() {
return list.size();
} // method size
// Postcondition: true has been returned if this Queue object has no
// elements. Otherwise, false has been returned.
public boolean isEmpty() {
return list.isEmpty();
} // method isEmpty
// Postconditon: A copy of element has been inserted at the back of this
// Queue object. The averageTime (n) is constant and
// worstTime (n) is O (n).
public void enqueue(Object element) {
list.addLast(element);
} // method enqueue
// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: The element that was at the front of this Queue object -
// just before this method was called -- has been removed
// from this Queue object and returned.
public Object dequeue() {
return list.removeFirst();
} // method dequeue
// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: the element at index 0 in this Queue object has been
// returned.
public Object front() {
return list.getFirst();
} // method front
} // Queue class
//Car.java
public class Car {
protected int arrivalTime;
// The Car has been constructed.
public Car() {
}
// The car has been constructed from the arrival time.
public Car(int nextArrivalTime) {
arrivalTime = nextArrivalTime;
} // constructor with int parameter
// Postcondition: The arrival time of the Car has been returned.
public int getArrivalTime() {
return arrivalTime;
} // method getArrivalTime
} // class Car
//CarWash.java
import general.GUI;
import general.Process;
import java.util.*;
public class CarWash implements Process {
protected final String PROMPT = "\nIn the Input line, please enter "
+ "the next arrival time. The sentinel is ";
protected final int ILLEGAL_INPUT = -1;
protected final int SENTINEL = 999;
protected final int INFINITY = 10000; // indicates no car being washed
protected final int MAX_SIZE = 5; // maximum cars allowed in carQueue
protected final int WASH_TIME = 10; // minutes to wash one car
protected GUI gui;
protected Queue carQueue;
protected int currentTime, nextDepartureTime, numberOfCars,
sumOfWaitingTimes;
// The CarWash has been initialized.
public CarWash() {
gui = new GUI(this);
carQueue = new Queue();
currentTime = 0;
numberOfCars = 0;
sumOfWaitingTimes = 0;
nextDepartureTime = INFINITY;
gui.println(PROMPT + SENTINEL);
} // constructor
// Postcondition: The next arrival time, in
the string s, has been
// processed.
public void processInput(String s) {
int nextArrivalTime;
gui.println(s);
nextArrivalTime = parseOK(s);
if (nextArrivalTime != ILLEGAL_INPUT)
if (nextArrivalTime != SENTINEL) {
while (nextArrivalTime >= nextDepartureTime)
processDeparture();
processArrival(nextArrivalTime);
gui.println(PROMPT + SENTINEL);
} // processing next arrival
else { // process any cars remaining on the queue:
while (nextDepartureTime < INFINITY)
processDeparture();
printResult();
gui.freeze();
} // processing cars still on queue after last arrival
else
gui.println(PROMPT + SENTINEL);
} // processInput
// Postcondition: if a legal value for next arri