car
Class Car

java.lang.Object
  extended by car.Car
All Implemented Interfaces:
java.lang.Comparable<Car>

public class Car
extends java.lang.Object
implements java.lang.Comparable<Car>

Class invariants:


Field Summary
static int FORWARD
           
static int PARK
           
static int REVERSE
           
 
Constructor Summary
Car()
          Create a car that gets 20 mpg and has a tank capacity of 10 gallons
 
Method Summary
 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 gear
 void 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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 Detail

Car

public Car()
Create a car that gets 20 mpg and has a tank capacity of 10 gallons

Method Detail

go

public void go(double miles)
Go for a specified distance (or as far as the current gas allows) in the current gear

precondition: car is in gear and has fuel, miles is non-negative

postcondition:

Parameters:
miles - is the number of miles to go

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 class java.lang.Object

compareTo

public int compareTo(Car otherCar)
compare this car with otherCar to see which one is ahead (i.e. which one has gone a further distance.)

Specified by:
compareTo in interface java.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