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 that changes an instance field (mileage, etc.) 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. It is not necessary to call 'System.out.println()' inside the methods that return a value.

You can use the '\n' character to produce a new line; for example in the following line from my program:
     System.out.println("\nThe car just had a fuel-saving device added that adds 5 mpg to its efficiency.");
the '\n' adds a blank line before the rest of the line prints.

You also need to write 2 constructors: a default constructor that does nothing, and a constructor that accepts 6 parameters and uses them to set up the 6 instance fields (mileage, miles-per-gallon, etc.).

The 6 instance variables that you'll need are: the car's year (integer), model (String), color (Color), miles-per-gallon (double), mileage (double) and fuel level (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(my parameters);

Then mimic the method calls that I used to create the output you see below in 'Demo'. You want to call each of your methods to be sure that everything is working properly. For example, here is how I call the 'printStatus()' method in main():
     myCivic.printStatus();

One method that you write returns the car's mileage and another returns its fuel level. To use one of these methods and print the result, put the method call inside a call to 'System.out.println()'. For example, here is a line from my program:
     System.out.println("The gallons of gas in my car is now: " + myCivic.getFuelLevel() );
where 'myCivic' is my Car object.

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: 1991 Make: Honda Civic
Color: java.awt.Color[r=255,g=0,b=0] Mileage: 140000
Miles per gallon: 30 Gallons of gas in tank: 5

The car just drove 100 miles

My car's mileage is now: 140100

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: 1991 Make: Honda Civic
Color: java.awt.Color[r=255,g=0,b=0] Mileage: 140100
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 java.awt.Color[r=255,g=0,b=0] to
java.awt.Color[r=0,g=0,b=0]

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: 1991 Make: Honda Civic
Color: java.awt.Color[r=0,g=0,b=0] Mileage: 140300
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.