Jan 02

Speedboat.java Illustrates inheritance from Ship class

*****************************
Speedboat.java Illustrates inheritance from Ship class. See SpeedboatTest.java for a test.
*****************************
/** A fast Ship. Red and going 20 knots by default. 
 *
 ***********************
public class Speedboat extends Ship {
  private String color = "red";

  /** Builds a red Speedboat going N at 20 knots. */
  
  public Speedboat(String name) {
    super(name);
    setSpeed(20);
  }

  /** Builds a speedboat with specified parameters. */
  
  public Speedboat(double x, double y, double speed,
                   double direction, String name,
                   String color) {
    super(x, y, speed, direction, name);
    setColor(color);
  }

  /** Report location. Override version from Ship. */
  
  public void printLocation() {
    System.out.print(getColor().toUpperCase() + " ");
    super.printLocation();
  }
  
  /** Gets the Speedboat's color. */
  
  public String getColor() {
    return(color);
  }

  /** Sets the Speedboat's color. */
  
  public void setColor(String colorName) {
    color = colorName;
  }
}
**********************
SpeedboatTest.java 
**********************
/** Try a couple of Speedboats and a regular Ship. 
 *
*****************************

public class SpeedboatTest {
  public static void main(String[] args) {
    Speedboat s1 = new Speedboat("Speedboat1");
    Speedboat s2 = new Speedboat(0.0, 0.0, 2.0, 135.0,
                                 "Speedboat2", "blue");
    Ship s3 = new Ship(0.0, 0.0, 2.0, 135.0, "Ship1");
    s1.move();
    s2.move();
    s3.move();
    s1.printLocation();
    s2.printLocation();
    s3.printLocation();
  }
}
*****************************

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Skip to toolbar