Package car
Class Car
java.lang.Object
car.Car
- All Implemented Interfaces:
java.lang.Comparable<Car>
public class Car extends java.lang.Object implements java.lang.Comparable<Car>
Class invariants:
- The gas tank does not contain a negative amount of gas.
- The gas tank does not have more gas than the tank capacity.
- For every mile traveled, 1/MPG gallons of gas are used.
- The car is in PARK, FORWARD, or REVERSE gear.
-
Field Summary
-
Constructor Summary
Constructors Constructor Description Car()
Create a car that gets 20 mpg and has a tank capacity of 10 gallons -
Method Summary
Modifier and Type Method Description int
compareTo(Car otherCar)
compare this car with otherCar to see which one is ahead (i.e.void
fillTank()
Fills the tank to the full capacity.double
getCapacity()
int
getGear()
double
getLocation()
double
getMPG()
double
getRemainingGas()
void
go(double miles)
Go for a specified distance (or as far as the current gas allows) in the current gearvoid
refuel(double fuelAmount)
Fills the gas tank with the given amount of gas, or enough to fill the tank, whichever is less.void
setGear(int newGear)
Sets the gear that the car will be in.java.lang.String
toString()
Used in printing the car's status.
-
Field Details
-
PARK
public static final int PARK- See Also:
- Constant Field Values
-
FORWARD
public static final int FORWARD- See Also:
- Constant Field Values
-
REVERSE
public static final int REVERSE- See Also:
- Constant Field Values
-
-
Constructor Details
-
Car
public Car()Create a car that gets 20 mpg and has a tank capacity of 10 gallons
-
-
Method Details
-
go
public void go(double miles)Go for a specified distance (or as far as the current gas allows) in the current gearprecondition: car is in gear and has fuel, miles is non-negative
postcondition:
- for gear = FORWARD:
- location = min(location' + miles, location' + (remainingFuel' * milesPerGallon))
- for gear = REVERSE:
- location = max(location' - miles, location' - (remainingFuel' * milesPerGallon))
- Parameters:
miles
- is the number of miles to go
- for gear = FORWARD:
-
fillTank
public void fillTank()Fills the tank to the full capacity. -
refuel
public void refuel(double fuelAmount)Fills the gas tank with the given amount of gas, or enough to fill the tank, whichever is less.precondition: fuelAmount is non-negative
postcondition: remainingFuel = max(tankCapacity, remainingFuel' + fuelAmount)
- Parameters:
fuelAmount
- is the amount of fuel to add
-
getRemainingGas
public double getRemainingGas()- Returns:
- the current amount of gas car has
-
getCapacity
public double getCapacity()- Returns:
- the fuel tank capacity
-
getLocation
public double getLocation()- Returns:
- current location of car
-
getMPG
public double getMPG()- Returns:
- miles per gallon rating on car
-
getGear
public int getGear()- Returns:
- current gear
-
setGear
public void setGear(int newGear)Sets the gear that the car will be in.- Parameters:
newGear
- is the gear to set to.
-
toString
public java.lang.String toString()Used in printing the car's status. The format is:
Tank Capacity: <tankCapacity>
MPG: <mpg>
Gas left: <gasLeft>
Gear: <gear>
Location: <location>
where <tankCapacity>, <mpg>, <gasLeft>, and <location> are numbers, but <gear> is a string with one of the following values: {"Park", "Reverse", "Forward"}- Overrides:
toString
in classjava.lang.Object
-
compareTo
compare this car with otherCar to see which one is ahead (i.e. which one has gone a further distance.)- Specified by:
compareTo
in interfacejava.lang.Comparable<Car>
- Parameters:
otherCar
- is the car to which to compare- Returns:
- -1 if this otherCar is ahead, 0 if equal, 1 if this car is ahead
-