Intro to Java 30-IT-396

Example: Add Four Wheel Drive to Car


First, our interface.

/*
 * FourWheelDrive.java
 *
 * Created on March 5, 2003, 12:54 AM
 */

/**
 *
 * @author  default
 */
public interface FourWheelDrive {

    public void fourWheelDriveOn();
 
    public void fourWheelDriveOff();
 
}

Now, our class Talon.

/*
 * Talon.java
 *
 * Created on March 5, 2003, 1:00 AM
 */

/**
 *
 * @author  jonesbr@email.uc.edu
 */
public class Talon extends Car implements FourWheelDrive {

    // true when on, false when off.
    boolean fourWheelDrive = false;
    static int talonMpg = 20;
 
    /** Creates a new instance of Talon */
    public Talon(int gallonsOfGas, int numberOfDoors, int numberOfGears)
    {
        super (gallonsOfGas, numberOfDoors, numberOfGears, talonMpg);
    }
 
 

    public void fourWheelDriveOff() {
        if (fourWheelDrive) {
            //increase the mpg back to normal, but only if four wheel drive was previously off.
            mpg += 5;
        }
        // change fourWheelDrive to off again.
        fourWheelDrive = false;
    }
 
    public void fourWheelDriveOn() {
        if (!fourWheelDrive) {
            System.out.println("Turning 4wd on, previously off.");
            // decrease mpg by 5 when turned on.
            mpg = mpg -5;
        }
        // turn it on.
        fourWheelDrive = true;
    }
 
}
 
 
 

Changes to Controller

/*
 * Controller.java
 *
 * Created on March 4, 2003, 11:35 PM
 */

/**
 *
 * @author  default
 */

// must import this class to use it.
import java.util.ArrayList;

// Class controller uses the Vehicle classes.
public class Controller {

    // Default constructor.
    public Controller() {
    }

    // Begin method instantiates two vehicles and passes them to run.
    public void begin() {
        // instantiate our two vehicles.
        Cavalier car1 = new Cavalier(6, 2, 4);
        Hybrid car2 = new Hybrid(500, 8, 4, 5, 68);
 
        // Add a few Talons.
        Talon car3 = new Talon(10, 2, 4);
        Talon car4 = new Talon(10, 2, 4);
        Talon car5 = new Talon(10, 2, 4);
 
        // Tell car 4 it is going to be in mixed terrain.
        // Note the use of polymorphism with an interface.
        mixedTerrain(car4);
 
 
        // Turn four wheel drive on for car 5 as well.
        // This does the same thing as the method above, but
        // 1) it does not use polymorphism, and
        // 2) it demonstrates that we can call the method directly instead of using polymorphism.
        car5.fourWheelDriveOn();
 
        // Make an ArrayList to hold our vehicles.  Put them in it.
        ArrayList cars = new ArrayList();
        cars.add(car1);
        cars.add(car2);
 
        // Add the talons to our ArrayList.
        cars.add(car3);
        cars.add(car4);
        cars.add(car5);
 
        run(cars);

    }

    // run executes the go() method on the vehicle passed in through the ArrayList.
    public void run(ArrayList vehs) {
        // for loop iterates through the ArrayList.
        for (int i = 0; i < vehs.size(); i++) {
            // For informational purposes only.
            System.out.println("Car: " + (i + 1));
            // get the Vehicle out of the ArrayList and cast it from an object to a vehicle.
            Vehicle v = (Vehicle) vehs.get(i);
            // tell it to go 10 units.
            v.go(100);
        }
    }

    public void mixedTerrain(FourWheelDrive fwd) {
        fwd.fourWheelDriveOn();
    }
 
    // Instantiate and start a Controller object.
    public static void main(String[] args) {
        Controller controller = new Controller();
        controller.begin();
    }

}
 

All other classes remain the same.

Results:
Notice that, even though car3, car4, and car5 are all Talons, car4 and car5 have 1 less gallon of gas, because we turned four wheel drive on.

Turning 4wd on, previously off.
Turning 4wd on, previously off.
Car: 1
Travelled 100
Gallons of Gas remaining: 3
Car: 2
Travelling via electric mode
KWH remaining: 300
Car: 3
Travelled 100
Gallons of Gas remaining: 5
Car: 4
Travelled 100
Gallons of Gas remaining: 4
Car: 5
Travelled 100
Gallons of Gas remaining: 4