/** * This class simulates an automobile's odometer and * fuel guage operations. A person can drive the car, * add fuel to the car. * * @author Chuck Bates * @version 02.01.2010 */ public class Auto { // instance variables private int odometer; private double fuelGauge; private int tankCapacity; private int mpg; /** * Default constructor for objects of class Auto */ public Auto() { // initialise instance variables odometer = 0; fuelGauge = 0; tankCapacity = 15; mpg = 25; System.out.println("A simple automobile for Chuck has been initialized "); } /** * Constructs creates an Auto with with specified * values of 25mpg a fixed tankCapacity of 15. * @param initMile, the mileage on the odometer * @param initFuel sets the amount of fuel in Auto * as long as this is less than or equal to the tanks limit */ public Auto(int initMiles, double initFuel){ tankCapacity = 15; if(initFuel <= tankCapacity){ fuelGauge = initFuel; odometer = initMiles; mpg = 25; System.out.println("A custom automobile has been initialized"); }else{ System.out.println("Instance not created; " + initFuel + " exceeds the tank capacity"); } } /** * addFuel adds fuel to the tank while not * allowing an overflow and updating the * fuel gauge. */ public void addFuel(double amtToAdd) { if (tankCapacity >= amtToAdd + fuelGauge ){ fuelGauge = amtToAdd + fuelGauge; System.out.println("you added " + amtToAdd + " gallons to your tank \n and now have " + fuelGauge + " gallons in your tank"); }else{ System.out.println("Can't add " + amtToAdd + " gallons, Your tank will overflow"); } } /** * drive allows the Auto to go a distance * as long as that distance does not * require more fuel than is available. * @param distance is the amount a person * wants to drive Auto */ public void drive(int distance){ if( fuelGauge < distance / mpg){ System.out.println("You don't have enough fuel to go " + distance + " miles"); }else{ System.out.println("You drove " + distance + " miles"); fuelGauge = fuelGauge - (float) distance / mpg; //casts int distance as a float odometer = odometer + distance; } } public void printCurrentStatus() { System.out.println("Current Status"); System.out.println("==========================================================="); System.out.println("odometer \t : " + odometer ); System.out.println("fuel gauge \t : " + fuelGauge); System.out.println("===========================================================\n"); } /*Dr Alsabbagh's test cases here */ public static void testing() { Auto myCar = new Auto(); // instantiate an automobile object named myCar // myCar.addFuel(12); myCar.drive(100); myCar.printCurrentStatus(); myCar.drive(100); myCar.printCurrentStatus(); myCar.drive(150); myCar.printCurrentStatus(); myCar.addFuel(13); myCar.printCurrentStatus(); myCar.addFuel(10); myCar.printCurrentStatus(); // Auto yourCar = new Auto(1000, 16); // instantiate an automobile object named yourCar Auto herCar = new Auto(1000, 10); // instantiate an automobile object named herCar herCar.printCurrentStatus(); } }