/* AUTHOR: Dr. Sandra Bartlett GSI: COURSE: EECS/CS 285 PROJECT: OO Design DUE DATE: NA SUBMISSION DATE: 5/7/03 SUMMARY This class provides the basis for creating a wide range of virtual boats. INPUT None OUTPUT None CLASS HIERARCHY Object - Boat ASSUMPTIONS None */ import java.awt.*; abstract public class Boat { public static final String SPEED_UNITS = " kilometers per hour"; protected Point location; // where to draw the boat private Dimension dimension; private Color color; // color to draw the boat private int capacity; private int maxSpeed; public Boat() { this(new Dimension(100, 50), Color.magenta, 4, 2, new Point()); } // default constructor public Boat(Dimension size, Color color) { if (size != null && size.width > 0 && size.height > 0) dimension = size; else dimension = new Dimension(100, 50); if (color != null) this.color = color; else this.color = Color.red; location = new Point(); capacity = size.width / 5; maxSpeed = this.color == Color.red ? 10 : 5; } // constructor public Boat(int width, int height, Color color) { this(new Dimension(width, height), color); } // constructor public Boat(Dimension size, Color col, int cap, int speed) { this(size, col, cap, speed, new Point()); } // constructor public Boat(Dimension d, Color c, int cap, int speed, Point p) { if (d != null && d.width > 0 && d.height > 0) dimension = d; else dimension = new Dimension((int)(Math.random() * 250) + 50, (int)(Math.random() * 150) + 50); if (c != null) color = c; else color = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256)); if (cap > 0) capacity = cap; else capacity = dimension.width / 5; if (speed > 0) maxSpeed = speed; else maxSpeed = (int)(Math.random() * 5) + 1; if (p != null) location = p; else location = new Point(); } // constructor - all values abstract public void draw(Graphics g); public int getCapacity() { return capacity; } // getCapacity public Dimension getDimension() { return dimension; } // getDimension public Color getColor() { return color; } // getColor public void setColor(Color newColor) { if (newColor != null) color = newColor; } // setColor public int getMaxSpeed() { return maxSpeed; } // getMaxSpeed public void setMaxSpeed(int maxSpeed) { if (maxSpeed > 0) this.maxSpeed = maxSpeed; } // setMaxSpeed public Point getLocation() { return location; } // getLocation /* Note to the student - instead of writing a setLocation() method, I chose to change the location of the Boat using move methods. "Moving" is more like how boats change location in the real world, and this way I can define 2 ways of moving - absolute (moveTo - like "go to Argo Boat Livery") and relative (move - like "go 500 feet west then 400 feet north"). This is just a design decision. I could have written a set method. */ // move the Boat to the specified location public void moveTo(int toX, int toY) { location.translate(toX - location.x, toY - location.y); } // moveTo // move the Boat the specified number of pixels in each direction public void move(int deltaX, int deltaY) { location.translate(deltaX, deltaY); } // move } // Boat