Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We want to write a program to simulate the behavior of a self-serve gas station.

ID: 3568771 • Letter: W

Question

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 station's 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.

______Car PACKAGE_________________

// DO NOT ADD NEW METHODS OR DATA FIELDS!

package PJ3;

class Car

{

private int carId;

private int serviceDuration;

private int arrivalTime;

Car()

{

// add statements

}

Car(int CID, int serviceTime, int arriveTime)

{

// add statements

}

int getServiceDuration()

{

// add statements

   return 0;

}

int getArrivalTime()

{

// add statements

   return 0;

}

int getCarId()

{

   return carId;

}

public String toString()

{

   return ""+carId+":"+serviceDuration+":"+arrivalTime;

}

public static void main(String[] args) {

// quick check!

   Car mycar = new Car(20,30,40);

   System.out.println("Car Info:"+mycar);

}

}

_______GasPump Package _____________

// DO NOT ADD NEW METHODS OR DATA FIELDS!

package PJ3;

class GasPump {

   // start time and end time of current interval

   private int startIntervalTime;

   private int endIntervalTime;

   // pump id and current car which is served by this gas pump

   private int pumpId;

   private Car currentCar;

   // for keeping statistical data

   private int totalFreeTime;

   private int totalBusyTime;

   private int totalCars;

   // Constructor

   GasPump()

   {

// add statements

   }

   // Constructor with gas pump id

   GasPump(int gasPumpId)

   {

// add statements

   }

   // accessor methods

   int getPumpId ()

   {

return pumpId;

   }

   Car getCurrentCar()

   {

// add statements

return null;

   }

   // need this to setup priority queue for Busy pumps

   int getEndIntervalTime()

   {

// return end time of busy interval

// add statements

return 0;

   }

   // State transition : FREE interval -> BUSY interval:

   void switchFreeToBusy (Car currentCar, int currentTime)

   {

   // Main goal : switch from free interval to busy interval

   //

   // end free interval, start busy interval

   // steps : update totalFreeTime

   //     set Busy interval - startIntervalTime , endIntervalTime, currentCar,

   //    update totalCars

// add statements

   }

   // State transition : BUSY interval -> FREE interval:

   Car switchBusyToFree ()

   {

      // Main goal : switch from busy interval to free interval

      //

   // end busy interval, start free interval

   // steps : update totalBusyTime

   //    set Free interval - startIntervalTime

   // return currentCar

// add statements

   return null;

   }

   // use this method at the end of simulation to update gas pump data in free and busy queues

   void setEndSimulationTime(int endsimulationtime, int intervalType)

   {

   // for end of simulation

   // set endIntervalTime to endsimulationtime,

   // for FREE interval, update totalFreeTime

   // for BUSY interval, update totalBusyTime

// add statements

   }

   // functions for printing statistics :

   void printStatistics ()

   {

   // print gasPump statistics, see project statement

   System.out.println(" GasPump ID : "+pumpId);

   System.out.println(" Total free time : "+totalFreeTime);

   System.out.println(" Total service time : "+totalBusyTime);

   System.out.println(" Total # of cars : "+totalCars);

   if (totalCars > 0)

      System.out.format(" Average service time : %.2f%n ",(totalBusyTime*1.0)/totalCars);

   }

   public String toString()

   {

return "GasPump:"+pumpId+":"+startIntervalTime+"-"+endIntervalTime+":Car:"+currentCar;

   }

   public static void main(String[] args) {

Car mycar = new Car(15,30,40);

   GasPump mypump = new GasPump(5);

mypump.switchFreeToBusy (mycar, 45);

System.out.println(mypump);

   }

};

_______ Gas Station package_______

package PJ3;

import java.util.*;

//--------------------------------------------------------------------------

//

// 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<GasPump>{

// overide compare() method

    public int compare(GasPump o1, GasPump o2) {

       return o1.getEndIntervalTime() - o2.getEndIntervalTime();

   }

}

// DO NOT ADD NEW METHODS OR DATA FIELDS

