September:16(Wed)
CS 162[M]
Homework Assignment #2
Due September 23 (Wednesday)
Preparation for this assignment and upcoming lectures
-
Read
our textbook's
Chapters 2-3.
Objectives
After completing this assignment, you should be able to:
- write source code for a Java class definition
- describe the concept of object state
- describe the components of a method signature
- explain the differences between local variables, method parameters, and instance variables
- write conditional statements with boolean expressions
Specification
Implement a class that simulates an automobile's odometer and fuel gauge operations, keeping track of the
overall and trip odometers and the amount in the fuel tank.
A person can drive the car and add fuel to the tank. Feedback is provided to the user via short text messages.
Sample Demonstration
Here is a sample demonstration of the program specified here;
your program needs to function like this.
Note that this example does not cover all possible situations;
but your program needs to handle everything as specified. Pay close attention to the use of blank lines between each result for readability.
The user's actions which should cause these results are as follows:
(0) initialize the automobile,
(1) add 12 gallons to the tank, (2) drive 250 miles using 10 gallons,
(3) attempt to put in -3 gallons, (4) attempt to drive 100 miles using 4 gallons, and (5) print the current status.
Initializing Auto 1G12345 for Hugh McGuire
You have added 12 gallons to the tank
You have driven 250 miles (10 gallons were used).
Sorry, you cannot enter a negative amount.
You cannot drive that far - there is not enough fuel.
Current status
======================
VIN : 1G12345
owner : Hugh McGuire
miles per gallon: 25.0
odometer : 250
trip odometer : 250
fuel gauge : 2.0
======================
Fields a.k.a. Instance Variables
You need to come up with fields, providing appropriate names and data types for each of them,
corresponding to the data which this class is supposed to have:
Vehicle Identification Number (VIN),
owner,
overall odometer, trip odometer, the amount in the fuel tank,
and the amount of fuel used since the last
time the trip odometer was reset.
Note that VIN isn't really a number since it can contain
letters.
Method Signatures
- public NAMEYOUCHOSE(String VIN_given, String owner_given)
- this constructor
needs to set the VIN and owner fields
and
initialize all the other variables to zero,
and print a greeting that includes the VIN and the owner.
If the VIN's length is wrong, simply print an error message
and carry on anyway.
For this assignment, we'll say that the proper length of a VIN is 7.
- public NAMEYOUCHOSE(String VIN_given, String owner_given, int initMiles, double initFuel)
- a second constructor
sets VIN and owner like the first constructor
and
initializes
the overall odometer
and fuel gauge to the provided parameters.
Negative amounts are simply ignored.
Again, check VIN length, and print a greeting.
- public void addGas(double amtToAdd) - add the amount to the fuel gauge. The amount must be positive. Print appropriate messages for either condition.
- public void resetTrip() - Resets the trip odometer to zero,
and also zeroes the field recording the amount of fuel used since
the last time the trip odometer was reset. No messages are printed.
- public void drive(int miles, double fuelUsed) - add the miles driven to both odometers. Subtract the fuel used from the fuel gauge,
and add it to the amount used since the last time the trip odometer
was reset. The amounts must be positive and the fuel used must be less than or equal to the current fuel gauge. Print appropriate messages for each of the three conditions.
- public double getMPG() - calculate and return the mileage since the last time the trip odometer was reset.
- public int getOdometer() - return the value of the main odometer. No messages are printed.
- public int getTripOdometer() - return the value of the trip odometer. No messages are printed.
- public double getFuelGage() - returns the current fuel gauge. No messages are printed.
- public void printCurrentStatus() - display an appropriate header and the mileage and current state of the auto. (See the sample demonstration.)
Use getMPG() instead of redoing its work.
Further Characteristics
-
Type your name as the author
(like the source code we've been using by Barnes and Kölling).
- Follow our Java Style Guide including javadoc tags
Turn In
- Source code - a printout of your source code.
- Text from two demonstrations, one like the sample one here, and another
short one exercising different methods (you don't have to demonstrate all your
methods — particularly the accessors would be difficult to demonstrate).
Weight of This Assignment
This assignment is worth 40 points.