Question
I am trying to alter my code to display engine size number of tires Fuel gauge displays current fuel Odometer displays current mileage prompt user
I am trying to alter my code to display engine size number of tires Fuel gauge displays current fuel Odometer displays current mileage prompt user by asking how do you want to drive in miles? then drive
The car can hold a maximum of 15 gallon, 1 gallons drive 24 miles, therefore 15 gallons will drive 360 when fuel is empty prompt user " The tank is empty please enter amount of fuel to be added" Fuel gauge displays current fuel Odometer displays current mileage
user enters amount of fuel then drive until fuel runs out again and the prompt agian " The tank is empty please enter amount of fuel to be added"
process continues until odometer reaches 999,999 The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
//currently displays
//Car engine size: 3000, number of tires: 4
//Fuel left: 15 liter(s)
//Odometer reading: 0 mile(s)
//driving for 240 miles...
//Fuel left: 5 liter(s)
//Odometer reading: 240 mile(s)
// Heresmy code so far
public class FuelGauge { // instance variable private int fuel;
// know about current fuel public void setFuel(int fuel) { this.fuel = fuel; }
// report current fuel public int getFuel() { return fuel; }
// increae current fuel public void increaseFuel() { if (fuel <= 15) fuel++; }
// decrease current fuel public void decreaseFuel() { if (fuel > 0) fuel--; }
}
public class Odometer {
// instance variable private int mileage;
// to know current mileage public void setMileage(int mileage) { this.mileage = mileage; }
// to report about mileage public int getMileage() { return mileage; }
// increament current milegae public void increaseMileage() { if (mileage <= 999999) mileage++; else mileage = 0; }
// decrease fuel for miles travelled public void fuelUsed(FuelGauge fuel) { int amount = mileage / 24;
for (int i = 0; i < amount; i++) fuel.decreaseFuel(); }
}
public class Car {
// instance variables
private int engineSize, numOfTires;
private FuelGauge fuel;
private Odometer odometer;
public Car(int engineSize, int numOfTires) {
this.engineSize = engineSize;
this.numOfTires = numOfTires;
// initializing fuelgauge and odometer objects
fuel = new FuelGauge();
odometer = new Odometer();
}
/** to know and report about car's engine size and number of tires **/
public int getEngineSize() {
return engineSize;
}
public void setEngineSize(int engineSize) {
this.engineSize = engineSize;
}
public int getNumOfTires() {
return numOfTires;
}
public void setNumOfTires(int numOfTires) {
this.numOfTires = numOfTires;
}
// returns the fuel guage object
public FuelGauge getFuelGauge() {
return fuel;
}
// returns the odometer object
public Odometer getOdometer() {
return odometer;
}
public static void main(String[] args) {
// creating a Car object with 3000 as engine capacity, and 4 wheels
Car car = new Car(3000, 4);
// setting fuel to 15 liters
car.getFuelGauge().setFuel(15);
// setting odo reading to 0
car.getOdometer().setMileage(0);
// displaying engine size and number of tires
System.out.println("Car engine size: " + car.getEngineSize()
+ ", number of tires: " + car.getNumOfTires());
// displaying initial fuel left and odo reading
System.out.println(" Fuel left: " + car.getFuelGauge().getFuel()
+ " liter(s)");
System.out.println("Odometer reading: "
+ car.getOdometer().getMileage() + " mile(s)");
// looping and driving 240 miles (calling increaseMileage method of
// odometer object in car 240 times)
System.out.println(" driving for 240 miles...");
while (car.getOdometer().getMileage() < 240) {
car.getOdometer().increaseMileage();
}
// updating the fuel used, assuming fuel guage had enough fuel to go 240
// miles
car.getOdometer().fuelUsed(car.getFuelGauge());
//displaying fuel left and odo reading after 240 miles
System.out.println(" Fuel left: " + car.getFuelGauge().getFuel()
+ " liter(s)");
System.out.println("Odometer reading: "
+ car.getOdometer().getMileage() + " mile(s)");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started