class GasStation {

  

// Private data fields:

  

// define one priority queue

private PriorityQueue <GasPump> busyGasPumpQ;

// define two FIFO queues

private Queue<Car> carQ;

private Queue<GasPump> freeGasPumpQ;

// define car queue size limit

private int carQSizeLimit;

// Constructor

public GasStation()

{

// add statements

}

// Constructor

public GasStation(int numGasPumps, int carQlimit, int startGasPumpID)

{

// use ArrayDeque to construct FIFO queue objects

// construct PriorityQueue object

    // overide compare() in Comparator to compare busy GasPump objects

   busyGasPumpQ= new PriorityQueue<GasPump>( numGasPumps,

                          new BusyGasPumpComparator());

// initialize carQlimit

// Construct GasPump objects and insert into FreeGasPumpQ

// add statements

}

public GasPump removeFreeGasPumpQ()

{

// remove and return a free gasPump

// Add statetments

return null;

}

public GasPump removeBusyGasPumpQ()

{

// remove and return a busy gasPump

// Add statetments

return null;

}

public Car removeCarQ()

{

// remove and return a car

// Add statetments

return null;

}

public void insertFreeGasPumpQ(GasPump gasPump)

{

      // insert a free gasPump

      // Add statetments

}

public void insertBusyGasPumpQ(GasPump gasPump)

{

// insert a busy gasPump

// Add statetments

}

public void insertCarQ(Car car)

{

// insert a car

// Add statetments

}

public boolean emptyFreeGasPumpQ()

{

// is freeGasPumpQ empty?

// Add statetments

return false;

}

public boolean emptyBusyGasPumpQ()

{

// is busyGasPumpQ empty?

// Add statetments

return false;

}

public boolean emptyCarQ()

{

// is carQ empty?

// Add statetments

return false;

}

public int numFreeGasPumps()

{

// get number of free gasPumps

// Add statetments

return 0;

}

public int numBusyGasPumps()

{

// get number of busy gasPumps

// Add statetments

return 0;

}

public int numWaitingCars()

{

// get number of cars

// Add statetments

return 0;

}

public GasPump getFrontBusyGasPumpQ()

{

// get front of busy gasPumps

// "retrieve" but not "remove"

// Add statetments

return null;

}

public boolean isCarQTooLong()

{

// is carQ too long?

// Add statetments

return false;

}

public void printStatistics()

{

   System.out.println(" # waiting cars : "+numWaitingCars());

   System.out.println(" # busy gas pumps : "+numBusyGasPumps());

   System.out.println(" # free gas pumps : "+numFreeGasPumps());

}

public static void main(String[] args) {

GasStation sc = new GasStation(4, 5, 1001);

Car c1 = new Car(1,18,10);

Car c2 = new Car(2,33,10);

Car c3 = new Car(3,21,10);

Car c4 = new Car(3,37,10);

   sc.insertCarQ(c1);

   sc.insertCarQ(c2);

   sc.insertCarQ(c3);

   System.out.println(""+sc.carQ);

   System.out.println("Remove car:"+sc.removeCarQ());

   System.out.println("Remove car:"+sc.removeCarQ());

   System.out.println("Remove car:"+sc.removeCarQ());

   System.out.println(""+sc.freeGasPumpQ);

   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);

p1.switchFreeToBusy (c1, 13);

p2.switchFreeToBusy (c2, 13);

p3.switchFreeToBusy (c3, 13);

p4.switchFreeToBusy (c4, 13);

   sc.insertBusyGasPumpQ(p1);

   sc.insertBusyGasPumpQ(p2);

   sc.insertBusyGasPumpQ(p3);

   sc.insertBusyGasPumpQ(p4);

   System.out.println(""+sc.busyGasPumpQ);

   p1=sc.removeBusyGasPumpQ();

   p2=sc.removeBusyGasPumpQ();

   p3=sc.removeBusyGasPumpQ();

   p4=sc.removeBusyGasPumpQ();

   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);

   }

};

_______ Gas Station Simpulation Package______

package PJ3;

import java.util.*;

import java.io.*;

// 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

// 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;

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 from user

// setup dataFile or dataRandom

// add statements

}

// 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?

       } else {

          System.out.println(" No new car!");

       }

       // Step 2: free busy pumps, add to free pumpQ

       // Step 3: get free pumps to serve waiting cars

   } // end simulation loop

   // clean-up

}

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 tellers

}

// *** main method to run simulation ****

public static void main(String[] args) {

      GasStationSimulation gas_station_simulation=new GasStationSimulation();

      gas_station_simulation.getUserParameters();

      gas_station_simulation.doSimulation();

      gas_station_simulation.printStatistics();

}

}

Explanation / Answer

Car class:-

package PJ3;

class Car {

        private int carId;

        private int serviceDuration;

        private int arrivalTime;

        Car() {

              

        }

        Car(int CID, int serviceTime, int arriveTime) {

               // add statements

               this.carId = CID;

               this.serviceDuration = serviceTime;

               this.arrivalTime = arriveTime;

        }

        int getServiceDuration() {

               // add statements

               return serviceDuration;

        }

        int getArrivalTime() {

               // add statements

               return arrivalTime;

        }

        int getCarId() {

               return carId;

        }

        public String toString() {

               return "" + carId + ":" + serviceDuration + ":" + arrivalTime;

        }

        public static void main(String[] args) {

               // quick check!

               Car mycar = new Car(20, 30, 40);

               System.out.println("Car Info:" + mycar);

        }

}

GasPump class:-

GasStation Class:-

GasStationSimulation class:-

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote