Preparation
Do the following before starting this project:
- Read chapter 2
- Complete all of the book exercises related to the Ticket Machine
example
Objectives
After completing this project, you should be able to:
- write 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
- explain the different between the int and double primitive data types
- write conditional statements with boolean expressions
Project Specification
Implement a class that simulates a car. You can drive and add gas as needed. Feedback is provided to the driver via short text messages.
- Provide appropriate names and data types for each of the instance
variables.
- an integer for the mileage
- an integer for the mileage of the next oil change
- a double for the gallons of gas in the tank
- a double for the miles per gallon.
- a final double of FULL_TANK = 12.5 that never changes.
Method signatures
- public Car ( ) - this default constructor sets the miles per gallon to 22.7, fills the tank, sets the odometer to zero and sets the next oil change variable.
- public Car (double mpg) - this constructor does everything the default constructor does and sets the miles per gallon to the provided amount.
- public int checkOdometer ( ) - return the current mileage. Do not print a message.
- public void drive (int miles) - Plan for three options. 1) the car is already out of gas, or 2) the driver runs out of gas before completing the trip but the odometer is updated correctly; or 3) the driver completes the trip without running out of gas. Print an appropriate message for each of the three conditions. See sample output below.
- public void addGas (double gallons) - Plan for three options, 1) print a warning if the number of gallons is less than or equal to zero; 2) provide a warning message if the number of gallons results in the tank overflowing but still fill the tank; or 3) add the provided amount of gallons to the tank. Print an appropriate message for each of the three conditions. See sample output below.
- public void honkHorn ( ) - print a message more creative than "beep beep!"
- public void changeOil ( ) - set the internal reminder to change the oil in 3,000 miles. Print an appropriate message that the oil was changed and when it should be next changed.
- public void checkOil ( ) - Plan for two options: 1) It is time to change the oil, or 2) it is not. Print an appropriate message for each condition. See sample output below.
- public double checkGasGauge ( ) - return the gallons in the tank. Do not print a message.
Bonus Challenges
If you finish the project early, you may want to attempt some of the
following additional challenges.
- Assume a gallon of gas costs $2.19. Add code to the addGas method to calculate the amount of money needed for the gas. For example, you just added 13.5 gallons of gas at a cost of $29.57. Display all money amounts using the appropriate symbols and decimals places ($1.25 or
$0.50). Use the DecimalFormat object to help. (+3 pts)
- Add a boolean instance variable that monitors if the engine is on or off (see section 3.8 for a few clues). Create new methods: startEngine( ) and stopEngine( ) that set the boolean variable appropriately. Modify existing methods with a warning message if the car is, or is not, running as appropriate. The car must be running to drive, check the gas gauge or check the odometer. The car must be turned off to add gas, check oil, or change the oil. (+3 pts)
Sample Results
Here are some sample messages based on different options. Your program
should generate similar (but not necessarily exact) messages. Pay close attention to blank spaces, spelling and punctuation.
You drove 100 miles. You drove 30 miles.
The oil looks OK for now. You added 4.2 gallons and now have 15.522222222222222 gallons in the tank. You drove 349.25 miles before running out of gas.... You have no gas! You overfilled the tank and now have 12.5 gallons in the tank.
You just added oil and the next change should occur at 6,455 miles.
Grading Criteria
This project is worth a possible 100 points.
- Stapled cover page with your name and signed pledge (- 10
pts if missing)
- Code does not compile (-10 pts)
- Each method does what it is supposed to do (60 pts)
- Source code follows our Java
Style Guide (15 pts)
- Appropriate documentation including javadoc tags (15 pts)
- Sample output showing all possible messages (10 pts)
Turn In
- Cover page - Your project must have a cover page that includes your name, a title,
an appropriate picture and the following signed pledge. "I pledge
that this work is entirely mine, and mine alone (except for any code
provided by my instructor). " You are responsible for understanding and adhering to the School of CIS
Guidelines for Academic Honesty.
- Source code - a printout of your source code (with your name).
- Sample output - a printout of sample output. You will need to cut and paste from the terminal window to print the results. As an alternative, you can save the contents of the Terminal to a text file and then print.
- Be prepared to demo your project during lab.
Late Policy
Projects are expected to be turned in on time. However, you are encouraged to complete a project even if you must turn it in late.
- 1 weekday late (-20 pts)
- every subsequent weekday is an additional -10 pts
- Saturday and Sunday are free days
|