In Java take the sample code Simulator below and break it down into the followin
ID: 3865978 • Letter: I
Question
In Java take the sample code Simulator below and break it down into the following parts: The code is done, all you are doing is taking it and put into these 5 methods. If any portion is missing please include. Code should contain no errors.
1. getStopsFromUser(): int
Prompts user to enter number of stops.
Checks input (must be integer > 1) and gives error message if it is not. Loops until correct value is entered.
Returns the obtained value to caller
2. getInputFile(): File
Loop until the path given is for file that exists
Prompts user for the input file path
If no file is given (user presses enter) then use the default path name (C:/train/customer-data.txt)
Use given path to create File instance
Checks if file exists.
If file does not exist gives an error message and loop until correct path is entered.
Return File instance
3. getInputFile(int stops, File file):
Creates empty list (ArrayList<Customer> list)
In while loop
Reads file line by line
Parses the four integer values per line
Checks data that it meets the above constraints. If data is incorrect, stops parsing, gives error message, and returns null
Creates instance of customer class (using data from single line from data file)
Stores instance of customer into list.
Exits loop when no more lines to read.
Returns list
4. run(int stops, ArrayList<Customer> custList):
Create an instance of Train and pass through constructor stops and custList
Call Train’s simulate method
Call Train’s displayStops method
5. main() method:
Creates an instance of Simulation class
Creates custList of type ArrayList<Customer> and assigns null
Calls Simulator’s getStopsFromUser() method and saves the return value in variable (stops)
Loops as long as custList is null
Calls Simulator’s getInputFile() method. Save the returned File instance
Call Simulator’s checkFile() method passing it stops and instance of File. Save returned value in custList
Call Simulator’s run method passing it the stops and custList
________________________________________________________________________________________
Sample code Simulator
public class Simulator {
static final String FILE_PATH = "customer-data.txt";
int getStopsFromUser=0;
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
Scanner fileReader = null;
String filePath = "";
int numStops = 0;
ArrayList<Customer> customers = new ArrayList<Customer>();
while(true)
{
System.out.print("Enter number of stops the train has on its route (must be greater than 1): ");
numStops = keyboard.nextInt();
if(numStops < 1)
System.out.println("Invalid input, try again.");
else
break;
}
keyboard.nextLine();
while(true)
{
System.out.print(" Enter absolute path for data file or for default (C:/train/customer-data.txt) press Enter:");
filePath = keyboard.nextLine();
try {
if(filePath.equals(""))
fileReader = new Scanner(new File(FILE_PATH));
else fileReader = new Scanner(new File(filePath));
} catch (FileNotFoundException e) {
System.out.println("File not found, try again. ");
continue;
}
break;
}
while(fileReader.hasNextLine())
{
//assuming that the file contains customer information in one line separated by space
String line = fileReader.nextLine();
String tokens[] = line.split("\s+");
customers.add(new Customer(Integer.valueOf(tokens[0]), Integer.valueOf(tokens[1]), Integer.valueOf(tokens[2]), Integer.valueOf(tokens[3])));
}
fileReader.close();
Train train = new Train(numStops, customers);
train.simulate();
train.displayStops();
}
Explanation / Answer
Discrete-event simulation is a good option for problems that can be modeled as individual events that take place at specific times. Key activities are:
For example, you could simulate the activity in a parking garage as the entries and departures of a cars and the loss of customers who can't enter because the garage is full. This can be done with two model classes, a Car and the Garage, and three infrastructure classes, an Event class (described below), a Schedule to manage events, and a Monitor to accumulate data.
Here's a brief sketch of how it could work.
Event
An Event has a time, and represents calling a specific method on an object of a specific class.
Schedule
The Schedule keeps a queue of Events, ordered by Event time. The Schedule drives the overall simulation with a simple loop. As long as there are remaining Events (or until the Event that marks the end of the simulation run):
Car
The Car class holds the inter-arrival and length-of-stay statistics.
When a Car arrives, it:
logs its arrival with the Monitor,
consults the world clock, determines how long before the next Car should arrive, and posts that arrival Event on the Schedule.
asks the Garage whether it is full:
if full, the Car logs its departure as a lost customer with the Monitor.
if not full, the Car:
logs its entry with the Monitor,
tells the Garage it has entered (so that the Garage can decrease its available capacity),
determines how long it will stay, and posts its departure Event with the Schedule.
When a Car departs, it:
tells the Garage (so the Garage can increase available capacity), and
logs its departure with the Monitor.
Garage
The Garage keeps track of the Cars that are currently inside, and knows about its available capacity.
Monitor
The Monitor keeps track of the statistics in which you're interested: number of customers (successfully-arriving Cars), number of lost customers (who arrived when the lot was full), average length of stay, revenue (based on rate charged for parking), etc.
A simulation run
Start the simulation by putting two Events into the schedule:
the arrival of the first Car (modeled by instantiating a Car object and calling its "arrive" event) and
the end of the simulation.
Repeat the basic simulation loop until the end-of-simulation event is encountered. At that point, ask the Garage to report on its current occupants, and ask the Monitor to report the overall statistics for the session.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.