import java.awt.Color;
/** * This is a class representing cars. A car has certain features, such * as color, age, number of doors etc and a car can be repainted, * the tank can be filled etc. * * @author Jerry Meng * @version %I%, %G% */ public class Car {
/** * The maximum size of the tank in litres. */ private static final double TANK_SIZE = 100.0; /** * The color of the car. */ private Color color;
/** * The age of the car. */ private int age; /** * The number of doors of the car. */ private int doors;
/** * The amount of gasoline in the tank. */ private double gasoline;
/** * Class constructor, which constructs a brand new, black car with * five doors and a full tank. */ public Car() { this(Color.black, 0, 5, TANK_SIZE); } /** * Class constructor specifying the color, age, number of doors * and litres of gasoline * * @param color The color of the car * @param age The age of the car * @param doors The number of doors * @param km Kilometres driven * @param gasoline The litres of gasoline */ public Car(Color color, int age, int doors, double gasoline) { this.color = color; this.age = age; this.doors = doors; this.gasoline = gasoline; } /** * Returns the color of the car */ public Color getColor() { return color; }
/** * Repaints the car (i.e. changes its color) */ public void setColor(Color color) { this.color = color; } /** * Returns the age of the car */ public int getAge() { return age; }
/** * Returns the number of doors of the car */ public int getDoors() { return doors; }
/** * Returns the amount of gasoline in the tank */ public double getGasoline() { return gasoline; }
/** * Fills the tank. The amount of gasoline cannot exceed * the size of the tank. In that case, the tank will be * filled to the maximum and the rest will run out in * the sand. * * @param gas The amount of gasoline to put in the tank */ public void setGasoline(double gas) { if(gasoline + gas <= TANK_SIZE) gasoline+=gas; else gasoline = TANK_SIZE; } }
|