Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please help me with this Java program? The problem is after the car cl

ID: 3605675 • Letter: C

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 {

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;
//   }

}

Explanation / Answer

The following is the code for your question along with the output. Only the Fleet.java is new class that is required and the other two (Car.java and FleetTesterMainStarter.java) remain unchanged.
To indent code in Eclipse IDE, you can select the code in each of the java file press "Ctrl + A" and then "Ctrl + i".

I have added the definitions and implementations for each of the required methods.


//============ Fleet.java ====================
import java.util.ArrayList;

public class Fleet {
//Instance variable to store the cars in the fleet
private ArrayList<Car> carList = null;
//No Args constructor
public Fleet() {
carList = new ArrayList<>();
}
//Returns the number of the cars in the fleet
public int size() {
return carList.size();
}
//Returns average miles per galloon of all cars in the fleet
//or return -1, when there are no cars
public double getCarAverageMPG() {
double retCarAverageMPG = -1.0d;
if(carList.size() > 0) {
double totalFleetMPG = 0.0d;
for(int i = 0; i < carList.size(); i++) {
Car car = carList.get(i);
totalFleetMPG += car.getMPG();
}
retCarAverageMPG = totalFleetMPG/carList.size();
}
return retCarAverageMPG;
}
//Returns average miles per galloon of the fleet
//or return -1, when no fuel has been consumed
public double getFleetAverageMPG() {
double retFleetAverageMPG = -1.0d;
if(carList.size() > 0) {
double totalFleetMPG = 0.0d, totalFleetFuelConsumed = 0.0d;
for(int i = 0; i < carList.size(); i++) {
Car car = carList.get(i);
totalFleetMPG += car.getMPG();
totalFleetFuelConsumed += car.getFuelUsed();
}
retFleetAverageMPG = totalFleetMPG/totalFleetFuelConsumed;
}
return retFleetAverageMPG;
}
//Returns the total miles driven by the fleet
public int getTotalMiles() {
int retTotalMiles = 0;
if(carList.size() > 0) {
for(int i = 0; i < carList.size(); i++) {
Car car = carList.get(i);
retTotalMiles += car.getMiles();
}
}
return retTotalMiles;
}
//Returns the total fuel consumed by the fleet
public int getTotalFuel() {
int retTotalFuelConsumed = 0;
if(carList.size() > 0) {
for(int i = 0; i < carList.size(); i++) {
Car car = carList.get(i);
retTotalFuelConsumed += car.getFuelUsed();
}
}
return retTotalFuelConsumed;
}
//Returns the Car object with the given ID
public Car findById(String carId) {
Car retCar = null;
if(carList.size() > 0) {
for(int i = 0; i < carList.size(); i++) {
Car car = carList.get(i);
if(car.getIdentifier().equals(carId)) {
retCar = car;
break;
}
}
}
return retCar;
}
// Returns the Car object at the given position
public Car get(int position) {
Car retCar = null;
if(position >= 0 && position < carList.size()) {
retCar = carList.get(position);
}
return retCar;
}
//String representation of the fleet
public String toString() {
String retString = null;
if(carList != null) {
retString = "Fleet [" + carList.toString() + "]";
}
return retString;
}
//Adds the given car to the fleet
boolean add(Car carToAdd) {
return carList.add(carToAdd);
}
}

//---------------------------------------------------------------

//Output when the tester program is executed.

Congrats, you made it