Question: Application #3 Using the Queue interface and its implementation ArrayDequeclass from java.utilwrite a program that simulates a train route. A train route consists of a

Application #3

Using the Queueinterface and its implementation ArrayDequeclass from java.utilwrite a program that simulates a train route.

A train route consists of a number of stations (assume 10, represented by the variableSTATIONS), starting with station 0, and ending with a FINAL_STATION(value of STATIONS 1). The time that the train needs to travel between a pair of consecutive stations on the route is randomly generated (number between 5 and 9, represented by the variablesMIN_TIME_TO_NEXT_STATIONand MAX_TIME_TO_NEXT_STATION).

Associated with each stationis a queue of passengers. Random number of passengers (between 0 and 10, see variableMAX_NUM_OF_PASSENGERS) is generated at each tick of the simulation clock. Each passenger is given a random entry station, and a random destination station (the entry station is always smaller than the destination station).

Step#1 implement generateStations()method.

Run your application.

OUTCOME: 10 stations should be created with valid times to next station

Step #2 implementgeneratePassengers()method.

Run your application.

OUTCOME: At every time marker, between 0 and 9 passengers should be created with valid start and destination stations. Number of waiting passengers should be changed accordingly. Number of passengers on trains should be 0.

Step#3 implementstartNewTrain()method.

Run your application.

OUTCOME: At everyTRAIN_INTERVAL marker a new train should be created

Step #4 implementmoveTrains()method.

Run your application

OUTCOME: At every time marker you should see trains unloading and loading the appropriate number of passengers. If a train reaches the final station it should no longer be in the queue. Your produced output should be similar to the sample run below.

CODE THAT NEEDS IMPLEMENTATION

