Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer’s total, and an accessor method that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel efficiencies. You should decide which variables should be public, if any.
//Odometer
package pkg3;
public class Odometer {
private double oldmiles ,tmilesdriven, tmilage, gallons;
public Odometer(double miles,double average){
oldmiles = miles;
tmilesdriven = miles;
tmilage = average;
}
public double addmiles (double newmiles){
tmilesdriven = 0;
tmilesdriven = newmiles;
return setaverage(tmilesdriven);
}
public double reset(){
tmilesdriven = 0;
return addmiles(tmilesdriven);
}
public double setaverage(double newtrip){
gallons = (newtrip / tmilage);
return gallons;
}
public String toString(){
return "NUMBER OF GALLONS USED ON " + tmilesdriven + " miles trip = " + gallons + "\nTOTAL MILES AFTER TRIP: " + (oldmiles+tmilesdriven);
}
}
//MAIN class
package pkg3;
public class Main {
public static void main(String[] args) {
Odometer o1 = new Odometer(53.7, 32.1);
Odometer o2 = new Odometer(42.9, 15.4);
Odometer o3 = new Odometer(220, 27);
Odometer o4 = new Odometer(672.78, 36);
Odometer o5 = new Odometer(430.9, 27);
o1.addmiles(61.7);
o2.addmiles(404.45);
o3.addmiles(3);
o4.addmiles(224);
o5.addmiles(78);
System.out.println(o1);
System.out.println(o2);
System.out.println(o3);
System.out.println(o4);
System.out.println(o5);
}
}