Instructions
Objective
Write a java assignment program to implement vehicle management.
Requirements and Specifications
Source Code
/**
* This class represents a Bus object.
* Bus that contains location and stopsPerMile
* @author NAME
* @version 1.0
*/
public class Bus extends Vehicle {
/**
* Location of the Bus
*/
private String location;
/**
* Number of stops per Mile
*/
private int stopsPerMile;
/**
* Constructor that creates a Bus object.
* Given id, numMiles, location and stopsPermile
* @param id String
* @param numMiles int
* @param location String
* @param stopsPerMile int
*/
public Bus(String id, int numMiles, String location, int stopsPerMile) {
super(id, numMiles, new String[20]);
this.location = location;
this.stopsPerMile = stopsPerMile;
}
/**
* Constructor that creates a Bus object.
* Given id, numMiles and location
* @param id String
* @param location String
*/
public Bus(String id, String location) {
super(id, 0, new String[20]);
this.location = location;
this.stopsPerMile = 2;
}
/**
* Method to check if the us can drive a given distance.
* @return boolean
*/
@Override
public boolean canDrive(int distance) {
return distance >= 0;
}
/**
* Method that calculates the cost for a travel.
* @return double
*/
@Override
public double calculateCost(int distance) {
return (canDrive(distance)) ? distance * 3 / stopsPerMile : -1;
}
/**
* Method that add passengers.
* @return boolean
*/
@Override
public boolean addPassenger(int distance, String[] names) {
if (canDrive(distance)) {
// Loop through names
for (String s: names) {
// Get index of a free seat
for (int i = 0; i < this.passengers.length; i++) {
if (this.passengers[i] == null) { // seat is free
this.passengers[i] = s;
break;
}
}
}
chargeRide(distance);
setEarnings(getEarnings() + calculateCost(distance));
return true;
} else {
return false;
}
}
/**
* Compare two bus objects.
* @return boolean
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Bus)) {
return false;
}
Bus otherBus = (Bus) other;
return super.equals(other) && getLocation().equals(otherBus.getLocation())
&& getStopsPerMile() == otherBus.getStopsPerMile();
}
/**
* Return a string representation of the bus.
* @return String
*/
@Override
public String toString() {
return String.format("Bus %s This bus drives around %s and "
+ "makes %d stops per mile.", super.toString(), getLocation(), getStopsPerMile());
}
/**
* Getter method for location.
* @return String
*/
public String getLocation() {
return this.location;
}
/**
* Getter method for stops per mile.
* @return int
*/
public int getStopsPerMile() {
return this.stopsPerMile;
}
/**
* Setter method for location.
* @param newLocation String
*/
public void setLocation(String newLocation) {
this.location = newLocation;
}
/**
* Setter method for stops per mile.
* @param newStopsPerMile int
*/
public void setStopsPerMile(int newStopsPerMile) {
this.stopsPerMile = newStopsPerMile;
}
}
Car
/**
* This class represents a Car object.
* Car that contains rate, fees and maxNumMiles
* @author NAME
* @version 1.0
*/
public class Car extends Vehicle {
/**
* Car rate
*/
private double rate;
/**
* One time fee
*/
private double fees;
/**
* Max number of miles
*/
private int maxNumMiles;
/**
* Constructor that creates a Car object.
* Given a id, numMiles, passengers, rate, fees and maxNumMiles
* @param id String
* @param numMiles int
* @param passengers Array of String
* @param rate double
* @param fees double
* @param maxNumMiles int
*/
public Car(String id, int numMiles, String[] passengers,
double rate, double fees, int maxNumMiles) {
super(id, numMiles, passengers);
this.rate = rate;
his.maxNumMiles = maxNumMiles;
}
/**
* Constructor that creates a Car object.
* Given a id, numMiles and maxNumMiles
* @param id String
* @param numMiles int
* @param maxNumMiles int
*/
public Car(String id, int numMiles, int maxNumMiles) {
super(id, numMiles, new String[4]);
this.rate = 10.0;
this.fees = 15.0;
this.maxNumMiles = maxNumMiles;
}
/**
* Constructor that creates a Car object.
* Given a id and set default values to all other variables
* @param id String
*/
public Car(String id) {
super(id, 0, new String[4]);
this.maxNumMiles = 200;
this.rate = 10.0;
this.fees = 15.0;
}
/**
* Method to check if the car can drive the given distance.
* @return boolean
*/
@Override
public boolean canDrive(int distance) {
return distance >= 0 && (getNumMiles() + distance) <= maxNumMiles;
}
/**
* Method to calculate the cost.
* @return double
*/
@Override
public double calculateCost(int distance) {
double cost = (canDrive(distance)) ? (fees + rate * distance) : -1;
return cost;
}
/**
* Method to add list of passengers.
* @return boolean
*/
@Override
public boolean addPassenger(int distance, String[] names) {
if (canDrive(distance)) {
// Get number of free seats
int numFree = 0;
for (int i = 0; i < this.passengers.length; i++) {
if (this.passengers[i] == null) {
numFree++;
}
}
if (numFree >= names.length) { // there are seats for everybody
for (String s: names) {
// First, get the index of the first free seat
int index = -1;
for (int i = 0; i < this.passengers.length; i++) {
if (this.passengers[i] == null) {
index = i;
break;
}
}
// Add to passengers
this.passengers[index] = s;
}
chargeRide(distance);
setEarnings(getEarnings() + calculateCost(distance));
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Method to check if two car objects are equal.
* @return boolean
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Car)) {
return false;
}
Car otherCar = (Car) other;
return super.equals(other) && this.getRate() == otherCar.getRate()
&& otherCar.getFees() == this.getFees()
&& this.getMaxNumMiles() == otherCar.getMaxNumMiles();
}
/**
* Method that returns a string representation of the car object.
* @return String
*/
@Override
public String toString() {
return String.format("Car %s It can only drive %s miles."
+ " It costs %.2f dollars per mile and "
+ "there is a one-time fee of %.2f dollars", super.toString(),
getMaxNumMiles(),
getRate(),
getFees());
}
/**
* Getter method that returns the rate of the Car.
* @return double
*/
public double getRate() {
return this.rate;
}
/**
;* Getter method to return the fee.
* @return double
*/
; public double getFees() {
return this.fees;
}
/**
* Getter method to get the max num of miles.
* @return int
*/
public int getMaxNumMiles() {
return this.maxNumMiles;
}
/**
* Setter method for rate.
* @param newRate double
*/
public void setRate(double newRate) {
; this.rate = newRate;
}
/**
* Setter method for fees.
* @param newFees double
*/
public void setFees(double newFees) {
this.fees = newFees;
}
/**
* Setter method for max number of miles.
* @param newMaxNumMiles int
*/
public void setMaxNumMiles(int newMaxNumMiles) {
this.maxNumMiles = newMaxNumMiles;
}
}
Similar Samples
At Programming Homework Help, we provide expert assistance with coding assignments, ensuring you understand key concepts while meeting your deadlines. Our professional tutors cover a wide range of programming languages and topics, offering personalized support to help you succeed in your coursework.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java