Intro to Java 30-IT-396 |
Example
/**
*
* @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(50, 8, 4, 5, 68);
// Make an ArrayList to hold our vehicles. Put them in it.
ArrayList cars = new ArrayList();
cars.add(car1);
cars.add(car2);
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++) {
// 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(10);
}
}
// Instantiate and start
a Controller object.
public static void main(String[]
args) {
Controller controller = new Controller();
controller.begin();
}
}
/*
* Vehicle.java
*
* Created on March 4, 2003, 11:36 PM
*/
/**
*
* @author default
*/
public abstract class Vehicle {
// Attributes specific
to class Vehicle and its subclasses
protected int gallonsOfGas;
protected int location;
protected int mpg;
protected int miles;
protected int numberOfTires;
// Constructor to initialize
attributes.
public Vehicle(int gallonsOfGas,
int mpg) {
this.gallonsOfGas = gallonsOfGas;
this.mpg = mpg;
}
// Go method for Vehicle
and its subclasses
public void go(int distance)
{
location += distance;
gallonsOfGas = gallonsOfGas - (distance/mpg);
System.out.println("Travelled " + distance);
System.out.println("Gallons of Gas remaining: " + gallonsOfGas);
}
}
/*
* Car.java
*
* Created on March 4, 2003, 11:37 PM
*/
/**
*
* @author default
*/
// A subclass of Vehicle
public class Car extends Vehicle {
// Attributes specific
to class Car and its subclasses.
protected int numberOfDoors;
protected int numberOfGears;
// Constructor for Car.
// Initializes variables
and calls constructor for Vehicle.
public Car(int gallonsOfGas,
int numberOfDoors, int numberOfGears, int mpg)
{
super(gallonsOfGas, mpg);
this.numberOfDoors = numberOfDoors;
this.numberOfGears = numberOfGears;
numberOfTires = 4;
}
}
/*
* Hybrid.java
*
* Created on March 4, 2003, 11:39 PM
*/
/**
*
* @author default
*/
// Class Hybrid extends Car
public class Hybrid extends Car {
// Attributes specific
to Hybrid
protected int kwhRemaining;
protected int mpkwh;
protected boolean electricMode;
protected int minkwh;
// Constructor for Hybrid.
// Initializes attributes
and calls cosntructor for Car.
public Hybrid(int kwhRemaining,
int gallonsOfGas, int numberOfDoors, int numberOfGears, int mpg) {
super(gallonsOfGas, numberOfDoors, numberOfGears, mpg);
this.kwhRemaining = kwhRemaining;
electricMode = true;
mpkwh = 2;
}
// go() method overrides
go() method of Vehicle superclass.
public void go(int distance)
{
if (electricMode) {
System.out.println("Travelling via electric mode");
location += distance;
kwhRemaining = kwhRemaining - (distance * mpkwh);
if (kwhRemaining < minkwh) {
electricMode = false;
}
System.out.println("KWH remaining: " + kwhRemaining);
} else {
super.go(distance);
}
}
// Method specific to class
Hybrid
public void setElectricMode(boolean
electricMode) {
this.electricMode = electricMode;
}
// Method specific to class
Hybrid
public boolean isElectricMode()
{
return electricMode;
}
}
/*
* Cavalier.java
*
* Created on March 4, 2003, 11:38 PM
*/
/**
*
* @author default
*/
// This class extends Car.
public class Cavalier extends Car {
static int cavMpg = 30;
public Cavalier(int gallonsOfGas,
int numberOfDoors, int numberOfGears)
{
super (gallonsOfGas, numberOfDoors, numberOfGears, cavMpg);
}
}
Results:
Travelled 10
Gallons of Gas remaining: 6
Travelling via electric mode
KWH remaining: 30