Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

CAR Program

my current program 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 desired 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 display fuel used and current 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 user adds 15 gallons the car drives 24 miles the odometer shows 24 miles and current fuel 14 gal ( this is not working) if user adds 15 the car drives 240 miles the odometer shows 240 miles and current fuel 5 Gallons (this is working)

if the car drives 360 miles the odometer shows 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)

if car drives 1000 and then add 10 gallon of it give the odometer mileage

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.

image text in transcribed

HERE IS MY CODE

import java.util.Scanner;

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

// decrease current fuel public void decreaseFuel() { if (fuel > 0) fuel--;

}

}

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

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(); boolean maxLimitReached = false;

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

while (car.getOdometer().getMileage()

car.getOdometer().increaseMileage(); count++;

// reducing fuel if (count == 24) { car.getFuelGauge().decreaseFuel(); count = 0; }

if (car.getFuelGauge().getFuel() == 0 && car.getOdometer().getMileage()

// increasing fuel for (int i = 0; i

System.out.println(" Fuel used: " + car.getFuelGauge().getFuel() + " Gallon(s)"); System.out.println("Odometer reading: " + car.getOdometer().getMileage() + " mile(s)");

} if (car.getOdometer().getMileage() >= 99990) { System.out.println("The car can't travel further as it reached its limit"); // exception // when // car // mileage // reaches // the // limit //car.getOdometer().setMileage(0); maxLimitReached = true; break; } } if(!maxLimitReached){ System.out.println(); System.out.println("You have reached your detination with Fuel left: " + car.getFuelGauge().getFuel() + " Gallon(s)"); } userinput.close(); }

}

Car engine size: 3000cubic Inches, number of tires: 4 Current Fuel: 15 Gallon(s) Odometer reading: 0 mile(s) How many miles you want to drive: 1000 The tank is empty please enter amount of fuel to be added: 15 Fuel used: 15 Gallon(s) Odometer reading: 360 mile(s) The tank is empty please enter amount of fuel to be added: 15 Fuel used: 15 Gallon(s) Odometer reading: 720 mile(s) You have reached your detination with Fuel left: 4 Gallon(s)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

If the NPV for an investment is less than zero, the firm will

Answered: 1 week ago