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:

 

  • Create the following variables in the class, and determine the type, if static, if final
    1. number of wheels
    2. number of doors
    3. epa mpg – make this private
    4. tank capacity
    5. current fuel level – make this private
    6. mpg
    7. mileage – make this private
  • Create a non default c’tor for sedan

public Sedan(double ptankCapacity, double mpg)

  1. full tank
  2. zero mileage
  3. 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
    1. tank = 10
    2. 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)

  1. Drive either the specified miles or what the fuel allows
  2. Deduct fuel according to miles driven
  3. Return actual miles driven
  • Define a pump fuel method

public double pumpFuel(double gallons)

  1. Pump with the specified gallons of what the fueld tank allows
  2. Return actual fuel pumped
  • Overload a pump fuel method

public double pumpFuel()

  1. Fill up tank
  2. Return actual fuel pumped
  • setEPA

public static boolean setEPA(double newEPAmpg)

  1. Sets the new EPA mpg
  2. Return true if new EPA mpg is > 0; else do not set and return false
  • getEPA
    1. get the current EPA mpg
  • getMileage
    1. get the current mileage
    2. note we cannot set the mileage directly because this is illegal!
  • getFuel
    1. get the current fuel
  • equals
    1. 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)

  1. Print the prompt s
  2. Read a double from Scanner sc
  3. Return if the double is > 0
  4. Print error if no good and loop
  • Define a printMenu method
  • Define a main method
    1. Set EPA mpg to 25
    2. Create scanner
    3. Create default sedan s1
    4. Print it
    5. Read in tank capacity and mpg for sedan 2 – using the utility method readNNegDouble
    6. Create and print sedan s2
    7. Do loop that operates on s2
      1. printMenu
      2. get input string
  • create switch statement to handle
    1. drive
      1. print actual miles driven
    2. pump fuel
      1. print actual gallons pumped
    3. pump up
      1. print actual gallons pumped
    4. change epa
    5. sedan stats
    6. quit
  1. 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.