Create Car class using information below Instance Variables private String carMo
ID: 3827161 • Letter: C
Question
Create Car class using information below
Instance Variables
private String carModel; // Car Model (e.g., "Honda Civic")
private double milesPerGallon; // Car's average mpg rating (e.g., 28.5)
private double gasTankSize; // Car's gas tank size, in gallons (e.g., 12.0)
private double gallonsInTank; // Gas (in gallons) remaining in gas tank (e.g., 9.2)
Car API Function parameters) Description Carl Default Constructor tankSize, double galLevel) String getCarModelO Returns a String representing the car model void Isett Car Modelstring m) Sets the cars model to m double getMilesPerGalon Returns a double representing the cars mog rating void setMilesPerGallon double mpg) Sets the cars miles-per-gallon rating to mpg double get GasTanksize() Returns a double representing the car's tank size in void setGasTanksize(double tankSize) Sets the car's tank size to tanksize (gallons) double allonslnTankl Returns a double representing the number of gallons in the car's tank. void setGallonslinTank(double galLevel Sets the car's tank level to galLevel. If galLevel is bigger than the tank size, then automa set the number of atical gallons in the tank to the tank size. drive Distance double miles) boolean Simulates the car driving some distance, represented by the miles parameter. This function will appropriately burn" the proper amount of gas in the tank. f there is enough gas to driv e a distance of miles, this function should print out how many gallons of fuel were just burnt AND how many more miles can be driven before running out of fuel. Return true to indicate a successful drive. f there is not enou g in the tank to drive a distance of miles without running out of fuel, the function should print out a statement indicating how many miles were traveled before running out of fuel. Return false to ndicate a failed drive. NOTE: See sample output for print formatting double filIT "Tops" off the tank by filling it to capacity. Prints to the Tank user how many gallons of fuel were added and how many miles they can travel on their full tank. Finally, return the number of gallons added as a double NOTE. See sample output for print formgtting. Car convertToHybrid() Creates a new car object with the same instance parameter variables, but adds the word ybrid" to the new car model and increases its miles-per-gallon rating by 10mpg. Returns the new car object. String toString) boolean equals(object o Returns true if ois a stance of a Car AND has the same exact values as all of the instance variables. Returns fal se otherwise.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class Car {
private String carModel; // Car Model (e.g., "Honda Civic")
private double milesPerGallon; // Car's average mpg rating (e.g., 28.5)
private double gasTankSize; // Car's gas tank size, in gallons (e.g., 12.0)
private double gallonsInTank; // Gas (in gallons) remaining in gas tank (e.g., 9.2)
public Car() {
}
public Car(String carModel, double milesPerGallon, double gasTankSize, double gallonsInTank) {
this.carModel = carModel;
this.milesPerGallon = milesPerGallon;
this.gasTankSize = gasTankSize;
this.gallonsInTank = gallonsInTank;
}
public String getCarModel() {
return carModel;
}
public double getMilesPerGallon() {
return milesPerGallon;
}
public double getGasTankSize() {
return gasTankSize;
}
public double getGallonsInTank() {
return gallonsInTank;
}
public void setCarModel(String carModel) {
this.carModel = carModel;
}
public void setMilesPerGallon(double milesPerGallon) {
this.milesPerGallon = milesPerGallon;
}
public void setGasTankSize(double gasTankSize) {
this.gasTankSize = gasTankSize;
}
public void setGallonsInTank(double gallonsInTank) {
this.gallonsInTank = gallonsInTank;
}
public boolean driveDistance(double miles){
double totalMiles = gallonsInTank*milesPerGallon;
double requiredGallon = miles/milesPerGallon;
if(miles <= totalMiles){
System.out.println(requiredGallon+" gallons of fuel just burnt and "+
((gallonsInTank-requiredGallon)*milesPerGallon)+" more miles can be driven");
gallonsInTank = gallonsInTank - requiredGallon;
return true;
}else{
gallonsInTank = 0;
System.out.println((gallonsInTank*milesPerGallon)+" miles driven without running out of fuel");
return false;
}
}
public double fillTank(){
double requiredGallon = gasTankSize - gallonsInTank;
gallonsInTank = gasTankSize; // filling tank
System.out.println(requiredGallon+" gallons added and can be driven "+(gasTankSize*milesPerGallon)+" miles");
return requiredGallon;
}
public Car convertToHybride(){
Car newCar = new Car(carModel+" Hybride", milesPerGallon+10, gasTankSize, gallonsInTank);
return newCar;
}
@Override
public String toString() {
return "Model: "+carModel+" "+
"Miles Per Gallon: "+milesPerGallon+" "+
"Gallon in tank: "+gallonsInTank+" "+
"Tank capacity: "+gasTankSize;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Car){
Car other = (Car)obj;
if(carModel.equals(other.carModel) && gallonsInTank == other.gallonsInTank
&& gasTankSize == other.gasTankSize && milesPerGallon ==other.milesPerGallon)
return true;
}
return false;
}
}