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

    Fields 
    Modifier and Type Field Description
    static int FORWARD  
    static int PARK  
    static int REVERSE  
  • 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 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

    equals, 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 = 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