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

Write a Car.java blueprint. Implement a class Car with the following properties.

ID: 3733074 • Letter: W

Question

Write a Car.java blueprint. Implement a class Car with the following properties.

A car has a type (like Honda, Subaru, etc), a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel left in the gas tank. Besides our normal methods, you should have the following methods:

milesLeft() that returns the number of miles left before you run out of fuel

drive() that simulates driving the car for a certain distance. The parameter is the number of miles driven. The method returns a void, reducing the fuel level in the gas tank after driving those miles.

Write a driver named CarDriver.java. Your main() method in the driver should be

public static void main(String[] args) {

                        Car honda = new Car("Honda", 22, 15.0);

                        System.out.println(honda.toString());

                        honda.drive(90);

                        System.out.println(honda.toString());

                        honda.drive(250);

                        System.out.println(honda.toString());

            }

And the output should be

The Honda has a fuel efficiency of 15.0 miles per gallon.

     There are 22.0 gallons left so you can drive 330.0 miles

The Honda has a fuel efficiency of 15.0 miles per gallon.

     There are 16.0 gallons left so you can drive 240.0 miles

You ran out of gas!!

The Honda has a fuel efficiency of 15.0 miles per gallon.

     There are 0.0 gallons left so you can drive 0.0 miles

GCar midterm2018ANSWERSitp120 carType: String gallonsLeft: double fuelEfficiency: double Car) Car(String.double) toString):String milesLeft):double drive(double):void getCarType):String setCarType(String):void getGallonsLeft():double setGallonsLeft(double):void getFuelEfficiency):double setFuelEfficiency(double):void

Explanation / Answer

CarDriver.java

public class CarDriver {

public static void main(String[] args) {

Car honda = new Car("Honda", 22, 15.0);

System.out.println(honda.toString());

honda.drive(90);

System.out.println(honda.toString());

honda.drive(250);

System.out.println(honda.toString());

}

}

Car.java

public class Car {

private String carType;

private double gallonsLeft, fuelEfficiency;

public Car() {

}

public Car(String carType, double gallonsLeft, double fuelEfficiency) {

this.carType=carType;

this.gallonsLeft=gallonsLeft;

this.fuelEfficiency=fuelEfficiency;

}

public String getCarType() {

return carType;

}

public void setCarType(String carType) {

this.carType = carType;

}

public double getGallonsLeft() {

return gallonsLeft;

}

public void setGallonsLeft(double gallonsLeft) {

this.gallonsLeft = gallonsLeft;

}

public double getFuelEfficiency() {

return fuelEfficiency;

}

public void setFuelEfficiency(double fuelEfficiency) {

this.fuelEfficiency = fuelEfficiency;

}

public String toString() {

if(gallonsLeft <=0 ) {

return "You ran out of gas!!. The Honda has a fuel efficiency of "+fuelEfficiency+" miles per gallon."+

"There are 0.0 gallons left so you can drive 0.0 miles";

} else {

return "The Honda has a fuel efficiency of "+fuelEfficiency+" miles per gallon."+

"There are "+gallonsLeft+" gallons left so you can drive "+(gallonsLeft*fuelEfficiency)+" miles";

}

}

public void drive(double f) {

gallonsLeft = (gallonsLeft*fuelEfficiency-f)/fuelEfficiency;

}

public double milesLeft() {

return gallonsLeft;

}

}

Output:

The Honda has a fuel efficiency of 15.0 miles per gallon.There are 22.0 gallons left so you can drive 330.0 miles
The Honda has a fuel efficiency of 15.0 miles per gallon.There are 16.0 gallons left so you can drive 240.0 miles
You ran out of gas!!. The Honda has a fuel efficiency of 15.0 miles per gallon.There are 0.0 gallons left so you can drive 0.0 miles

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote