Java

A Car Class

10 points

In this exercise you design a car class. The methods of the class will enable you to:

Each method should include a call to 'System.out.println()' that prints out what the method just did. For example, you'll see in 'Demo' below that the method that changes the car's color prints out the original color of the car and its new color.

You need to write the constructor that accepts 6 parameters and uses them to set up the 6 instance fields (mileage, mpg, etc.).

The 6 instance variables that you'll need are: the car's year (integer), model (String), color (String), mpg (double), mileage (double) and fuelLevel (double). Here is how the first instance field is set up in my program:
     private double mpg;

In 'main(String args[])', create a Car object using syntax like this:
Car myCar = new Car(model, color, etc.);

Then call each of the car's 7 methods. For example, here is how I call the 'printStatus()' method in main():
     myCar.printStatus();

If you want the numbers that you print out to have 2 decimal digits, use the 'NumberFormat' approach that we've used earlier.

 

Javadoc

Here is Javadoc documentation that lists the methods you are responsible for.

 

Demo

Here is output from my version of this program:
*************************************************************************************

*** Summary of the car's status: ***
Model year: 2018 Make: Toyota Camry
Color: red Mileage: 35000
Miles per gallon: 30 Gallons of gas in tank: 5

The car just drove 100 miles
My car's mileage is now: 35100
The gallons of gas in my car is now: 1.67

The car just had 5.0 gallons of gas added to the tank.
The gallons of gas in my car is now: 6.67

*** Summary of the car's status: ***
Model year: 2018 Make: Toyota Camry
Color: red Mileage: 35100
Miles per gallon: 30 Gallons of gas in tank: 6.67

The car just drove 100 miles

The car was just repainted and was changed from red to black
The gallons of gas in my car is now: 3.33

The car just had a fuel-saving device added that adds 5 mpg to its efficiency.

The car just drove 100 miles
The gallons of gas in my car is now: 0.48

*** Summary of the car's status: ***
Model year: 2018 Make: Toyota Camry
Color: black Mileage: 35300
Miles per gallon: 35 Gallons of gas in tank: 0.48

 

Starting Point

You can download Car.java to use as a starting point in writing your program.