Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CAR Program my current prgram displays current fuel: 15 Gallon(s) current mileage : 0 prompt user how many miles do you want to drive? user

CAR Program

my current prgram displays

current fuel: 15 Gallon(s) current mileage : 0

prompt user how many miles do you want to drive? user enters any amount then car drives Fuel Gauge displays amount of fuel left and the Odometer number of miles driven. When the car reaches number of miles that user wants to drive. The program displays you have reached your destination with certain amount of left. If the car runs out of fuel at any time during the disired amount of miles to drive, prompt user to enter amount of fuel in gallons. Max amount of gallons that the tank can hold is 15 gallons. 1 gallon drives 24 miles. if the Odometer reaches 999,999 miles it will reset to 0.

The car will also diplay fuel used and cureent mileage after driving

1 gallon of fuel drives 24 miles therefore with 15 gallons of fuel the car will drive 360 miles

Scenarios i have tested

if the car drives 24 miles the odometer shows 24 miles and current fuel 14 gal ( this is not working) if the car drives 240 miles the odometer shhows 240 miles and current fuel 5 Gallons (this is working)

if the car drives 360 miles the odometer shhows 360 miles and current fuel 0 Gallons (this is not working) if the the distance in miles is over 999999 the the odometer resets to 0 and continues counting miles ( this is not working)

I have a problem with my code but i cannot find it. Certain scenarios work and others do not. Also some of my calculations are off.

HERE IS MY CODE

//Fuel Gauge Class

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--; }

}

//Car Class

// Car.java import java.util.Scanner; public class Car {

private int engineSize, numOfTires; // These are my instance variables

private FuelGauge fuel;

private Odometer odometer;

public Car(int engineSize, int numOfTires) {

this.engineSize = engineSize;

this.numOfTires = numOfTires;

fuel = new FuelGauge(); // here I am initializing fuelgauge and odometer objects

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;

}

public FuelGauge getFuelGauge() {

return fuel; // returns the fuel guage object

}

public Odometer getOdometer() {

return odometer; // returns the odometer object

}

public static void main(String[] args) {

Car car = new Car(3000, 4); // I am creating a Car object with 3000 as engine capacity, and 4 wheels

car.getFuelGauge().setFuel(15); // I am setting fuel to 15 liters

car.getOdometer().setMileage(0); // setting odo reading to 0

System.out.println(" Car engine size: " + car.getEngineSize() + "cubic Inches" // displaying engine size and number of tires

+ ", number of tires: " + car.getNumOfTires());

System.out.println(" Current Fuel: " + car.getFuelGauge().getFuel() // displaying initial fuel left and odo reading

+ " Gallon(s)");

System.out.println("Odometer reading: "

+ car.getOdometer().getMileage() + " mile(s)");

Scanner userinput = new Scanner(System.in); // Will be asking for user input int fuel, mileage, count=0; System.out.println();

System.out.print("How many miles you want to drive: "); mileage = userinput.nextInt();

while (car.getOdometer().getMileage() < mileage) { car.getOdometer().increaseMileage(); count++; //reducing fuel if(count==24) { car.getFuelGauge().decreaseFuel(); count=0; }

if (car.getFuelGauge().getFuel() == 0) { System.out.print(" The tank is empty please enter amount of fuel to be added: "); fuel = userinput.nextInt(); //increasing fuel for(int i=0; i < fuel; i++) car.getFuelGauge().increaseFuel(); System.out.println(" Fuel used: " + car.getFuelGauge().getFuel() + " Gallon(s)"); System.out.println("Odometer reading: " + car.getOdometer().getMileage() + " mile(s)"); if(car.getOdometer().getMileage() == 999999) { System.out.println("The car can't travel further as it reached its limit"); //exception when car mileage reaches the limit break; } } } System.out.println(); System.out.println("You have reached your detination with Fuel left: " + car.getFuelGauge().getFuel() +" Gallon(s)"); userinput.close(); }

}

//Odometer Class

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 <= 999990) 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(); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions