Package car
Class Car
java.lang.Object
car.Car
- All Implemented Interfaces:
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
-
Method Summary
Modifier and TypeMethodDescriptionint
compare this car with otherCar to see which one is ahead (i.e.void
fillTank()
Fills the tank to the full capacity.double
Returns the car's fuel tank capacityint
getGear()
Returns the car's current geardouble
Returns the car's current locationdouble
getMPG()
Returns the car's miles per gallondouble
Returns car's current amount of gasvoid
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.toString()
Used in printing the car's status.
-
Field Details
-
PARK
public static final int PARK- See Also:
-
FORWARD
public static final int FORWARD- See Also:
-
REVERSE
public static final int REVERSE- See Also:
-
-
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 = min(tankCapacity, remainingFuel' + fuelAmount)
- Parameters:
fuelAmount
- is the amount of fuel to add
-
getRemainingGas
public double getRemainingGas()Returns car's current amount of gas- Returns:
- the current amount of gas the car has
-
getCapacity
public double getCapacity()Returns the car's fuel tank capacity- Returns:
- the fuel tank capacity
-
getLocation
public double getLocation()Returns the car's current location- Returns:
- current location of car
-
getMPG
public double getMPG()Returns the car's miles per gallon- Returns:
- miles per gallon rating on car
-
getGear
public int getGear()Returns the car's current gear- 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
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"} -
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 interfaceComparable<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
-