import java.text.*; import java.util.*; import java.util.concurrent.*; /**  * A class that simulates a train line with passengers.  *  * @author YOUR NAME  * @version 10/16/2018  */ public class TrainSimulation { // an array that will hold all stations private Station[] allStations; // a queue that will hold all passengers, so we can print statistics // when the simulation is over private Queue allPassengers; // a queue that will hold all trains private Queue allTrains; // keeps track of the number of trains en route private int trainCount; // total number of passengers created private int passengersCreated; // total number of passengers on the trains private int passengersOnTrains; // total number of passengers off the trains private int passengersDelivered; // number of stations for the simulation private final int STATIONS = 10; private final int FINAL_STATION = STATIONS - 1; // frequency of trains departing from the station 0 private final int TRAIN_INTERVAL = 5; // max number of passengers per train private final int TRAIN_CAPACITY = 20; // simulation time private final int DURATION = 50; // time range between two stations private final int MIN_TIME_TO_NEXT_STATION = 5; private final int MAX_TIME_TO_NEXT_STATION = 9; // max number of passengers to be randomly generated in one simulation tick private final int MAX_NUM_OF_PASSENGERS = 10; public Random generator; public TrainSimulation() { // create an array that will hold all stations this.allStations = new Station[STATIONS]; // create a queue that will hold all trains this.allTrains = new ArrayDeque<>(); // create a queue that will hold all passengers this.allPassengers = new ArrayDeque<>(); // initialize counters this.trainCount = 0; this.passengersCreated = 0; this.passengersOnTrains = 0; this.passengersDelivered = 0; // create Random object to be used for generating random values this.generator = new Random(); // generate all stations generateStations(); } public void generateStations() { // TODO Project 3 - Step #1   // fill the allStation array with Station objects where the value // of "time to next station" is randomly generated. // for each created station print the station's "time to next" } public void startNewTrain(int clock) { if ((clock % TRAIN_INTERVAL) == 0) { // TODO Project 3 - Step #3  // create new train object and add it to the allTrains queue // print the new train object // increment the train count by 1 } } public void generatePassengers(int clock) { // randomly generate number of new passengers int newPassengers = this.generator.nextInt(MAX_NUM_OF_PASSENGERS + 1); // TODO Project 3 - Step #2  // create the calculated number of passenger objects. For each new passenger // randomly generate the destination station and the start station. Remember // that the start station must be smaller than the destination station // add each passenger to the allPassengers queue and increment the number // of passengers created - you will need this information for the statistics // add each passenger to its appropriate start station - use allStations array } public void moveTrains(int clock) { System.out.println(" >> Moving each train <<"); int trainsToCheck = this.trainCount; for (int i = 0; i < trainsToCheck; i++) { // TODO Project 3 - Step #4  // 1. remove train from the allTrains queue // 2. move the train (see the Train class) // 3. if the train's time to the next station is 0 the train is at the station // a. unload the passengers from the train // b. load the waiting passengers on the train // c. update the passenger counters // d. if the train is at the final station // print the appropriate message and decrement the number of trains // otherwise update train's next station data: train.updateStation(allStations[stationNo].getTimeToNextStation()) // put the train back on the queue, and print the the train object // 4. if the train's time to the next station is not 0 the train is still in transit // so put it back to the queue and print the train object } } public void reportAtTimeMarker(int simulationTimer) { int passengersWaiting = this.passengersCreated - this.passengersOnTrains - this.passengersDelivered; System.out.println("-----At time marker " + simulationTimer + " -> passengers waiting: " + passengersWaiting + "\t on trains: " + this.passengersOnTrains + "\t active trains: " + this.trainCount + "-----"); System.out.println(); } public void finalSimulationReport(int clock) { DecimalFormat df = new DecimalFormat("#0.00"); System.out.println("***************** Final Report ***************"); System.out.println("The total number of passengers is " + this.passengersCreated); System.out.println("The number of passengers currently on a train " + this.passengersOnTrains); System.out.println("The number of passengers delivered is " + this.passengersDelivered); int passengersWaiting = this.passengersCreated - this.passengersOnTrains - this.passengersDelivered; System.out.println("The number of passengers waiting is " + passengersWaiting); int waitBoardedSum = 0; Passenger passenger; for (int i = 0; i < this.passengersCreated; i++) { passenger = this.allPassengers.poll(); if (passenger.boarded()) { waitBoardedSum += passenger.waitTime(clock); } } System.out.print("The average wait time for passengers that have boarded is "); System.out.println(df.format((double) waitBoardedSum / (this.passengersOnTrains + this.passengersDelivered))); } public static void main(String args[]) { System.out.println("************** TRAIN SIMULATION ************** "); TrainSimulation simulator = new TrainSimulation(); System.out.println("--> Starting the clock; duration set to " + simulator.DURATION + " "); for (int clock = 0; clock < simulator.DURATION; clock++) { simulator.generatePassengers(clock); simulator.startNewTrain(clock); simulator.moveTrains(clock); simulator.reportAtTimeMarker(clock); } simulator.finalSimulationReport(simulator.DURATION); } } 

Train Class

import java.util.*; import java.util.concurrent.*; /**  * A class that represents a train in a simulation.  *  * @author Charles Hoot  * @version 10/16/2018  * @modifiedBy atb  */ public class Train { private Queue onTrain; private int numberOnTrain; private int capacity; private int nextStation; private int timeToArrivalAtNextStation; private int trainNo; private static int trainsCreated = 0; public Train(int capacity) { this.onTrain = new ArrayDeque<>(); this.numberOnTrain = 0; this.capacity = capacity; this.nextStation = 0; //the simulation will move the train so it is operational this.timeToArrivalAtNextStation = 1; trainsCreated++; this.trainNo = trainsCreated; } public int getTrainNo() { return this.trainNo; } public int getNextStation() { return this.nextStation; } public int getTimeToNext() { return this.timeToArrivalAtNextStation; } public void move() { this.timeToArrivalAtNextStation--; } public void updateStation(int timeToNext) { this.timeToArrivalAtNextStation = timeToNext; this.nextStation++; } public int unloadPassengers(int station) { int count = this.numberOnTrain; Passenger passenger; for (int i = 0; i < count; i++) { passenger = this.onTrain.poll(); if (passenger.getDestination() != station) { // Not there yet, put them back this.onTrain.offer(passenger); } else { // They arrived this.numberOnTrain--; } } int passengersLeaving = count - this.numberOnTrain; System.out.print("\tTrain " + this.trainNo + " is at station " + station +"; unloaded " + passengersLeaving); return passengersLeaving; } public int loadPassengers(Station station, int clock) { int count = numberOnTrain; boolean passengerWaiting = station.isWaiting(); Passenger passenger; while ((this.numberOnTrain < this.capacity) && passengerWaiting) { passenger = station.getPassenger(); this.onTrain.offer(passenger); passenger.boardTrain(clock); passengerWaiting = station.isWaiting(); this.numberOnTrain++; } int passengersEntering = this.numberOnTrain - count; System.out.print("; loaded " + passengersEntering + " passengers"); System.out.println("; Space left " + (this.capacity - this.numberOnTrain)); return passengersEntering; } public String toString() { return "Train " + this.trainNo + " capacity of " + this.capacity + " arriving at station " + this.nextStation + " in " + this.timeToArrivalAtNextStation + " minutes; currently " + this.numberOnTrain + " passengers on board"; } }

Station Class

import java.util.*; import java.util.concurrent.*; /**  * A class that represents a train station  * where passengers will wait.  *  * @author Charles Hoot  * @modifiedBy atb  * @version 10/16/2018  */ public class Station { private Queue waiting; private int timeToNextStation; private int stationNumber; /**  * Constructor for objects of class Station  */  public Station(int stationNumber, int timeToNext) { this.waiting = new ArrayDeque<>(); this.timeToNextStation = timeToNext; this.stationNumber = stationNumber; } public void addPassenger(Passenger passenger) { this.waiting.offer(passenger); } public boolean isWaiting() { return !this.waiting.isEmpty(); } public Passenger getPassenger() { return this.waiting.poll(); } public int getTimeToNextStation() { return this.timeToNextStation; } public String toString() { return "Station " + this.stationNumber + " has " + (isWaiting()?"some":"no") + " passengers waiting; the time to next station is " + this.timeToNextStation; } }

Passenger Class

/**  * A class that represents a train passenger.  *  * @author Charles Hoot  * @modifiedBy atb  * @version 10/16/2018  */ public class Passenger { private int startedWaiting; private int boardedAt; private boolean boardedTrain; private int destination; private int startStation; Passenger(int startStation, int destination, int createdAt) { this.startedWaiting = createdAt; this.startStation = startStation; this.destination = destination; this.boardedTrain = false; } public int getDestination() { return this.destination; } public void boardTrain(int clock) { this.boardedAt = clock; this.boardedTrain = true; } public int waitTime(int clock) { int result = clock - this.startedWaiting; if(this.boardedTrain) result = this.boardedAt - this.startedWaiting; return result; } public boolean boarded() { return this.boardedTrain; } public String toString() { return "Passenger arrived at time marker " + this.startedWaiting + " at station " + this.startStation + " heading to " + this.destination; } } 

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!