Question
JAVA Program using Joptionpane This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class
JAVA Program using Joptionpane
This assignment is about aggregation and class collaboration.
You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. You Must program each class in its own source file. The InstrumentDisplay must have the other classes as private field varaiables.
Your implementation should allow a user to input gallons to add to the fuel tank and miles to drive down the road. Don't allow overfilling the tank. Don't allow the user to drive on an empty tank.
The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current value.
You should also have a display that shows how many miles traveled since you last added gasoline, MilesSinceLastGas, and a display for the estimated number of miles until you run out of gasoline, the CruisingRange.
The Odometer should keep track of total mileage from 0 to 999,999. After that it turns over back to 0. Make sure your code allows for this rollover.
The overall class is to be named InstrumentDisplay. You may create the various outputs using System.out or JOptionPane. Make sure that account for both filling and emptying the tank. While your odometer will display in 1 mile increments, you should keep track of mileage internally in one tenth of a mile increments for purposes of computing gasoline remaining in the FuelGage and miles in the CruisingRange
Some ideas and hints for the Car Instrument Display Programming Challenge.
You need 5 classes plus a demo/test class each in their own source file. The classes should all be public. InstrumentDisplay should use have as private fields the other classes FuelGage, Odometer, MilesSinceLastGas and CruisingRange.
Here is a UML design
InstrumentDisplay |
-fuelGage : FuelGage -odometer : Odometer -milesSinceLastGas : MilesSinceLastGas -cruising : CruisingRange
|
+InstrumentDisplay(fuelGage : FuelGage, odometer : Odometer, mileSinceLastGas : MilesSinceLastGas cruisingRange : CruisingRange)
+getFuelGage() : FuelGage +getOdometer() : Odometer +getMilesSinceLastGas : MilesSinceLastGas +getCruisingRange : CruisingRange
+toString() : String
|
The InstrumentDisplay class essentially gets created and holds a copy of each of the other classes. Whenever you want to display the status of the panel, you call the toString() method either explicitly or implicitly with a print method.
FuelGage |
-sizeOfTank : int -gasInTank : double
|
+FuelGage(sizeOfTank : int, gasInTank : double) +FuelGage(FuelGage : fgToCopy) +addGas(double gas) : +driveDownRoad( int miles):
+toString() : String
|
Odometer |
-miles : int
|
+Odometer(int miles) +Odometer( Odometer : odometerToCopy) +addMiles(int miles) :
+toString() : String
|
MilesSinceLastGas() |
-miles : int
|
+MilesSinceLastGas(int miles) +MilesSinceLastGas(Odometer : odometerToCopy) +addMiles(int miles) : +resetMilesToZero(): +toString() : String
|
CruisingRange |
-milesLeft : int -maxDistance: int
|
+CruisingRange(milesLeft : int, maxDistance : int) +CruisingRange(CruisingRange : cruisingRangeToCopy) +addMiles(int miles) : +subtractMiles(int miles):
+toString() : String
|
The above methods all have Constructors that take their parameters to set each private field. They also have copy Constructors and toString() methods to provide a string representation of the objects state.
You also need a class to test these methods. I suggest a simple class that does the following:
- Create an instance of each of the 4 classes that are fields and use these instances in a constructor to create an InstrumentDisplay object.
- Your demo code should provide a choice to the user in a loop. You need to ask the user if they want to:
- Display all of the Instrument Panel.
- Drive down the road. Ask the user to enter the number of miles to drive. This will reduce gas in the fuel gage, add miles to the odometer, add to miles since last gas and subtract miles from cruising range. Make sure that you dont drive further than the current cruising range because you will run out of gas. You may assume that if you drive exactly to the end of the cruising range, you will, miraculously be at a gasoline station.
- Add gas. This will add to the gas tank and increase the cruising range. Make sure that you dont overfill the tank. Add to the cruising range based on the amount of gas added.
- Park the car. (This ends the demo)
If you need help on reading the UML diagrams and how to aggregate, look at Chapter 8.7 in the text.
SAMPLE OUTPUT (just ideas for you to consider)
Do you want to: A->Display Instrument Panel
B->Drive down the road
C->Add gas
D->Park the car. (This choice ends your program)
User enters A: The output is whatever your toString() method for the InstrumentPanel produces.
User enters B. You prompt the user for how many miles to drive. You test to make sure that it is less than or equal to CruisingRange available. If greater than the cruising range miles, you drive the cruising range miles. At that point your fuelgage is at 0. You tell the user that the user needs to buy gasoline.
User enters C. You prompt the user for how many gallons. If the user tries to overflow the tank, you stop at the tank capacity and tell the user that they could only add to the top of the tank. You will reset Cruising range based on the gas added.
D. If the user chooses D, print the state of the panel one last time with the toString() method then exit.
Try the above actions of getting gas, display, and driving, display several times.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started