// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

//----------------------------------------------------
/**
 * Ship example to demonstrate OOP in Java.
 *
 * @author <A HREF="mailto:hall@apl.jhu.edu">
 *         Marty Hall</A>
 * @version 1.0
 */

public class Ship {
  //----------------------------------------------------
  // Instance variables
  
  private double x=0.0, y=0.0, speed=1.0, direction=0.0;
  private String name;

  //----------------------------------------------------
  // Constructors
  
  /** Build a ship with specified parameters. */
  
  public Ship(double x, double y,
              double speed, double direction,
              String name) {
    setX(x);
    setY(y);
    setSpeed(speed);
    setDirection(direction);
    setName(name);
  }

  /** Build a ship with default values
   *  (x=0, y=0, speed=1.0, direction=0.0).
   */
  public Ship(String name) {
    setName(name);
  }

  //----------------------------------------------------
  /** Move ship one step at current speed/direction. */
 
  public void move() {
    moveInternal(1);
  }
  
  /** Move N steps. */
  
  public void move(int steps) {
    moveInternal(steps);
  }

  private void moveInternal(int steps) {
    double angle = degreesToRadians(direction);
    x = x + (double)steps * speed * Math.cos(angle);
    y = y + (double)steps * speed * Math.sin(angle);
  }

  private double degreesToRadians(double degrees) {
    return(degrees * Math.PI / 180.0);
  }
  
  //----------------------------------------------------
  /** Report location to standard output. */
  
  public void printLocation() {
    System.out.println(getName() + " is at (" + getX()
                       + "," + getY() + ").");
  }

  //----------------------------------------------------
  /** Get current X location. */
  
  public double getX() {
    return(x);
  }

  /** Set current X location. */
  
  public void setX(double x) {
    this.x = x;
  }

  //----------------------------------------------------
  /** Get current Y location. */
  
  public double getY() {
    return(y);
  }

  /** Set current Y location. */
  
  public void setY(double y) {
    this.y = y;
  }

  //----------------------------------------------------
  /** Get current speed. */
  
  public double getSpeed() {
    return(speed);
  }

  /** Set current speed. */
  
  public void setSpeed(double speed) {
    this.speed = speed;
  }

  //----------------------------------------------------
  /** Get current heading (0=East, 90=North, 180=West,
   *  270=South). Ie uses standard math angles,
   *  <B>not</B> nautical system where 0=North,
   *  90=East, etc.
   */
  public double getDirection() {
    return(direction);
  }

  /** Set current direction (0=East, 90=North, 180=West,
   *  270=South). Ie uses standard math angles,
   *  <B>not</B> nautical system where 0=North,
   *  90=East, etc.
   */
  public void setDirection(double direction) {
    this.direction = direction;
  }

  //----------------------------------------------------
  /** Get Ship's name. Can't be modified by user. */
  
  public String getName() {
    return(name);
  }

  private void setName(String name) {
    this.name = name;
  }

  //----------------------------------------------------
}

