Question
In java In this project we are going to create a class Sedan. A sedan is a four wheeled automobile with 4 doors. Each sedan
In java
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
number of wheels
number of doors
epa mpg make this private
tank capacity
current fuel level make this private
mpg
mileage make this private
2) Create a default ctor for sedan
tank = 10
mpg = EPA mpg
full tank
zero mileage
3) Create a non default ctor for sedan
public Sedan(double ptankCapacity, double mpg)
a. print warning to if it does not meet the current EPA requirements
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)
Drive either the specified miles or what the fuel allows
Deduct fuel according to miles driven
Return actual miles driven
6) 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
7) Overload a pump fuel method
public double pumpFuel()
Fill up tank
Return actual fuel pumped
8) 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
9) getEPA
a. get the current EPA mpg
10) getMileage
get the current mileage
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) Define an utility method to read a non-negative number from a scanner
static double readNNegDouble(String s, Scanner sc)
Print the prompt s
Read a double from scanner sc
Return if the double is > 0
Print error if no good and loop
2) Define a printMenu method
3) 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
i. printMenu
get input string
create switch statement to handle
1. 2. 3.
4. 5. 6.
h. print program
Sample Output:
drive a. print actual miles driven
pump fuel a. print actual gallons pumped
pump up a. print actual gallons pumped
change epa sedan stats quit
terminated
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.
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