Question: Could you please help me with this Java program? The problem is after the car class. Here is the car class. public class Car {
Could you please help me with this Java program? The problem is after the car class.
Here is the car class.
public class Car { private String identifier; private int milage; private int fuelUsed; public Car() { identifier = ""; milage = 0; fuelUsed = 0; }
public Car(String identifier) { this.identifier = identifier; milage = 0; fuelUsed = 0; } public Car(String identifier, int milage, int fuelUsed) { this.identifier = identifier; this.milage = milage; this.fuelUsed = fuelUsed; } public void addFuel(int fuel) { fuelUsed += fuel; } public void addMiles(int miles) { milage += miles; } public double getMPG() { return (double)milage / fuelUsed; }
public String getIdentifier() { return identifier; }
public void setIdentifier(String identifier) { this.identifier = identifier; }
public int getMiles() { return milage; }
public void setMiles(int milage) { this.milage = milage; }
public int getFuelUsed() { return fuelUsed; }
public void setFuelUsed(int fuelUsed) { this.fuelUsed = fuelUsed; } public int compareMPG(Car otherCar) { double myMPG = getMPG(); double otherMPG = otherCar.getMPG(); if (myMPG == otherMPG) return 0; if (myMPG - otherMPG > 0) return 1; return -1; }
public boolean equals(Object obj){ if (this==obj) return true; if (obj==null) return false; if (this.getClass()!=obj.getClass()) return false; final Car other = (Car)obj; if ((this.getIdentifier().equals(other.getIdentifier()) && this.getMPG()==other.getMPG())) return true; return false; } public String toString() { return "Car [identifier=" + identifier + ", milage=" + milage + ", fuelUsed=" + fuelUsed + "]"; }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the tester file:
import java.util.ArrayList;
// note: all the new String("xyz") stuff // is to flush out code using == to compare String objects // instead of .equals(....). // Sane code would normally just do // new Car("lhl", 100, 200), // 0
// note: this code looks a bit odd because I derived it // from a JUNIT test class
public class FleetTestMainStarter {
// 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 (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; // }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
