hypesystem

GD - Triangle.java

Nov 1st, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package gd;
  2.  
  3. /**
  4.  * A specific shape: A triangle.
  5.  * This triangle inherits from shape
  6.  * @author hypesystem
  7.  * @email  [email protected]
  8.  */
  9. public class Triangle extends Shape {
  10.     private double a, b, c;
  11.    
  12.     /**
  13.      * Creates a triangle witout a coordinate argument (sets coordinates to 0,0)
  14.      * @param a side of triangle
  15.      * @param b side of triangle
  16.      * @param c side of triangle
  17.      */
  18.     public Triangle(double a, double b, double c) {
  19.         super(new Coordinate(0,0));
  20.         setAttr(a,b,c);
  21.     }
  22.    
  23.     /**
  24.      * Creates a triangle with a coordinate argument.
  25.      * @param a side of triangle
  26.      * @param b side of triangle
  27.      * @param c side of triangle
  28.      * @param coordinates coordinates of triangle
  29.      */
  30.     public Triangle(double a, double b, double c, Coordinate coordinates) {
  31.         super(coordinates);
  32.         setAttr(a,b,c);
  33.     }
  34.    
  35.     /**
  36.      * Sets the attributes of the Triangle. Called from constructors (hence
  37.      * private)
  38.      * @param a side of triangle
  39.      * @param b side of triangle
  40.      * @param c side of triangle
  41.      */
  42.     private void setAttr(double a, double b, double c) {
  43.         this.a = a;
  44.         this.b = b;
  45.         this.c = c;
  46.     }
  47.    
  48.     /**
  49.      * Calculates and returns the circumference of the triangle. Inherited from
  50.      * Shape.
  51.      * @return circumference of triangle
  52.      */
  53.     @Override
  54.     public double circumference() {
  55.         return a + b + c;
  56.     }
  57.    
  58.     /**
  59.      * Calculates and returns the area of the triangle. Inherited from Shape.
  60.      * @return area of triangle
  61.      */
  62.     @Override
  63.     public double area() {
  64.         double s = (a + b + c) / 2;
  65.         return Math.sqrt(s * (s - a) * (s - b) * (s - c));
  66.     }
  67.    
  68.     /**
  69.      * Counts the amount of simple shapes in this shape. This is a simple shape.
  70.      * Inherited from Shape.
  71.      * @return 1
  72.      */
  73.     @Override
  74.     public int countSimple() {
  75.         return 1;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment