import java.util.Stack; /** * A second attempt at creating a VehicleStack by "wrapping" the Stack * type and forwarding its method calls. *
* This class provides type checking during compile time for all stack methods. * This type checking insures that an Object which is not a Vehicle cannot be * used with any of the VehicleStack methods. If an attempt is made to * use a non-Vehicle type with a VehicleStack method, the program will fail to * compile and the compiler indicate a type check error. *
* This is know as strong type checking. However, our class no longer * extends the Stack class, so the following code will not compile *
* Stack s = new VehicleStack(); * * s.push(new Vehicle()); * Vehicle v = s.pop(); ** Since VehicleStack no longer extends Stack we cannot use it in place of a * an ordinary stack. * * @version 2.0 * @author John Kloss */ public class VehicleStackTwo { Stack s = new Stack(); /** * Creates a new instance of VehicleStackTwo. */ public VehicleStackTwo() { } /** * Tests if this VehicleStack is empty. * * @return Returns
true if this VehicleStack is
* empty; false otherwise.
*/
public boolean empty() {
return s.empty();
}
/**
* Looks at the Vehicle at the top of this VehicleStack without removing
* it from the stack.
*
* @return The Vehicle at the top of this VehicleStack.
*
* @exception EmptyStackException Thrown if this VehicleStack is
* empty.
*/
public Vehicle peek() {
return (Vehicle) s.peek();
}
/**
* Removes the Vehicle at the top of this VehicleStack and returns that
* Vehicle.
*
* @return The Vehicle at the top of this VehicleStack.
*
* @exception EmptyStackException Thrown if this VehicleStack is
* empty.
*/
public Vehicle pop() {
return (Vehicle) s.pop();
}
/**
* Pushes a Vehicle onto the top of this stack.
*
* @param obj A Vehicle to be pushed onto this stack.
*
* @return The Vehicle argument.
*/
public Vehicle push(Vehicle obj) {
return (Vehicle) s.push(obj);
}
/**
* Returns where a Vehicle is on this stack.
*
* @param obj A Vehicle to search for in this stack.
*
* @returns The distance from the top of this stack where the
* Vehicle is located. A return value of -1 indicates
* that the Vehicle is not on this stack.
*/
public int search(Vehicle obj) {
return s.search(obj);
}
}