Package car

Class Car

All Implemented Interfaces:
Comparable<Car>

public class Car extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
    static final int
     
  • 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
    Fills the tank to the full capacity.
    double
    Returns the car's fuel tank capacity
    int
    Returns the car's current gear
    double
    Returns the car's current location
    double
    Returns the car's miles per gallon
    double
    Returns car's current amount of gas
    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.
    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 Details

  • 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 gear

      precondition: 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
    • 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

      public 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 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 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