hypesystem

GD - Shape.java

Nov 1st, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package gd;
  2.  
  3. /**
  4.  * This is a superclass: A shape. Cirlce, Rectangle and Triangle inherit from
  5.  * it.
  6.  * This class was from the beginning defined (by me) as an abstract class. This
  7.  * is because this class should never exist: You can't just add a "shape". It
  8.  * has to be a certain kind of shape (as "shape" doesn't tell you anything about
  9.  * it's properties).
  10.  * A new class has been defines: Coordinate. This holds an x and a y coordinate
  11.  * for the movable classes (making the coordinate set returnable in one state-
  12.  * ment).
  13.  * @author hypesystem
  14.  * @email  [email protected]
  15.  */
  16. public abstract class Shape implements Movable {
  17.     protected Coordinate coordinates;
  18.    
  19.     /**
  20.      * Creates a Shape.
  21.      * @param coordinates a coordinate object holding the shapes position
  22.      */
  23.     public Shape(Coordinate coordinates) {
  24.         if(coordinates != null) this.coordinates = coordinates;
  25.         else this.coordinates = new Coordinate(0,0);
  26.     }
  27.    
  28.     /**
  29.      * Returns the area of the shape
  30.      * @return area of shape
  31.      */
  32.     abstract double area();
  33.    
  34.     /**
  35.      * Returns circumference of shape
  36.      * @return circumference of shape
  37.      */
  38.     abstract double circumference();
  39.    
  40.     /**
  41.      * Counts simple shapes in the shape
  42.      * @return simple shapes in shape
  43.      */
  44.     abstract int countSimple();
  45.    
  46.     /**
  47.      * Moves the shape's coordinates. Inherited from Movable interface.
  48.      * @param dx distance to move horizontally
  49.      * @param dy distance to move vertically
  50.      */
  51.     @Override
  52.     public void move(int dx, int dy) {
  53.         if(coordinates != null)
  54.             coordinates.set(coordinates.getX() + dx,coordinates.getY() + dy);
  55.     }
  56.    
  57.     /**
  58.      * Gets the coordinates of the shape in a Coordinate-object. Inherited from
  59.      * Movable interface
  60.      * @return coordinates of shape
  61.      */
  62.     @Override
  63.     public Coordinate getCoordinate() {
  64.         return coordinates;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment