Question
1. Introduction We want to write a program to simulate the behavior of a self-serve gas station. The goal of this simulation is to collect
1. Introduction
We want to write a program to simulate the behavior of a self-serve gas station. The goal of this simulation is to collect statistical information of the gas stations cars and gas pumps. A gas station consists of several pumps and a car waiting line (i.e. a car queue). In each time unit, at most one new car arrives at the gas station. If the car waiting line is too long, the car leaves without getting gas; otherwise, the car gets into the waiting line. If all pumps are busy, cars in the waiting lines must wait for a gas pump. If a pump is free and cars are waiting, the first car in the waiting line gets the pump and begins pumping gas immediately. When a car finishes using a pump, the car departs and the pump becomes free. We will run the simulation through many units of time. At the end of each time unit, your program should print out a "snap-shot" of queues, cars and pumps. At the end of the program, your program should print out statistics and conclude simulation.
2. Assumptions and Requirements
2.1. Assumptions
- At most one car arrival per time unit - All numbers are positive integer numbers (>=0), except average values should be displayed to two decimal places - No time lost in between events: a car arriving and entering waiting line a car arriving and leaving without pumping gas a car completing service and departing a car leaving the waiting line, advancing to a pump and beginning service
2.2. The limits of simulations parameters
- Maximum number of pumps 10 - Maximum simulation length 10000 - Maximum service duration 500 - Maximum car queue limit 50 - Probability of a new car 1% - 100%
2.3. Input parameters and car (random/file) data
- The following data are read at the beginning of the simulation:
int numGasPumps; // number of gas pumps. int simulationTime; // time to run simulation int carQSizeLimit; // car queue size limit int chancesOfArrival; // probability of a new car (1 - 100) int maxDuration; // maximum service (or pumping gas) duration per car int dataSource; // data source: from file or random
- Sample input layout :
$ java PJ3.GasStationSimulation
*** Get Simulation Parameters ***
Enter simulation time (positive integer) : 10 Enter the number of gas pumps : 3 Enter maximum service time of cars : 5 Enter chances (0% < & <= 100%) of new car : 75 Enter car queue size limit : 2 Enter 1/0 to get data from file/rand() : 1 <-- see more details below Enter filename : DataFile <-- for input 1 above
- In each time unit of simulation, your program will need two positive integers numbers to compute (i) boolean anyNewArrival & (ii) int serviceDuration. A user should have two options (1 or 0) to specify the source of those numbers:
For user input 1, numbers are read from a file. A filename should be provided at the beginning of simulation. Each line in a datafile should contain two positive numbers (> 0). A datafile should contain sufficient data for simulationTime. In each time unit, anyNewArrival & serviceDuration are computed in getCarData() as follows :
read data1 and data2 from the file; anyNewArrival = (((data1%100)+1)<= chancesOfArrival); serviceDuration= (data2%maxDuration)+1;
For user input 0, numbers are generated by method nextInt() in a Random object, dataRandom, which should be constructed at the beginning of simulation. In each time unit, anyNewArrival & serviceDuration are computed in getCarData() as follows :
anyNewArrival = ((dataRandom.nextInt(100)+1) <= chancesOfArrival); serviceDuration = dataRandom.nextInt(maxDuration)+1;
2.4. Input/Output information
- At the end of each time unit, you program should print out "snap-shot" of simulation. At the end of simulation, you program need to print out end of simulation report
- Sample run is provided at the end of Readme file.
2.5. Data structures and simulation algorithm
- I have defined package PJ3 with the Car, GasPump and GasStation classes, you need to implement their methods. I also provide an outline of a simulation program GasStationSimulation.java.
3. Compile and run program
- You need to download PJ3.zip file (with sample datafie) from ilearn.
- Compile programs (you are in directory containing Readme file):
javac PJ3/*.java
- Run programs (you are in directory containing Readme file):
// Run simulation java PJ3.GasStationSimulation
4. Due Date
- 11:59:59PM, Sunday, November 26, 2017 - To submit your project, you need to zip all source files (*.java) into PJ3.zip and upload PJ3.zip file to ilearn.
===================================================================================== SAMPLE RUN ===================================================================================== $ ls DataFile PJ3 Readme
$ javac PJ3/*.java
$ java PJ3.Car Car Info:CarID=1:ServiceDuration=30:ArrivalTime=40
$ java PJ3.GasPump CarID=1:ServiceDuration=5:ArrivalTime=15 GasPumpID:5:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null
Start service car: GasPumpID:5:startIntervalTime=20:endIntervalTime=25:total free time=20:total service time=0:Car:CarID=1:ServiceDuration=5:ArrivalTime=15
End service car: GasPumpID:5:startIntervalTime=25:endIntervalTime=25:total free time=20:total service time=5:Car:CarID=1:ServiceDuration=5:ArrivalTime=15
Gas pump statistics data: GasPump ID : 5 Total free time : 20 Total service time : 5 Total # of cars : 1 Average service time : 5.00
$ java PJ3.GasStation [CarID=1:ServiceDuration=18:ArrivalTime=10, CarID=2:ServiceDuration=33:ArrivalTime=11, CarID=3:ServiceDuration=21:ArrivalTime=12, CarID=3:ServiceDuration=37:ArrivalTime=13] =============================================== Remove car:CarID=1:ServiceDuration=18:ArrivalTime=10 Remove car:CarID=2:ServiceDuration=33:ArrivalTime=11 Remove car:CarID=3:ServiceDuration=21:ArrivalTime=12 Remove car:CarID=3:ServiceDuration=37:ArrivalTime=13 =============================================== freeGasPumpQ:[GasPumpID:1:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null, GasPumpID:2:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null, GasPumpID:3:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null, GasPumpID:4:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null] =============================================== Remove free gas pump:GasPumpID:1:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null Remove free gas pump:GasPumpID:2:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null Remove free gas pump:GasPumpID:3:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null Remove free gas pump:GasPumpID:4:startIntervalTime=0:endIntervalTime=0:total free time=0:total service time=0:Car:null =============================================== freeGasPumpQ:[] busyGasPumpQ:[] =============================================== Assign cars to gas pumps =============================================== Insert busy gas pumps to busy Q busyGasPumpQ:[GasPumpID:1:startIntervalTime=13:endIntervalTime=31:total free time=13:total service time=0:Car:CarID=1:ServiceDuration=18:ArrivalTime=10, GasPumpID:2:startIntervalTime=13:endIntervalTime=46:total free time=13:total service time=0:Car:CarID=2:ServiceDuration=33:ArrivalTime=11, GasPumpID:3:startIntervalTime=13:endIntervalTime=34:total free time=13:total service time=0:Car:CarID=3:ServiceDuration=21:ArrivalTime=12, GasPumpID:4:startIntervalTime=13:endIntervalTime=50:total free time=13:total service time=0:Car:CarID=3:ServiceDuration=37:ArrivalTime=13] =============================================== Remove busy gas pumps from busy Q Remove busy gas pump:GasPumpID:1:startIntervalTime=31:endIntervalTime=31:total free time=13:total service time=18:Car:CarID=1:ServiceDuration=18:ArrivalTime=10 Remove busy gas pump:GasPumpID:3:startIntervalTime=34:endIntervalTime=34:total free time=13:total service time=21:Car:CarID=3:ServiceDuration=21:ArrivalTime=12 Remove busy gas pump:GasPumpID:2:startIntervalTime=46:endIntervalTime=46:total free time=13:total service time=33:Car:CarID=2:ServiceDuration=33:ArrivalTime=11 Remove busy gas pump:GasPumpID:4:startIntervalTime=50:endIntervalTime=50:total free time=13:total service time=37:Car:CarID=3:ServiceDuration=37:ArrivalTime=13
$ java PJ3.GasStationSimulation
*** Get Simulation Parameters ***
Enter simulation time (positive integer) : 10 Enter the number of gas pumps : 3 Enter chances (0% < & <= 100%) of new car : 75 Enter maximum service time of cars : 5 Enter car queue size limit : 2 Enter 0/1 to get data from Random/file : 1 Enter filename : DataFile
*** Start Simulation ***
Gas Pump #1 to #3 are ready...
--------------------------------------------- Time : 0 car #1 arrives with duration 5 units car #1 wait in the car queue car #1 gets a pump pump #1 starts serving car #1 for 5 units --------------------------------------------- Time : 1 car #2 arrives with duration 2 units car #2 wait in the car queue car #2 gets a pump pump #2 starts serving car #2 for 2 units --------------------------------------------- Time : 2 car #3 arrives with duration 5 units car #3 wait in the car queue car #3 gets a pump pump #3 starts serving car #3 for 5 units --------------------------------------------- Time : 3 No new car! car #2 is done pump #2 is free --------------------------------------------- Time : 4 car #4 arrives with duration 3 units car #4 wait in the car queue car #4 gets a pump pump #2 starts serving car #4 for 3 units --------------------------------------------- Time : 5 No new car! car #1 is done pump #1 is free --------------------------------------------- Time : 6 car #5 arrives with duration 3 units car #5 wait in the car queue car #5 gets a pump pump #1 starts serving car #5 for 3 units --------------------------------------------- Time : 7 car #6 arrives with duration 5 units car #6 wait in the car queue car #4 is done pump #2 is free car #3 is done pump #3 is free car #6 gets a pump pump #2 starts serving car #6 for 5 units --------------------------------------------- Time : 8 No new car! --------------------------------------------- Time : 9 No new car! car #5 is done pump #1 is free
============================================
End of simulation report
# total arrival cars : 6 # cars gone-away : 0 # cars served : 6
*** Current GasPumps Info. ***
# waiting cars : 0 # busy gas pumps : 1 # free gas pumps : 2
Total waiting time : 0 Average waiting time : 0.00
Busy GasPumps Info. :
GasPump ID : 2 Total free time : 2 Total service time : 8 Total # of cars : 3 Average service time : 2.67
Free GasPumps Info. :
GasPump ID : 3 Total free time : 5 Total service time : 5 Total # of cars : 1 Average service time : 5.00
GasPump ID : 1 Total free time : 2 Total service time : 8 Total # of cars : 2 Average service time : 4.00
Here's what I have so far
package PJ3;
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner;
class Car { private int carId; private int serviceDuration; private int arrivalTime;
// default constructor Car() { carId = -1; serviceDuration = -1; arrivalTime = 0; }
// constructor to set carId, serviceDuration, // and arrivalTime Car(int CID, int serviceTime, int arriveTime) { carId = CID; serviceDuration = serviceTime; arrivalTime = arriveTime; }
int getServiceDuration() { return serviceDuration; }
int getArrivalTime() { return arrivalTime; }
int getCarId() { return carId; }
public String toString() { return "CarID="+carId+":ServiceDuration="+serviceDuration+":ArrivalTime="+arrivalTime;
}
public static void main(String[] args) { // quick check! Car mycar = new Car(1,30,40); System.out.println("Car Info:"+mycar); } } // DO NOT ADD NEW METHODS OR DATA FIELDS! // DO NOT MODIFY METHODS OR DATA FIELDS!
class GasPump {
// pump id and current car which is served by this gas pump private int pumpId; private Car currentCar;
// start time and end time of current interval private int startIntervalTime; private int endIntervalTime;
// for keeping statistical data private int totalFreeTime; private int totalBusyTime; private int totalCars;
// Constructor GasPump() { this(-1); }
// Constructor with gas pump id GasPump(int gasPumpId) { // add statements }
//-------------------------------- // accessor methods //--------------------------------
int getPumpId () { return pumpId; }
Car getCurrentCar() { return currentCar; }
int getEndIntervalTime() { return endIntervalTime; }
//--------------------------------- // GasPump State Transition methods //---------------------------------
// State transition : FREE interval -> BUSY interval: void switchFreeToBusy (Car currentCar, int currentTime) { // Main goal : switch from free interval to busy interval // // steps : end Free interval - set endIntervalTime, update totalFreeTime // start Busy interval - set startIntervalTime , endIntervalTime, currentCar, // update totalCars
// add statements }
// State transition : BUSY interval -> FREE interval: Car switchBusyToFree () { // Main goal : switch from busy interval to free interval // // steps : end Busy interval - update totalBusyTime // start Free interval - set startIntervalTime // return currentCar (just done with service)
// add statements return null; }
//------------------------------------------------------------------- // Update totalBusyTime and totalFreeTime at the end of simulation // // use these method at the end of simulation to update gas pump // totalFreeTime or totalBusyTime data in free and busy queues //------------------------------------------------------------------
void updateTotalFreeTimeEndSimulationTime(int endsimulationtime) { // add statements }
void updateTotalBusyTimeEndSimulationTime(int endsimulationtime) { // add statements }
//-------------------------------- // Print statistical data //-------------------------------- void printStatistics () { // print gasPump statistics, see project statement
System.out.println("\t\tGasPump ID : "+pumpId); System.out.println("\t\tTotal free time : "+totalFreeTime); System.out.println("\t\tTotal service time : "+totalBusyTime); System.out.println("\t\tTotal # of cars : "+totalCars); if (totalCars > 0) System.out.format("\t\tAverage service time : %.2f%n ",(totalBusyTime*1.0)/totalCars); }
public String toString() { return "GasPumpID:"+pumpId+ ":startIntervalTime="+startIntervalTime+ ":endIntervalTime="+endIntervalTime+ ":total free time="+totalFreeTime+ ":total service time="+totalBusyTime+ ":Car:"+currentCar; }
public static void main(String[] args) { Car mycar = new Car(1,5,15); System.out.println(mycar); GasPump mypump = new GasPump(5); System.out.println(mypump); System.out.println(" Start service car:"); mypump.switchFreeToBusy (mycar, 20); System.out.println(mypump); System.out.println(" End service car:"); mypump.switchBusyToFree(); System.out.println(mypump); System.out.println(" Gas pump statistics data:"); mypump.printStatistics();
}
}; // DO NOT ADD NEW METHODS OR NEW DATA FIELDS! // DO NOT MODIFY METHODS OR NEW DATA FIELDS!
//-------------------------------------------------------------------------- // // Define simulation queues in a gas station. Queues hold references to Car & // GasPump objects // // Car (FIFO) queue is used to hold waiting cars. If the queue is too long // (i.e. > carQSizeLimit), car goes away without entering car queue // // There are several gas pumps in a gas station. Use PriorityQueue to // hold BUSY gas pumps and FIFO queue to hold FREE gas pumps, // i.e. a pump that is FREE for the longest time should start be used first. // // To handle gasPump in PriorityQueue, we need to define comparator // for comparing 2 gasPump objects. Here is a constructor from Java API: // // PriorityQueue(int initialCapacity, Comparator super E> comparator) // // For priority queue, the default compare function is "natural ordering" // i.e. for numbers, minimum value is returned first // // User can define own comparator class for PriorityQueue. // For GasPump objects, we like to have smallest end busy interval time first. // // The following class define compare() for two busy gas pumps :
class BusyGasPumpComparator implements Comparator
class GasStation {
// Private data fields: // define one priority queue private PriorityQueue
// define two FIFO queues private Queue
// define car queue size limit private int carQSizeLimit;
// Constructor public GasStation() { // add statements }
// Constructor public GasStation(int numGasPumps, int carQlimit) { // add additional statements // use ArrayDeque to construct FIFO queue objects
// construct PriorityQueue object // overide compare() in Comparator to compare busy GasPump objects busyGasPumpQ= new PriorityQueue
// initialize carQlimit
// Construct GasPump objects and insert into FreeGasPumpQ // assign gas pump ID from 1, 2,..., numGasPumps }
// ------------------------------------------------- // freeGasPumpQ methods: remove, insert, empty, size // -------------------------------------------------
public GasPump removeFreeGasPumpQ() { // remove and return a free gasPump // Add statetments return null; }
public void insertFreeGasPumpQ(GasPump gasPump) { // insert a free gasPump // Add statetments }
public boolean emptyFreeGasPumpQ() { // is freeGasPumpQ empty? // Add statetments return false; }
public int numFreeGasPumps() { // get number of free gasPumps // Add statetments return 0; }
// -------------------------------------------------------- // busyGasPumpQ methods: remove, insert, empty, size, peek // --------------------------------------------------------
public GasPump removeBusyGasPumpQ() { // remove and return a busy gasPump // Add statetments return null; }
public void insertBusyGasPumpQ(GasPump gasPump) { // insert a busy gasPump // Add statetments }
public boolean emptyBusyGasPumpQ() { // is busyGasPumpQ empty? // Add statetments return busyGasPumpQ.isEmpty(); }
public int numBusyGasPumps() { // get number of busy gasPumps // Add statetments return 0; }
public GasPump getFrontBusyGasPumpQ() { // get front of busy gasPumps // "retrieve" but not "remove" // Add statetments return null; }
// -------------------------------------------------------- // carQ methods: remove, insert, empty, size // and check isCarQTooLong() // --------------------------------------------------------
public Car removeCarQ() { // remove and return a car // Add statetments return null; }
public void insertCarQ(Car car) { // insert a car // Add statetments }
public boolean emptyCarQ() { // is carQ empty? // Add statetments return false; }
public int numWaitingCars() { // get number of cars // Add statetments return 0; }
public boolean isCarQTooLong() { // is carQ too long? // Add statetments return false; }
public void printStatistics() { System.out.println("\t# waiting cars : "+numWaitingCars()); System.out.println("\t# busy gas pumps : "+numBusyGasPumps()); System.out.println("\t# free gas pumps : "+numFreeGasPumps()); }
public static void main(String[] args) {
// quick check GasStation sc = new GasStation(4, 5); Car c1 = new Car(1,18,10); Car c2 = new Car(2,33,11); Car c3 = new Car(3,21,12); Car c4 = new Car(3,37,13);
// insert cars into carQ sc.insertCarQ(c1); sc.insertCarQ(c2); sc.insertCarQ(c3); sc.insertCarQ(c4); System.out.println(""+sc.carQ); System.out.println("==============================================="); System.out.println("Remove car:"+sc.removeCarQ()); System.out.println("Remove car:"+sc.removeCarQ()); System.out.println("Remove car:"+sc.removeCarQ()); System.out.println("Remove car:"+sc.removeCarQ()); System.out.println("===============================================");
// remove gasPumps from freeGasPumpQ System.out.println("freeGasPumpQ:"+sc.freeGasPumpQ); System.out.println("==============================================="); GasPump p1=sc.removeFreeGasPumpQ(); GasPump p2=sc.removeFreeGasPumpQ(); GasPump p3=sc.removeFreeGasPumpQ(); GasPump p4=sc.removeFreeGasPumpQ(); System.out.println("Remove free gas pump:"+p1); System.out.println("Remove free gas pump:"+p2); System.out.println("Remove free gas pump:"+p3); System.out.println("Remove free gas pump:"+p4); System.out.println("==============================================="); System.out.println("freeGasPumpQ:"+sc.freeGasPumpQ); System.out.println("busyGasPumpQ:"+sc.busyGasPumpQ); System.out.println("===============================================");
// insert car to gas pumps p1.switchFreeToBusy (c1, 13); p2.switchFreeToBusy (c2, 13); p3.switchFreeToBusy (c3, 13); p4.switchFreeToBusy (c4, 13); System.out.println("Assign cars to gas pumps");
// insert gas pumps into busyGasPumpQ System.out.println("==============================================="); System.out.println("Insert busy gas pumps to busy Q"); sc.insertBusyGasPumpQ(p1); sc.insertBusyGasPumpQ(p2); sc.insertBusyGasPumpQ(p3); sc.insertBusyGasPumpQ(p4); System.out.println("busyGasPumpQ:"+sc.busyGasPumpQ); System.out.println("===============================================");
// remove gas pumps from busyGasPumpQ System.out.println("Remove busy gas pumps from busy Q"); p1=sc.removeBusyGasPumpQ(); p2=sc.removeBusyGasPumpQ(); p3=sc.removeBusyGasPumpQ(); p4=sc.removeBusyGasPumpQ();
p1.switchBusyToFree(); p2.switchBusyToFree(); p3.switchBusyToFree(); p4.switchBusyToFree(); System.out.println("Remove busy gas pump:"+p1); System.out.println("Remove busy gas pump:"+p2); System.out.println("Remove busy gas pump:"+p3); System.out.println("Remove busy gas pump:"+p4);
}
}; // You may add new functions or data in this class // You may modify any functions or data members here // You must use Car, GasPump and GasStation classes // to implement your simulator
class GasStationSimulation {
// input parameters private int numGasPumps, carQSizeLimit; private int simulationTime, dataSource; private int chancesOfArrival, maxDuration;
// statistical data private int numGoAway, numServed, totalWaitingTime;
// internal data private int carIdCounter; // car ID counter private GasStation gasStationObj; // Gas station object private Scanner dataFile; // get car data from file private Random dataRandom; // get car data using random function
// most recent car arrival info, see getCarData() private boolean anyNewArrival; private int serviceDuration;
// initialize data fields private GasStationSimulation() { // add statements }
private void getUserParameters() { // read input parameters // setup dataFile or dataRandom // add statements } // Refer to step 1 in doSimulation // this method is called for each unit simulation time private void getCarData() { // get next car data : from file or random number generator // set anyNewArrival and serviceDuration // add statements }
private void doSimulation() { // add statements
// Initialize GasStation
// Time driver simulation loop for (int currentTime = 0; currentTime < simulationTime; currentTime++) {
// Step 1: any new car enters the gas station? getCarData();
if (anyNewArrival) {
// Step 1.1: setup car data // Step 1.2: check car waiting queue too long? // if it is too long, update numGoaway // else enter car queue
} else { System.out.println("\tNo new car!"); }
// Step 2: free busy pumps that are done at currentTime, add to free pumpQ // Step 3: get free pumps to serve waiting cars
} // end simulation loop
// clean-up - close scanner }
private void printStatistics() { // add statements into this method! // print out simulation results // see the given example in project statement // you need to display all free and busy gas pumps
// need to free up all cars in Car queue to get extra waiting time. // need to free up all gas pumps in free/busy queues to get extra free & busy time }
// *** main method to run simulation **** public static void main(String[] args) { GasStationSimulation gas_station_simulator=new GasStationSimulation(); gas_station_simulator.getUserParameters(); gas_station_simulator.doSimulation(); gas_station_simulator.printStatistics(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started