/** * Base class for all Shapes, containing basic geometry common to all * shapes. A starting point for extension by other classes. * @author wokoun */ public abstract class Shape { /** Maximum value */ public static final double UPPER_BOUND = 1000.0; /** X-coordinate of the shape */ private double x; /** Y-coordinate of the shape */ private double y; /** * Construct a new shape instance * @param x X-coordinate, must be between 0 and 1000 * @param y Y-coordinate, must be between 0 and 1000 * @throws IllegalArgumentException if a parameter causes the shape to extend * out of bounds */ public Shape(double x, double y) { setX(x); setY(y); } /** * Get the X-coordinate * @return X-coordinate, between 0 and UPPER_BOUND */ public double getX() { return this.x; } /** * Set the X-coordinate * @param x X-coordinate, must be between 0 and UPPER_BOUND * @throws IllegalArgumentException if a parameter causes the shape to extend * out of bounds */ public void setX(double x) { if (x < 0.0) throw new IllegalArgumentException("X cannot be less than zero"); if (x > UPPER_BOUND) throw new IllegalArgumentException("X cannot be greater than " + UPPER_BOUND); this.x = x; } /** * Get the Y-coordinate * @return Y-coordinate, between 0 and UPPER_BOUND */ public double getY() { return this.y; } /** * Set the Y-coordinate * @param y Y-coordinate, must be between 0 and UPPER_BOUND * @throws IllegalArgumentException if a parameter causes the shape to extend * out of bounds */ public void setY(double y) { if (y < 0.0) throw new IllegalArgumentException("Y cannot be less than zero"); if (y > UPPER_BOUND) throw new IllegalArgumentException("Y cannot be greater than " + UPPER_BOUND); this.y = y; } /** * Calculate the area of the shape * @return Area in square units */ public abstract double area(); public String toString() { return "area=" + area() + ",x=" + this.x + ",y=" + this.y; } public static void main(String[] args) { Ellipse e = new Ellipse(10.0, 50.0, 75.0, 110.0); System.out.println(e); } }