Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to display my results by inserting a main method within the car class I have included the prompt and the code I

I am trying to display my results by inserting a main method within the car class I have included the prompt and the code I have so far.

Programming in java

Car Simulator

For this assignment, you will design a set of classes that work together to simulate a car, including its fuel gauge and odometer. The classes you will design are the following:

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are as follows:

To know the cars current amount of fuel, in gallons.

To report the cars current amount of fuel, in gallons.

To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons.)

To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

The Odometer Class: This class will simulate the cars odometer. Its responsibilities are as follows:

To know the cars current mileage.

To report the cars current mileage.

To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.

To be able to work with a FuelGauge object. It should decrease the FuelGauge objects current amount of fuel by 1 gallon for every 24 miles traveled. (The cars fuel economy is 24 miles per gallon.)

The Car Class: This class will simulate a car. Its responsibilities are as follows:

Prompt the user for the number of miles this car is travelling.

To know the cars engine size in cubic inches.

To report the cars engine size in cubic inches .

To know the cars number of tires.

To report the cars number of tires.

This class will contain an object from the FuelGauge class as one of its instance variables.

This class will contain an object from the Odometer class as one of its instance variables.

MY CODE

//FuelGauge

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

}

//Odometer

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

}

//Car

public class Car { // instance variables private int engineSize, numOfTires; private FuelGauge fuel; private Odometer 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; }

}

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