CS120 Project 5 JAVA
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:
1) Create the following variables in the class, and determine the type, if static, if final
a. number of wheels
b. number of doors
c. epa mpg – make this private
d. tank capacity
e. current fuel level – make this private
f. mpg
g. mileage – make this private
2) Create a non default c’tor for sedan
public Sedan(double ptankCapacity, double mpg)
a. full tank
b. zero mileage
c. print warning to if it does not meet the current EPA requirements
3) Create a default c’tor for sedan that calls the non default c’tor with the folloing
a. tank = 10
b. mpg = EPA mpg
4) Define a toString method that prints basic stats of the sedan in the example below
public String toString()
5) Define a drive method
public double drive(double miles)
a. Drive either the specified miles or what the fuel allows
b. Deduct fuel according to miles driven
c. Return actual miles driven
6) Define a pump fuel method
public double pumpFuel(double gallons)
a. Pump with the specified gallons of what the fueld tank allows
b. Return actual fuel pumped
7) Overload a pump fuel method
public double pumpFuel()
a. Fill up tank
b. Return actual fuel pumped
8) setEPA
public static boolean setEPA(double newEPAmpg)
a. Sets the new EPA mpg
b. Return true if new EPA mpg is > 0; else do not set and return false
9) getEPA
a. get the current EPA mpg
10) getMileage
a. get the current mileage
b. note we cannot set the mileage directly because this is illegal!
11) getFuel
a. get the current fuel
12) equals
a. determine if two sedans are equal – justify your answer
CLASS SedanTest:
1) Create a single global variable static Scanner sc = new Scanner(System.in);
2) Define an utility method to read a non-negative number from the Scanner sc
static double readNNegDouble(String s)
a. Print the prompt s
b. Read a double from Scanner sc
c. Return if the double is > 0
d. Print error if no good and loop
3) Define a printMenu method
4) Define a main method
a. Set EPA mpg to 25
b. Create scanner
c. Create default sedan s1
d. Print it
e. Read in tank capacity and mpg for sedan 2 – using the utility method readNNegDouble
f. Create and print sedan s2
g. Do loop that operates on s2
i. printMenu
ii. get input string
iii. create switch statement to handle
1. drive
a. print actual miles driven
2. pump fuel
a. print actual gallons pumped
3. pump up
a. print actual gallons pumped
4. change epa
5. sedan stats
6. quit
h. 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.