Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Train Simulator in Java ------ Passenger.java package Lab08; /** * A class that represents a train passenger. * * @author Charles Hoot * @modifiedBy atb

Train Simulator in Java

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

------

Passenger.java

package Lab08; /**  * 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; } } 

--------

QueueInterface.java

package Lab08; /**  An interface for the ADT queue.  @author Frank M. Carrano  @author Timothy M. Henry  @version 4.0 */ public interface QueueInterface { /** Adds a new entry to the back of this queue.  @param newEntry An object to be added. */  public void enqueue(T newEntry); /** Removes and returns the entry at the front of this queue.  @return The object at the front of the queue.  @throws EmptyQueueException if the queue is empty before the operation. */  public T dequeue(); /** Retrieves the entry at the front of this queue.  @return The object at the front of the queue.  @throws EmptyQueueException if the queue is empty. */  public T getFront(); /** Detects whether this queue is empty.  @return True if the queue is empty, or false otherwise. */  public boolean isEmpty(); /** Removes all entries from this queue. */  public void clear(); } // end QueueInterface 

--

Station.java

package Lab08; import java.util.*; /**  * 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; } }

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

Train.java

package Lab08; import java.util.*; /**  * 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 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 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"; } }

-----

TrainSimulation.java

package Lab08; import java.text.*; import java.util.*; /**  * 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 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 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   Application #3 Using the Queue interface and its implementation ArrayDeque class from java.util write a program that simulates a train route. A train route consists of a number of stations (assume 10, represented by the variable STATIONS), 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 variables MIN_TIME_TO_NEXT_STATION and MAX_TIME_TO_NEXT_STATION). Associated with each station is a queue of passengers. Random number of passengers (between 0 and 10, see variable MAX 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) Trains leave a station at regular intervals (represented by the variable TRAIN INTERVAL) and visit the stations on the route. When a train stops at a station, all passengers for that station exit first. Then any passenger waiting in the queue at the station boards the train until either the queue is empty, or the train is full. Once the simulation is completed the final report is printed giving the statistics from the simulation. See the sample run of the application below Lab08.zip contains all the classes needed for implementation of this project.  Application #3 Using the Queue interface and its implementation ArrayDeque class from java.util write a program that simulates a train route. A train route consists of a number of stations (assume 10, represented by the variable STATIONS), 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 variables MIN_TIME_TO_NEXT_STATION and MAX_TIME_TO_NEXT_STATION). Associated with each station is a queue of passengers. Random number of passengers (between 0 and 10, see variable MAX 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) Trains leave a station at regular intervals (represented by the variable TRAIN INTERVAL) and visit the stations on the route. When a train stops at a station, all passengers for that station exit first. Then any passenger waiting in the queue at the station boards the train until either the queue is empty, or the train is full. Once the simulation is completed the final report is printed giving the statistics from the simulation. See the sample run of the application below Lab08.zip contains all the classes needed for implementation of this project

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

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions

Question

(a) What model can you fit with the original 42 experiment? Pg45

Answered: 1 week ago