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 getSortedByMPG() // returns a copy of the fleet's list of cars sorted by MPG (with the worst MPG car first, the best MPG car last). NOTE THIS IS AN ACCESSOR. Include sorting code in your class; the Selection Sort code in the Arrays chapter may help. Also note that ArrayList has a constructor that will take a "Collection" to supply the elements of the new ArrayList. An ArrayList is a Collection, so that constructor may help. You may also wish to review the video(s) on the Object Memory Model that covered aliases. Do not use language elements that we've not covered.

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 getSortedByMPG() returns an array list of all the cars in the fleet with the worst-MPG car first, and the best-MPG car last. This method leaves the fleet's instance variable(s) unchanged.

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 as an instance varible

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 myCars;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!