Create a class called \"MPG\" to represent fuel economy of a car. Include the fo
ID: 3657919 • Letter: C
Question
Create a class called "MPG" to represent fuel economy of a car. Include the following: 1) Attributes: starting odometer reading (int), ending odometer reading (int), gallons (double), number of stops (int). 2) Default constructor: all attributes are set to zero. 3) Initial value constructor. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program. 4) Accessors and mutators for all attributes. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program. 5) A method that calculates the average miles-per-gallon for the last fill-up, determines whether it's a city reading (miles per stop < 0.5), highway reading (miles per stop > 1), or combined reading (miles per stop between 0.5 and 1, inclusive), and then estimates the other two readings. Use the estimate that (city MPG) = 0.75*(combined MPG) and (combined MPG) = 0.75*(highway MPG). The output for this method must be an array with three elements (city MPG, combined MPG, and highway MPG).Explanation / Answer
public class MPG
{
// 1) Attributes: starting odometer reading (int), ending odometer reading (int), gallons (double), number of stops (int).
private int odometer_start;
private int odometer_end;
private double gallons;
private int num_stops;
// 2) Default constructor: all attributes are set to zero.
public MPG()
{
odometer_start = 0;
odometer_end = 0;
gallons = 0.0;
num_stops = 0;
}
// 3) Initial value constructor. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program.
public MPG(int odometer_stat, int odometer_end, double gallons, int num_stops)
{
if(odometer_end <= odometer_start)
{
throw new RuntimeException("Ending odometer reading is not greater than starting odometer reading.");
}
this.odometer_start = odometer_start;
this.odometer_end = odometer_end;
this.gallons = gallons;
this.num_stops = num_stops;
}
// 4) Accessors and mutators for all attributes. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program.
public void setStartOdometer(int odometer_start)
{
this.odometer_start = odometer_start;
}
public void setEndOdometer(int odometer_end)
{
this.odometer_end = odometer_end;
}
public void setGallons(double gallons)
{
this.gallons = gallons;
}
public void setNumStops(int num_stops)
{
this.num_stops = num_stops;
}
public int getStartOdometer()
{
return odometer_start;
}
public int getEndOdometer()
{
return odometer_end;
}
public double getGallons()
{
return gallons;
}
public int getNumStops()
{
return num_stops;
}
// 5) A method that calculates the average miles-per-gallon for the last fill-up, determines whether it's a city reading (miles per stop < 0.5), highway reading (miles per stop > 1), or combined reading (miles per stop between 0.5 and 1, inclusive), and then estimates the other two readings. Use the estimate that (city MPG) = 0.75*(combined MPG) and (combined MPG) = 0.75*(highway MPG). The output for this method must be an array with three elements (city MPG, combined MPG, and highway MPG).
public double[] avgMPG()
{
// city, combined, or highway reading
double distance = odometer_end - odometer_start;
double cityMPG = 0.0;
double combinedMPG = 0.0;
double highwayMPG = 0.0;
// city reading
if(distance/num_stops < 0.5)
{
cityMPG = distance/gallons;
combinedMPG = cityMPG/0.75;
highwayMPG = combinedMPG/0.75;
}
// combined reading
else if(distance/num_stops < 1)
{
combinedMPG = distance/gallons;
cityMPG = combinedMPG*0.75;
highwayMPG = combinedMPG/0.75;
}
// highway reading
else
{
highwayMPG = distance/gallons;
combinedMPG = highwayMPG*0.75;
cityMPG = combinedMPG*0.75;
}
return new double[]{cityMPG, combinedMPG, highwayMPG};
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.