Question
Fleet Continued We continue our motor pool development. This class uses the Car class we wrote previously and extends the Fleet class. What now? This
Fleet Continued
We continue our motor pool development. This class uses the Car class we wrote previously and extends the Fleet class.
What now?
This lab adds a single method to the Fleet class you wrote previously. Here it is:
ArrayList
tester:
A tester is in the attached zip file.
Grading Elements:
class name, all constructors, all method names, all method signatures and return types are correct
ArrayList
Uses only language elements that we've covered so far, includes the sorting code.
The Previous Lab (for reference only)
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
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
import java.util.*;
public class Fleet {
private ArrayList
public Fleet(){
myCars = new ArrayList<>();
}
public void addCar(Car carToAdd){
myCars.add(carToAdd);
}
public int size(){
return myCars.size();
}
public double getCarAverageMPG(){
if(size() == 0)
return -1;
int i;
double MPG_total = 0.0;
for( i = 0 ; i < myCars.size() ; i++ ){
MPG_total += myCars.get(i).getMPG();
}
double avg_MPG = MPG_total / (double)myCars.size();
return avg_MPG;
}
public double getFleetAverageMPG(){
if(getTotalFuel() == 0)
return -1;
return (double)getTotalMiles() / (double)getTotalFuel();
}
public int getTotalMiles(){
int total_miles = 0;
int i;
for( i = 0 ; i < myCars.size() ; i++ ){
total_miles += myCars.get(i).getMiles();
}
return total_miles;
}
public int getTotalFuel(){
int total_fuel = 0;
int i;
for( i = 0 ; i < myCars.size() ; i++ ){
total_fuel += myCars.get(i).getFuelUsed();
}
return total_fuel;
}
public Car find(String carId){
int i;
for( i = 0 ; i < myCars.size() ; i++ ){
if(myCars.get(i).getIdentifier().equals(carId))
return myCars.get(i);
}
return null;
}
Car get(int position){
return myCars.get(position);
}
public String toString(){
String ans = "Fleet [ ";
int i;
for( i = 0 ; i < myCars.size() ; i++ ){
ans += myCars.get(i).toString() + " ";
}
ans += "]";
return ans;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started