CS120 Project 5
In this project we are going to create a class Sedan. A sedan is a four wheeled automobile with 4 doors. Each sedan can have a different gas tank that has a fixed capacity, and it can contain certain number of gallons of fuel. We will assume that each sedan can get a different fixed constant miles per gallon, and there is an EPA standard miles per gallon for all sedans that gets updates every year. The sedan also has a mileage. Right out of the factory, the sedan has a full tank of gas and zero mileage.
CLASS Sedan:
- Create the following variables in the class, and determine the type, if static, if final
- number of wheels
- number of doors
- epa mpg – make this private
- tank capacity
- current fuel level – make this private
- mpg
- mileage – make this private
- Create a non default c’tor for sedan
public Sedan(double ptankCapacity, double mpg)
- full tank
- zero mileage
- print warning to if it does not meet the current EPA requirements
- Create a default c’tor for sedan that calls the non default c’tor with the folloing
- tank = 10
- mpg = EPA mpg
- Define a toString method that prints basic stats of the sedan in the example below
public String toString()
- Define a drive method
public double drive(double miles)
- Drive either the specified miles or what the fuel allows
- Deduct fuel according to miles driven
- Return actual miles driven
- Define a pump fuel method
public double pumpFuel(double gallons)
- Pump with the specified gallons of what the fueld tank allows
- Return actual fuel pumped
- Overload a pump fuel method
public double pumpFuel()
- Fill up tank
- Return actual fuel pumped
- setEPA
public static boolean setEPA(double newEPAmpg)
- Sets the new EPA mpg
- Return true if new EPA mpg is > 0; else do not set and return false
- getEPA
- get the current EPA mpg
- getMileage
- get the current mileage
- note we cannot set the mileage directly because this is illegal!
- getFuel
- get the current fuel
- equals
- determine if two sedans are equal – justify your answer
CLASS SedanTest:
- Create a single global variable static Scanner sc = new Scanner(System.in);
- Define an utility method to read a non-negative number from the Scanner sc
static double readNNegDouble(String s)
- Print the prompt s
- Read a double from Scanner sc
- Return if the double is > 0
- Print error if no good and loop
- Define a printMenu method
- Define a main method
- Set EPA mpg to 25
- Create scanner
- Create default sedan s1
- Print it
- Read in tank capacity and mpg for sedan 2 – using the utility method readNNegDouble
- Create and print sedan s2
- Do loop that operates on s2
- printMenu
- get input string
- create switch statement to handle
- drive
- print actual miles driven
- pump fuel
- print actual gallons pumped
- pump up
- print actual gallons pumped
- change epa
- sedan stats
- quit
- drive
- print program terminated
Sample Output:
Sedan 1
Sedan 1 created.
Sedan with 4 wheels and 4 doors,
with 10.0 gallon fuel tank and 10.0 gallons fuel,
with 0.0 mileage,
and with 25.0 mpg meeting the EPA mpg of 25.0.
Sedan 2
Enter tank capacity:
-1
ERROR – input a non-negative number
Enter tank capacity:
10
Enter MPG:
-5
ERROR – input a non-negative number
Enter MPG:
25
Sedan with 4 wheels and 4 doors,
with 10.0 gallon fuel tank and 10.0 gallons fuel,
with 0.0 mileage,
and with 25.0 mpg meeting the EPA mpg of 25.0.
Sedan 2 created.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
d
Enter miles:
100
100.0 actual miles driven.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
p
Enter gallons:
50
4.0 actual gallons pumped.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
d
Enter miles:
1000
250.0 actual miles driven.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
u
10.0 actual gallons pumped.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
d
Enter miles:
200
200.0 actual miles driven.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
s
Sedan with 4 wheels and 4 doors,
with 10.0 gallon fuel tank and 2.0 gallons fuel,
with 550.0 mileage,
and with 25.0 mpg meeting the EPA mpg of 25.0.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
e
Enter EPA mpg:
30
EPA mpg updated.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
s
Sedan with 4 wheels and 4 doors,
with 10.0 gallon fuel tank and 2.0 gallons fuel,
with 550.0 mileage,
and with 25.0 mpg not meeting the EPA mpg of 30.0.
————-
Menu
————-
(D)rive
(P)ump fuel
Pump (U)p
Change (E)PA
Sedan (S)tats
(Q)uit
————-
q
Program Terminated.