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

Writing user defined data types Fuel Efficiency Calculator For this lab you will

ID: 3853169 • Letter: W

Question

Writing user defined data types Fuel Efficiency Calculator For this lab you will write a class encapsulating the concept of a Fuel Efficiency Calculator that will be used to track the fuel efficiency of an automobile and the average cost per gallon. It could be used to calculate the fuel efficiency on a particular trip, or to compare the fuel efficiency for highway travel vs. city travel, etc... YOU SHOULD READ THIS ENTIRE LAB FIRST, BEFORE YOU BEGIN TO PROGRAM THE SOLUTION! The FuelCalculator class is an Abstract Data Type (ADT). It defines a new type. A FuelCalculator has the following attributes: Instance variables: An integer representing the miles driven A double representing the gallons of gas consumed. A double representing the total cost of the gas used Methods: " mutator methods that can be used to: reset-resets the miles driven to 0 and gallons of gas used to 0 . logMiles - adds: the number of new miles driven the gallons of gas used to drive those miles the cost of that gas . accessor methods do the following: . . . . . get the miles driven get the gallons of gas used get the total cost of the gas used get the fuel efficiency for this trip in miles per gallon get the average cost of a gallon of gas equals method compares the fuel efficiency of two FuelCalculators. Two FuelCalculators are equal if their fuel efficiency is equal within a tenth of a gallon gas used per mile travelled. " toString method returns a String that contains the values for the miles driven, gallons used and the fuel efficiency in the format: · Miles Driven: ## Gas Used: ##.4 gallons Efficiency: ##.# mpg Total Cost: $ # ## Average Cost: $ ## # per gallon

Explanation / Answer


class FuelCalculator
{
private int milesDriven;
private double totalCost, gasUsed;

FuelCalculator()
{
reset();
}

public void reset()
{
milesDriven=0;
gasUsed=0;
totalCost=0;
}
public boolean logMiles(int newMilesDriven, double gasUsed, double costPerGallon)
{
milesDriven+=newMilesDriven;
this.gasUsed+=gasUsed;
totalCost+= (gasUsed * costPerGallon);
  
return true;
}

public int getMiles()
{
return milesDriven;
}
public double getTotalCost()
{
return totalCost;
}

public double getFuelEfficiency()
{
double e=0;
if(gasUsed > 0) e = milesDriven/gasUsed;
return e;
}
public double getCostPerGallon()
{
if(gasUsed > 0)
return totalCost/gasUsed;
else return 0;
}

public boolean equals(FuelCalculator otherCalculator)
{
double difference= getFuelEfficiency() - otherCalculator.getFuelEfficiency();
  
return Math.abs(difference) < 0.1;
}

public String toString()
{
String s = " Miles Driven:## "+milesDriven+" Gas Used: "+gasUsed+" gallons Total Cost: $"+totalCost+" Efficiency: "+getFuelEfficiency()+" Average Cost: "+getCostPerGallon()+" per gallon";
return s;
}

  
}

public class FuelCalculatorTester
{
public static void main(String s[])
{
FuelCalculator myHighwayDriving, myCityDriving;
  
myHighwayDriving = new FuelCalculator();
myCityDriving= new FuelCalculator();
  
myHighwayDriving.logMiles(75,1.34, 74);
myCityDriving.logMiles(70,1.34, 74);
  
System.out.print("My Highway Driving is ");
if(myHighwayDriving.equals(myCityDriving))
System.out.print(" EQUAL TO ");
else System.out.print(" NOT EQUAL TO ");
  
System.out.println(" my City Driving ");
  
System.out.println("Testing toString()");
  
System.out.println("My Highway Driving is "+myHighwayDriving);
System.out.println(" My City Driving is "+myCityDriving);
  
  
}
}