Car 2

15 points

This program asks you to create a more realistic version of the Car program you wrote earlier. The user will no longer be able to drive 10,000 miles without buying gas, or add 100 gallons of gas to his gas tank. This program is sufficiently different from your original Car program that I suggest that you start from scratch, using the Java program you can download in the Starting Point section below. You can run a completed version of the program in the Demo section below.

Instance Variables
In addition to the 6 instance variables in the original program, you'll need a double 'gasTankSize'. Instead of declaring 'Color color,' we declare 'String color' (We use a String to store the car's color.)

Give the user a series of options. This menu should repeat (inside a while loop) until the user chooses to quit. After asking a question with a 'System.out.print()' you can wait for the user's integer answer and record the answer with code like this: int choice = in.nextInt();
If the input from the user might be a double, you can record the answer like this: double choice2 = in.nextDouble();
If the input from the user is a String, you can use this: String choiceStr = in.nextLine();
If the input from the user is a single word, you can use this: String word =;

If you'd like, you can use the 'switch' command in lieu of if statements. The beginning of this switch statement might look like this:
switch (choice)
   case 1:    drive();
   case 2:    addFuel();

If 'choice' is equal to 1 in the code above, drive() is called. If 'choice' is equal to 2, addFuel() is called, and so on. It is necessary to conclude each 'case' statement with 'break'.


Here is how the completed version of the program handles 4 different situations:

SituationProgram's response
The car has just 0.5 gallons of gas left. The driver is not asked how many miles he wants to drive. He is told he has to buy gas.
The driver wants to drive more miles than he can with the fuel he has. The variable 'milesPossible' has been calculated as the number of miles the driver can travel and leave 0.5 gallons of gas in the tank. The driver is allowed to drive 'milesPossible' miles. The mileage and fuelLevel are updated.
The driver enters a negative number for the number of miles he wants to drive. The program doesn't respond. It simply pops up the question "What do you want to do?" again.
The driver wants to drive a reasonable number of miles. The driver drives. The mileage and fuelLevel are updated.

Here's how you could structure this in your program:

if (fuelLevel is less than or equal to 0.5)
{     ...
     ask the user how far s/he wants to travel ('miles')
     if ('miles' is greater than 0)
          calculate 'milesPossible'
          if ('miles' is greater than 'milesPossible')
               update 'mileage' and 'fuelLevel' and write a message
               update 'mileage' and 'fuelLevel' and write a message

The screenshot below shows how the drive() method should function.


Ask the user how much gas s/he wants to purchase. Check if this amount of gas would overfill the gas tank. If it would, notify the user of this problem and ask him to try again. If the amount of gas is OK, figure out the new 'fuelLevel' and print the results. If the user enters a negative number, do nothing.

The screenshot below illustrates addFuel():


As in the original program, add 5 mpg to 'mpg' when this method is called. However, set it up so that this method can only be called once. If the user calls addDevice() more than once, print a message that this fuel-saving device has already been added, and can only be added once.


Ask the user what color s/he wants the car to be. Update the car's color.


Create your car, and call menu().


Running your program
Your program runs interactively in the console window. In NetBeans, you may want to make the font size larger. To do this, click in the console window and hit "CTRL - up arrow," or right-click and choose "Larger Font".


Here is the compressed zip file for the completed program. Right-click on the link to download to the root of your server space (H:). Double-click on the downloaded to extract the files Car2.jar and Car2.bat. Then, to run the program, double-click on Car2.bat.


Starting Point
You can download to use as a starting point in writing your program.