Question: Java::: Fleet We continue our motor pool development. This class uses the Car class we wrote previously. Write a class Fleet . A Fleet object
Java::: Fleet
We continue our motor pool development. This class uses the Car class we wrote previously. Write a class Fleet. A Fleet object maintains a collection of Car objects. We will assume that all of the car identifiers in the fleet are unique. A Fleet keeps an
ArrayList as an instance variable
It has a no-argument constructor
accessors:
int size() // Returns the number of cars in the fleet
double getCarAverageMPG() // returns the average miles per gallon of all the cars in the fleet, or -1 if the fleet has no cars
double getFleetAverageMPG() // returns the miles per gallon of the fleet as a whole (i.e., uses the fleet's fuel consumption instead of number of cars), or -1 if the fleet has consumed no fuel
int getTotalMiles() // returns the total miles driven by the fleet
int getTotalFuel() // returns the total fuel consumed by the fleet
Car find(String carId) // returns the Car object with the given car identifier, or null if there is no such car
Car get(int position) // returns the Car in the given position in the fleet. Cars are maintained in insertion order, so the first car is in position 0, the second in position 1, etc.
String toString() // returns a String representation of the object, something like
Fleet [
Car [identifier=Ford1950, miles=160000, fuelUsed=16000]
Car [identifier=Buick1964, miles=50000, fuelUsed=3500]
]
mutators:
boolean add(Car carToAdd) // adds the carToAdd to the fleet's collection of cars and returns true if the addition was successful, false otherwise
tester:
A tester is in the zip file attached to this item.
Grading Elements:
class name, all constructors, all method names, all method signatures and return types are correct
constructor, find, total Miles and Fuel methods, average MPG methods, perform correct operation, return correct values
accessors do not modify object state.
public class Car {
public String ident;
public int totFuel;
public int totMiles;
public Car(){
ident = "";
totFuel = 0;
totMiles = 0;
}
public Car(String identifier) {
ident = identifier;
totFuel = 0;
totMiles = 0;
}
public Car(String identifier, int miles, int fuelUsed) {
ident = identifier;
totFuel = fuelUsed;
totMiles = miles;
}
public String getIdentifier() {
return ident;
}
public int getFuelUsed() {
return totFuel;
}
public int getMiles() {
return totMiles;
}
public double getMPG() {
return totMiles * totFuel;
}
public int compareMPG(Car otherCar) {
if (otherCar.getMPG() > this.getMPG()) {
return -1;
}
else if (otherCar.getMPG() < this.getMPG()) {
return 1;
}
else {
return 0;
}
}
public String toString() {
return "Car [identifier="+getIdentifier()+", miles="+getMiles()+", fuelUsed="+getFuelUsed()+"]";
}
public void setIdentifier(String identifier) {
ident = identifier;
}
public void setFuelUsed(int fuelUsed) {
totFuel = fuelUsed;
}
public void setMiles(int miles) {
totMiles = miles;
}
public void addFuel(int fuelAmount) {
totFuel += fuelAmount;
}
public void addMiles(int milesTraveled) {
totMiles += milesTraveled;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
