Question: Java program, help needed please, i will really appreciate your help and give a thumbs up Simulate a car sharing system in which car commuters
Java program, help needed please, i will really appreciate your help and give a thumbs up
Simulate a car sharing system in which car commuters pick up and drop-off passengers at designated stations. Assume that there are n such stations, one at every mile along a route. At each station, except the last station, there are randomly generated number of passengers, each of them have a desired target station. There are cars at each station except the last station. Each car has a target destination station. The car drive along the stations until it arrives at its destination station. For example, if a car is initially located at station 1 and has destination station 4, then it will drive along station 2 and 3 and finishes at station 4. Each driver picks up a maximum of three passengers (whose destination is either the same as cars destination or on the way to the cars destination), and drops them off where requested, and picks up more if possible. A driver gets paid per passenger per mile.
Complete classes Car, Passenger, Station, Simulation in your solution.
- Create a class Passenger with attributes String name and int destination.
- Create a custom constructor public Passenger(String name, int dest).
- Write down the getter method getDestination() to determine the destination of the passenger
- Write a toString() method to print the name and destination of the passenger.
- Create a class Car, which has attributes int idNo, int destination, int location, ArrayListPassenger>passengers, double faresCollected, int milesDriven, double FAREPERMILE =
1.0, and int MAXPASSENGERS = 3.
- Create a custom constructor to initialize idNo, location, and destination.
- Create getter methods getIdNo, getLocation, getDestination, getFares, getMilesDriven and getPassengers.
- Write getter methods public boolean hasArrived() to know whether the car has arrived its destination and public boolean hasRoom() to know whether the number of passengers in a car is within its maximum capacity.
- Write mutator method public void drive(), which drives to the next station. If the car is already at its destination, nothing happens. Note that you should also collect fares. Also print out Car X drives to station Y, as shown in sample output (see Appendix B).
- Write method public boolean dropoff(). This method drops off any passengers who have arrived, and return true or false depending on if someone is dropped. If someone is dropped, also prints Car X drops off Y at station Z. Now has W passengers, as shown in the sample output (see Appendix B).
- Note: for car sharing problem, Passenger get(index) method in Station class, should return the Passenger reference. The description should be "return the passenger at index 'index'
simulation class:
package carsharing;
import java.util.ArrayList; import java.util.Random; /** * Car Sharing Simulation * */ public class Simulation { private Random generator; private ArrayList
public void addCar(Car car) { cars.add(car); } public double getFares() { return faresCollected; } public int getMilesDriven() { return milesDriven; }
/** * Pick up passengers from a station. * @station the station to load passengers from. * @car the car that we want to load the passengers into. */ public void loadPassengers(Station station, Car car) { // This version picks up any passengers going to the // car's destination first, and then picks up any // other passengers only if there is room. int carDest = car.getDestination(); //Pick up people going to the destination first ("direct" passenger") Passenger p; //..... System.out.printf("Car %d loads direct passenger %s at station %d. Car now has %d passengers %s ", car.getIdNo(), p, station.getNumber(), car.getPassengers().size(), car.getPassengers());
//Now, anyone else going on the way ("ride passenger"). //..... System.out.printf("Car %d loads ride passenger %s at station %d. Car now has %d passengers %s ", car.getIdNo(), p, station.getNumber(), car.getPassengers().size(), car.getPassengers()); } /** * Drive all the cars to their destination. */ public void driveAllCars() { int count= 1; while (cars.size() > 0) { System.out.printf(" ------------ timestep %d------------ ", count++); for (int i = cars.size() - 1; i >= 0; i--) { Car car = cars.get(i); int passen = car.getPassengers().size(); loadPassengers(stations[car.getLocation() - 1+1], car); int passen2 = car.getPassengers().size(); if (passen2 == passen) System.out.println("Car " + car.getIdNo() + " has no one to load at station "+car.getLocation()); car.drive(); System.out.printf("Car %d arrives at station %d. ", car.getIdNo(), car.getLocation()); if (car.getLocation() != this.stations.length) System.out.printf("Waiting queue: %s ", stations[car.getLocation()].peopleWaitingList()); else System.out.printf(" "); boolean drop = car.dropOff(); if (!drop) System.out.println("Car " + car.getIdNo() + " has no one to drop off at station "+car.getLocation()); if (car.hasArrived()) { faresCollected += car.getFares(); milesDriven += car.getMilesDriven(); cars.remove(i); System.out.printf("%s finishes at destination station %d. Miles: %d. Fare %f ", car, car.getLocation(), car.getMilesDriven(), car.getFares()); } System.out.println(); } } //System.out.printf(" ------------ DONE------------ "); } /** * Load a scenario into the simulator. * @param numStations - the number of stations to add. */ public void loadRandomScenario(int numStations) { stations = new Station[numStations]; faresCollected = 0.0; // no revenue yet milesDriven = 0; // Visit each station; generate a station, cars and passengers int carID = 1000; for (int station = 0; station
for (int pass = 0; pass
simulationTester:
package carsharing;
import java.util.Random;
public class SimulationTester { /** * Runs the simulation n times. * Use hard-coded Random generators for testing. */ public static void main(String[] args) { final int N_RUNS = 1; final int N_STATIONS = 5; System.out.printf("A Commuter-car-sharing simulation: %d runs. ", N_RUNS); for (int i = 0; i



Appendix B A Commuter-car-sharing simulation: 1 runs. add passenger: D#0A->4 at station 0 add passenger: D#0B->4 at station 0 add passenger: P#0C->5 at station o add car: CaridNo=1000, location=0, destination=1, passengers=[]] at station 0 add passenger: P#1A->4 at station 1 add passenger: P#1B->5 at station 1 add passenger: P#1C->4 at station 1 add car: Car[idNo=1001, location=1, destination=3, passengers=[1] at station 1 add passenger: P#2A->3 at station 2 add passenger: P#2B->3 at station 2 add passenger: P#2C->4 at station 2 add passenger: P#2D->5 at station 2 add passenger: P#2E->4 at station 2 add car: Car[idNo=1002, location=2, destination=4, passengers=[1] at station 2 add passenger: P#3A->4 at station 3 add passenger: P#3B->4 at station 3 add passenger: P#3C->4 at station 3 add passenger: P#3D->5 at station 3 add passenger: P#3E->5 at station 3 add passenger: P#3F->4 at station 3 add car: Car[idNo=1003, location=3, destination=5, passengers=[]] at station 3 add passenger: P#4A->5 at station 4 add passenger: P#4B->5 at station 4 add passenger: P#4C->5 at station 4 add passenger: P#4D->5 at station 4 add passenger: P#4E->5 at station 4 add car: Car[idNo=1004, location=4, destination=5, passengers=[]] at station 4 --- timestep 1---- Car 1004 loads direct passenger P#4A->5 at station 4. Car now has 1 passengers [P#4A->5] Car 1004 loads direct passenger P#4B->5 at station 4. Car now has 2 passengers [P#4A->5, P#4B->5] Car 1004 loads direct passenger P#4C->5 at station 4. Car now has 3 passengers [P#4A->5, P#4B->5, P#4C->5] Car 1004 drives to station 5 Car 1004 arrives at station 5. Car 1004 drops off P#4C->5 at station 5. Car now has 2 passengers Car 1004 drops off P#4B->5 at station 5. Car now has 1 passengers Car 1004 drops off P#4A->5 at station 5. Car now has 0 passengers Car[idNo=1004, location=5, destination=5, passengers=[]] finishes at destination station 5. Miles: 1. Fare 3.000000 Car 1003 loads direct passenger P#3D->5 at station 3. Car now has 1 passengers [P#3D->5] Car 1003 loads direct passenger P#3E->5 at station 3. Car now has 2 passengers [P#3D->5, P#3E->5] Car 1003 loads ride passenger P#3A->4 at station 3. Car now has 3 passengers [P#3D->5, P#3E->5, P#3A->4] Car 1003 drives to station 4 Car 1003 arrives at station 4. Waiting queue: [P#4D->5, P#4E->5] Car 1003 drops off P#3A->4 at station 4. Car now has 2 passengers Car 1002 loads direct passenger P#2C->4 at station 2. Car now has 1 passengers [P#2C->4] Car 1002 loads direct passenger P#2E->4 at station 2. Car now has 2 passengers [P#2C->4, P#2E->4] Car 1002 loads ride passenger P#2A->3 at station 2. Car now has 3 passengers [P#2C->4, P#2E->4, P#2A->3] Car 1002 drives to station 3 Car 1002 arrives at station 3. Waiting queue: [P#3B->4, P#3C->4, D#3F->4] Car 1002 drops off P#2A->3 at station 3. Car now has 2 passengers Car 1001 has no one to load at station 1 Car 1001 drives to station 2 Car 1001 arrives at station 2. Waiting queue: [P#2B->3, P#2D->5] Car 1001 has no one to drop off at station 2 Car 1000 has no one to load at station 0 Car 1000 drives to station 1 Car 1000 arrives at station 1. Waiting queue: [P#1A->4, P#1B->5, P#1C->4] Car 1000 has no one to drop off at station 1 Car[idNo=1000, location=1, destination=1, passengers=[1] finishes at destination station 1. Miles: 1. Fare 0.000000 ------------ timestep2------------ Car 1003 loads direct passenger P#4D->5 at station 4. Car now has 3 passengers [P#3D->5, P#3E->5, P#4D->5] Car 1003 drives to station 5 Car 1003 arrives at station 5. Car 1003 drops off P#4D->5 at station 5. Car now has 2 passengers Car 1003 drops off P#3E->5 at station 5. Car now has 1 passengers Car 1003 drops off P#3D->5 at station 5. Car now has 0 passengers Car[idNo=1003, location=5, destination=5, passengers=[]] finishes at destination station 5. Miles: 2. Fare 6.000000 Car 1002 loads direct passenger P#3B->4 at station 3. Car now has 3 passengers [P#2C->4, P#2E->4, P#3B->4] Car 1002 drives to station 4 Car 1002 arrives at station 4. Waiting queue: [P#4E->5] Car 1002 drops off P#3B->4 at station 4. Car now has 2 passengers Car 1002 drops off P#2E->4 at station 4. Car now has 1 passengers Car 1002 drops off P#2C->4 at station 4. Car now has 0 passengers Car[idNo=1002, location=4, destination=4, passengers=[]] finishes at destination station 4. Miles: 2. Fare 6.000000 Car 1001 loads direct passenger P#2B->3 at station 2. Car now has 1 passengers [P#2B->3] Car 1001 drives to station 3 Car 1001 arrives at station 3. Waiting queue: [P#3C->4, P#3F->4] Car 1001 drops off P#2B->3 at station 3. Car now has 0 passengers Car[idNo=1001, location=3, destination=3, passengers=[l] finishes at destination station 3. Miles: 2. Fare 1.000000 ------------ Commuter-car-sharing simulation done ------ Appendix B A Commuter-car-sharing simulation: 1 runs. add passenger: D#0A->4 at station 0 add passenger: D#0B->4 at station 0 add passenger: P#0C->5 at station o add car: CaridNo=1000, location=0, destination=1, passengers=[]] at station 0 add passenger: P#1A->4 at station 1 add passenger: P#1B->5 at station 1 add passenger: P#1C->4 at station 1 add car: Car[idNo=1001, location=1, destination=3, passengers=[1] at station 1 add passenger: P#2A->3 at station 2 add passenger: P#2B->3 at station 2 add passenger: P#2C->4 at station 2 add passenger: P#2D->5 at station 2 add passenger: P#2E->4 at station 2 add car: Car[idNo=1002, location=2, destination=4, passengers=[1] at station 2 add passenger: P#3A->4 at station 3 add passenger: P#3B->4 at station 3 add passenger: P#3C->4 at station 3 add passenger: P#3D->5 at station 3 add passenger: P#3E->5 at station 3 add passenger: P#3F->4 at station 3 add car: Car[idNo=1003, location=3, destination=5, passengers=[]] at station 3 add passenger: P#4A->5 at station 4 add passenger: P#4B->5 at station 4 add passenger: P#4C->5 at station 4 add passenger: P#4D->5 at station 4 add passenger: P#4E->5 at station 4 add car: Car[idNo=1004, location=4, destination=5, passengers=[]] at station 4 --- timestep 1---- Car 1004 loads direct passenger P#4A->5 at station 4. Car now has 1 passengers [P#4A->5] Car 1004 loads direct passenger P#4B->5 at station 4. Car now has 2 passengers [P#4A->5, P#4B->5] Car 1004 loads direct passenger P#4C->5 at station 4. Car now has 3 passengers [P#4A->5, P#4B->5, P#4C->5] Car 1004 drives to station 5 Car 1004 arrives at station 5. Car 1004 drops off P#4C->5 at station 5. Car now has 2 passengers Car 1004 drops off P#4B->5 at station 5. Car now has 1 passengers Car 1004 drops off P#4A->5 at station 5. Car now has 0 passengers Car[idNo=1004, location=5, destination=5, passengers=[]] finishes at destination station 5. Miles: 1. Fare 3.000000 Car 1003 loads direct passenger P#3D->5 at station 3. Car now has 1 passengers [P#3D->5] Car 1003 loads direct passenger P#3E->5 at station 3. Car now has 2 passengers [P#3D->5, P#3E->5] Car 1003 loads ride passenger P#3A->4 at station 3. Car now has 3 passengers [P#3D->5, P#3E->5, P#3A->4] Car 1003 drives to station 4 Car 1003 arrives at station 4. Waiting queue: [P#4D->5, P#4E->5] Car 1003 drops off P#3A->4 at station 4. Car now has 2 passengers Car 1002 loads direct passenger P#2C->4 at station 2. Car now has 1 passengers [P#2C->4] Car 1002 loads direct passenger P#2E->4 at station 2. Car now has 2 passengers [P#2C->4, P#2E->4] Car 1002 loads ride passenger P#2A->3 at station 2. Car now has 3 passengers [P#2C->4, P#2E->4, P#2A->3] Car 1002 drives to station 3 Car 1002 arrives at station 3. Waiting queue: [P#3B->4, P#3C->4, D#3F->4] Car 1002 drops off P#2A->3 at station 3. Car now has 2 passengers Car 1001 has no one to load at station 1 Car 1001 drives to station 2 Car 1001 arrives at station 2. Waiting queue: [P#2B->3, P#2D->5] Car 1001 has no one to drop off at station 2 Car 1000 has no one to load at station 0 Car 1000 drives to station 1 Car 1000 arrives at station 1. Waiting queue: [P#1A->4, P#1B->5, P#1C->4] Car 1000 has no one to drop off at station 1 Car[idNo=1000, location=1, destination=1, passengers=[1] finishes at destination station 1. Miles: 1. Fare 0.000000 ------------ timestep2------------ Car 1003 loads direct passenger P#4D->5 at station 4. Car now has 3 passengers [P#3D->5, P#3E->5, P#4D->5] Car 1003 drives to station 5 Car 1003 arrives at station 5. Car 1003 drops off P#4D->5 at station 5. Car now has 2 passengers Car 1003 drops off P#3E->5 at station 5. Car now has 1 passengers Car 1003 drops off P#3D->5 at station 5. Car now has 0 passengers Car[idNo=1003, location=5, destination=5, passengers=[]] finishes at destination station 5. Miles: 2. Fare 6.000000 Car 1002 loads direct passenger P#3B->4 at station 3. Car now has 3 passengers [P#2C->4, P#2E->4, P#3B->4] Car 1002 drives to station 4 Car 1002 arrives at station 4. Waiting queue: [P#4E->5] Car 1002 drops off P#3B->4 at station 4. Car now has 2 passengers Car 1002 drops off P#2E->4 at station 4. Car now has 1 passengers Car 1002 drops off P#2C->4 at station 4. Car now has 0 passengers Car[idNo=1002, location=4, destination=4, passengers=[]] finishes at destination station 4. Miles: 2. Fare 6.000000 Car 1001 loads direct passenger P#2B->3 at station 2. Car now has 1 passengers [P#2B->3] Car 1001 drives to station 3 Car 1001 arrives at station 3. Waiting queue: [P#3C->4, P#3F->4] Car 1001 drops off P#2B->3 at station 3. Car now has 0 passengers Car[idNo=1001, location=3, destination=3, passengers=[l] finishes at destination station 3. Miles: 2. Fare 1.000000 ------------ Commuter-car-sharing simulation done
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
