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
What I have so far...
Car.java
package fleetss;
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; } }
Fleets.java
package fleetss;
import java.util.ArrayList;
public class Fleet { private ArrayList
FleetTester.java
package fleetss;
public class FleetTester { public static void main(String[] args) { Fleet fleet=new Fleet(); Car car; System.out.println("add: "+fleet.add(new Car("Ford1950",160000,16000))); System.out.println("add: "+fleet.add(new Car("Buick1964",50000,3500))); System.out.println("Size: "+fleet.size()); System.out.println("CarAvgMPG: "+fleet.getCarAverageMPG()); System.out.println("FleetAvgMPG: "+fleet.getFleetAverageMPG()); System.out.println("Total Miles: "+fleet.getTotalMiles()); System.out.println("Total Fule: "+fleet.getTotalFule()); System.out.println("Car Find: "+fleet.find("buick1964")); System.out.println("get(position): "+fleet.get(0)); System.out.println("To String: "+fleet.toString()); } }
FleetTestMain.java
import java.util.ArrayList;
// note: all the new String(new String("xyz")
// is to flush out anyone using == to compare String objects
// note: this code looks a bit odd because I derived it
// from a JUNIT test class
public class FleetTestMain {
// milage (l, m, h); fuel(l, m, h), mpg(l, m, h)
static final Car[] CAR_ARRAY = {
new Car(new String("lhl"), 100, 200), // 0
new Car(new String("lmm"), 100, 5), // 1
new Car(new String("llh"), 100, 2), // 2
new Car(new String("mhl"), 500, 200), // 3
new Car(new String("mmm"), 600, 5), // 4
new Car(new String("mlh"), 550, 2), // 5
new Car(new String("hhl"), 1001, 200), // 6
new Car(new String("hmm"), 1000, 5), // 7
new Car(new String("hlh"), 1000, 2), // 8
};
public static void main(String[] args) {
int failCount = 0;
if (!testFindById())
failCount++;
if (!testGetCarAverageMPG())
failCount++;
if (!testGetCarBestMPG())
failCount++;
if (!testGetCarHighestMilage())
failCount++;
if (!testGetFleetAverageMPG())
failCount++;
if (!testGetTotalFuel())
failCount++;
if (!testGetTotalMilage())
failCount++;
if (!testSortByMPG())
failCount++;
if (failCount == 0) {
System.out.println("Congrats, you made it");
} else {
System.out.println("There are " + failCount + " bugs remaining");
}
}
public static boolean testFindById() {
Fleet fl = new Fleet();
for (Car c : CAR_ARRAY) {
fl.add(c);
}
Car c = fl.findById(new String("mlh"));
if (!(CAR_ARRAY[5].equals(c))) {
System.out.println("findById failed, expected car " + CAR_ARRAY[5].toString() + " found " + c);
return false;
}
return true;
}
public static boolean testGetFleetAverageMPG() {
Fleet fl = new Fleet();
fl.add(CAR_ARRAY[3]);
fl.add(CAR_ARRAY[7]);
double mpg = fl.getFleetAverageMPG();
if (!(Math.abs(7.317 - mpg) < .001)) {
System.out.println("fleet mpg bad for " + CAR_ARRAY[3] +
" and " + CAR_ARRAY[7] + " expected 7.317, found " + mpg);
return false;
}
return true;
}
public static boolean testGetCarAverageMPG() {
Fleet fl = new Fleet();
fl.add(CAR_ARRAY[3]);
fl.add(CAR_ARRAY[7]);
double mpg = fl.getCarAverageMPG();
if (!(Math.abs(101.25 - mpg) < .001)) {
System.out.println("car avg mpg bad for " + CAR_ARRAY[3] + " and " + CAR_ARRAY[7]
+ " expected 101.25, but was " + mpg);
return false;
}
return true;
}
public static boolean testGetTotalFuel() {
Fleet fl = new Fleet();
fl.add(CAR_ARRAY[3]);
fl.add(CAR_ARRAY[7]);
int fuel = fl.getTotalFuel();
if (!(205 == fuel)) {
System.out.println("total fuel bad for " + CAR_ARRAY[3] + " and " + CAR_ARRAY[7]);
return false;
}
return true;
}
public static boolean testGetTotalMilage() {
Fleet fl = new Fleet();
fl.add(CAR_ARRAY[3]);
fl.add(CAR_ARRAY[7]);
int miles = fl.getTotalMiles();
if (!(1500 == miles)) {
System.out.println("total miles bad for " + CAR_ARRAY[3] + " and " + CAR_ARRAY[7]);
return false;
}
return true;
}
public static boolean testGetCarBestMPG() {
Fleet fl = new Fleet();
for (Car c : CAR_ARRAY) {
fl.add(c);
}
Car best = fl.getCarBestMPG();
if (!(best.getIdentifier().equals("hlh"))) {
System.out.println("best mpg bad, expected " + CAR_ARRAY[8] + " found " + best);
return false;
}
return true;
}
public static boolean testGetCarHighestMilage() {
Fleet fl = new Fleet();
for (Car c : CAR_ARRAY) {
fl.add(c);
}
Car best = fl.getCarHighestMilage();
if (!(best.getIdentifier().equals("hhl"))) {
System.out.println("highest milage bad expected " + CAR_ARRAY[6] + " found " + best);
return false;
}
return true;
}
public static boolean testSortByMPG() {
Fleet fl = new Fleet();
for (Car c : CAR_ARRAY) {
fl.add(c);
}
ArrayList
if (sorted.size() != CAR_ARRAY.length) {
System.out.println("sortedByMPG returned wrong number of elements, should be " + CAR_ARRAY.length
+ " but was " + sorted.size());
return false;
}
for (int i = 0; i < CAR_ARRAY.length - 2; i++) {
Car current = sorted.get(i);
Car next = sorted.get(i + 1);
if (!(current.getMPG() <= next.getMPG())) {
System.out.println("sorted arraylist is not in order: car( new String(" + i + ") is " + sorted.get(i)
+ " car( new String(" + (i + 1) + ") is " + sorted.get(i + 1));
return false;
}
}
boolean sameOrder = true;
int j = 0;
while (sameOrder && j < fl.size()) {
if (!(fl.get(j).getIdentifier().equals(sorted.get(j).getIdentifier())))
sameOrder = false;
j++;
}
if (sameOrder) {
System.out.println(
"fleet's internal array list was sorted instead of a copy being made of it and the copy sorted ");
return false;
}
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
