Question: Review the comments in the code for the following interfaces: Position Driver Vehicle RideRequest CompletedRide Create the following classes which implement each of the interfaces
Review the comments in the code for the following interfaces:
- Position
- Driver
- Vehicle
- RideRequest
- CompletedRide
Create the following classes which implement each of the interfaces above:
- PositionImpl
- The constructor for this class should have the following form: ` public PositionImpl(int x, int y) `
- DriverImpl
- The constructor for this class should have the following form: ` public DriverImpl(String first, String last, int id, Vehicle vehicle) `
- VehicleImpl
- The constructor for this class should have the following form: ` public VehicleImpl(String make, String model, String plate, Position position) `
- The mileage of a new vehicle should be initialized to 0.
- RideRequestImpl
- The constructor for this class should have the following form: ` public RideRequestImpl(Position clientPosition, Position destination) `
- CompletedRideImpl
- The constructor for this class should have the following form: ` public CompletedRideImpl(RideRequest request, Driver driver) `
A few notes:
- For now, ignore the code in the interface Dispatcher and the classes Simulation and RoundRobinDispatcher
- The constructors and methods of your implementations should appropriately check all values for validity. In particular, any reference types (i.e., String, Position, Driver, etc.) provided as parameters should be checked against null. If a reference is null, throw a runtime exception like this: ` throw new RuntimeException("An explanation of why"); `
- Any interface methods that can be implemented as default methods should be. This will require you to change the interfaces themselves to include the default method definition.
- You should get in the habit of writing testing code. For this assignment, I suggest writing small test programs that exercises each of the implementation and confirms that things are working as you expect. Get in the habit of putting your testing code in a separate package than the code it is testing. I've included an example in the package a4test called PositionTest for testing PositionImpl.
Once you have correct implementations for all of the classes described above, you should be able to run the program SimulationTest in the a4test package. This test uses the code in the class Simulation to simulate a series of random ride requests given a specific dispatching strategy. SimulationTest will print the profit associated with each ride in the simulation as well as the total overall profit. If all is well, you should be able to run this program and get the following output: ` Ride 0: -10.60 Ride 1: 36.70 Ride 2: 151.10 Ride 3: 22.50 Ride 4: 81.60 Ride 5: 70.40 Ride 6: 51.10 Ride 7: 50.00 Ride 8: -13.60 Ride 9: 49.70 Total Profit: 488.90 `
Position interface:
package a4;
/*
* Position
* Represents an integer (x,y) position on a grid.
* getX()
* Retrieves x coordinate of the position
* getY()
* Retrieves y coordinate of the position
* getManhattanDistanceTo(Position p)
* Calculates the "Manhattan" distance between two positions.
* The Manhattan distance is simply the absolute difference in x positions
* summed with the absolute difference in y positions.
*/
public interface Position {
int getX();
int getY();
int getManhattanDistanceTo(Position p);
}
*
* Driver
* Represents a driver associated with a vehicle.
*
* getFirstName()
* Retrieves first name of driver.
*
* getLastName()
* Retrieves last name of driver.
*
* getFullName()
* Retrieves full name of driver. This should be the first name followed
* a single space followed by the last name.
*
* getID()
* Retrieves the ID number of the driver.
*
* getVehicle()
* Retrieves the Vehicle object associated with the driver.
*
* setVehicle(Vehicle v)
* Setter for the vehicle associated with the driver.
*/
public interface Driver {
String getFirstName();
String getLastName();
String getFullName();
int getID();
Vehicle getVehicle();
void setVehicle(Vehicle v);
}
/*
* Vehicle
* Represents a vehicle in our system.
*
* getMake()
* Retrieves the make of the vehicle.
*
* getModel()
* Retrieves the model of the vehicle.
*
* getPlate()
* Retrieves the license plate of the vehicle.
*
* getMileage()
* Retrieves the total distance the vehicle has traveled
* up to now. Think of this like its "odometer".
*
* getPosition()
* Retrieves the current position of the vehicle.
*
* moveTo(Position p)
* Updates the mileage of the vehicle by adding the Manhattan
* distance between the vehicle's current position and the
* position p passed in as a parameter. Then updates the
* vehicle's current position to be p.
*/
public interface Vehicle {
String getMake();
String getModel();
String getPlate();
int getMileage();
Position getPosition();
void moveToPosition(Position p);
}
RideRequest
* Represents a request by a client for a ride between their current
* position and some destination.
*
* getClientPosition()
* Retrieves the client position at the time the ride request was
* created.
*
* getDestination()
* Retrieves the destination position.
*
* getIsComplete()
* Returns true if this request has been completed, false if not.
* A new RideRequest starts off as not completed and becomes completed
* if the complete method (see below) is invoked.
*
* complete(Driver driver)
* If the RideRequest has not yet been completed, the method should:
* - update the request to indicate that the request has been completed,
* - create a CompletedRide object that captures information
* about the completed ride (see CompletedRide interface)
* - move the vehicle of the driver to the position of the client and
* then move the vehicle to the destination of the ride,
* - and finally return the CompletedRide object.
*
* If the RideRequest has already been completed, simply returns
* the CompletedRide object describing the completed ride.
*
* getRideTime()
* Returns the Manahattan distance between the client position and the
* destination position as a measure of how long the requested ride
* will take.
*/
public interface RideRequest {
Position getClientPosition();
Position getDestination();
boolean getIsComplete();
CompletedRide complete(Driver driver);
int getRideTime();
}
CompletedRide
* Represents information about how a specific ride request was completed.
*
* getRequest()
* Returns the RideRequest associated with this completed ride.
*
* getDriver()
* Returns the Driver associated with this completed ride.
*
* getWaitTime()
* Returns the Manhattan distance between the position of the driver's vehicle
* and the position of the client making the request at the time that the
* ride was completed. This is intended to be a measure of how long the
* client needed to wait for the driver to arrive.
*
* getTotalTime()
* The sum of the wait time and the ride time associated with the ride request.
*
* getCost()
* Calculates the cost of providing the ride. This cost is calculated as
* one-half of the ride time summed with one-tenth of the wait time.
* Note: while wait and ride times are integer values, cost is a real value.
*
* getPrice()
* Calculates the price charged for the ride. This price is a function
* of the ride time based on the wait time as follows:
*
* Wait Time Price of Ride
* < 25 two and one-half times the ride time
* 25 to 49 twice the ride time
* 50 to 99 equal to ride time
* >= 100 half of ride time
*
* getProfit()
* Calculates the profit (or potentially the loss) generated by
* the completed ride. This is simply cost subtracted from price.
*/
public interface CompletedRide {
RideRequest getRequest();
Driver getDriver();
int getWaitTime();
int getTotalTime();
double getCost();
double getPrice();
double getProfit();
}
package a4test;
import a4.CompletedRide; import a4.RoundRobinDispatcher; import a4.Simulation;
public class SimulationTest {
public static void main(String[] args) { Simulation sim = new Simulation(0, 10, new RoundRobinDispatcher()); double total_profit = 0.0; CompletedRide[] ride_log = sim.getRideLog(); for (int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
