the question is : Implement a class Car. A Car object should have three instance
ID: 3648152 • Letter: T
Question
the question is :Implement a class Car. A Car object should have three instance variables, one for fuel efficiency (representing miles per gallon), one for fuel level (representing gallons), and a variable that acts as an odometer (representing miles).
The fuel efficiency of a car should be specified as a parameter in the Car constructor and the constructor should set the fuel level and the odometer to zero. There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods.
There should also be a method addFuel(double gallons) which adds a specified amount to the fuel level and returns the new fuel level, and there should be a method drive(double miles) which simulates driving the car a specified distance.
The drive() method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel. (Notice that there are no setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer should only be changeable by driving the car, as in a real car. The fuel level should only be changed by driving the car or by adding fuel.)
could help you help with this code :
it's java class
/*
This program tests the public interface of the Car class.
When you run this program using your Car class, the only
output you should get is the message "Your Car class passed
all of these tests."
*/
public class PreTestDrive
{
public static void main( String[] args )
{
Car car1 = new Car(25.5);
double fuelEfficiency = car1.getFuelEfficiency();
double fuelLevel = car1.getFuelLevel();
double odometer = car1.getOdometer();
if(fuelLevel!=0.0 || fuelEfficiency!=25.5 || odometer!=0.0)
{
System.out.println("There is a problem with your constructor.");
System.exit(0);
}
double newFuelLevel = car1.addFuel(12.5);
fuelEfficiency = car1.getFuelEfficiency();
fuelLevel = car1.getFuelLevel();
odometer = car1.getOdometer();
if(newFuelLevel!=12.5 || fuelLevel!=12.5 || fuelEfficiency!=25.5 || odometer!=0.0)
{
System.out.println("There is a problem with your addFuel method.");
System.exit(0);
}
double distance = car1.drive(76.5);
fuelEfficiency = car1.getFuelEfficiency();
fuelLevel = car1.getFuelLevel();
odometer = car1.getOdometer();
if(distance!=76.5 || fuelLevel!=9.5 || fuelEfficiency!=25.5 || odometer!=76.5)
{
System.out.println("There is a problem with your drive method.");
System.exit(0);
}
distance = car1.drive(1000);
fuelEfficiency = car1.getFuelEfficiency();
fuelLevel = car1.getFuelLevel();
odometer = car1.getOdometer();
if(distance!=242.25 || fuelLevel!=0.0 || fuelEfficiency!=25.5 || odometer!=318.75)
{
System.out.println("There is a problem with your drive method.");
System.exit(0);
}
distance = car1.drive(1);
fuelEfficiency = car1.getFuelEfficiency();
fuelLevel = car1.getFuelLevel();
odometer = car1.getOdometer();
if(distance!=0.0 || fuelLevel!=0.0 || fuelEfficiency!=25.5 || odometer!=318.75)
{
System.out.println("There is a problem with your drive method.");
System.exit(0);
}
System.out.println("Your Car class passed all of these tests.");
}//main
}//PreTestDrive
public class Car
{
private double fuelEfficiency; //Represents miles per gallon
private double fuelLevel; //Represents gallons
private double odometer; //Represents miles
private double gallons; //Represents the parameter in the addFuel method
public Car(double fuelEfficiency)
{
this.fuelEfficiency = fuelEfficiency;
this.fuelLevel = 0.0;
this.odometer = 0.0;
}
public double getFuelEfficiency()
{
return fuelEfficiency;
}
public double getFuelLevel()
{
//this.fuelLevel = fuelLevel;
return fuelLevel;
}
public double getOdometer()
{
return odometer;
}
public double drive(double miles) //This is where I'm completely lost.
{
miles = (fuelLevel*miles);
return miles;
}
public double addFuel(double gallons) //Not gonna lie, I'm a little confused here as well. Not sure if this is correct.
{
fuelLevel = fuelLevel + gallons;
//if(fuelLevel != gallons)
//fuelLevel = fuelLevel + 12.5;
return fuelLevel;
}
}
Explanation / Answer
//Revised Car Class public class Car { private double fuelEfficiency; //Represents miles per gallon private double fuelLevel; //Represents gallons private double odometer; //Represents miles public Car(double fuelEfficiency) { this.fuelEfficiency = fuelEfficiency; this.fuelLevel = 0.0; this.odometer = 0.0; } public double getFuelEfficiency() { return this.fuelEfficiency; } public double getFuelLevel() { return this.fuelLevel; } public double getOdometer() { return this.odometer; } public double drive(double miles) { double distance = (this.fuelLevel * this.fuelEfficiency); //gallons * MPG = total distance if(distance > miles) { distance = miles; } this.fuelLevel = (fuelLevel - (distance/fuelEfficiency)); this.odometer = odometer + distance; return distance; } public double addFuel(double gallons) { this.fuelLevel = this.fuelLevel + gallons; return this.fuelLevel; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.