Triangle

In this program we'll continue our geometric scheme with a Triangle class, where Triangles will be defined as a collection of three XYPoints.

  1. Go into the CS labs (see this map for details) and log onto a Windows machine.
  2. Start up a new project, called Triangle, with a main class called Main.java.
  3. In the blank file Main.java, write the following code:
  4. public class Main {
    
    	public static void main(String[] args) {
    
    	}
    
    }
    
  5. Add XYPoint.java to the project, from the last two exercises.
  6. Create another new blank text file called Triangle.java, and add it to the project.
  7. In Triangle.java, type in the following code:
  8. public class Triangle {
    
    	private final XYPoint p1;
    	private final XYPoint p2;
    	private final XYPoint p3;
    	private final double side12;
    	private final double side23;
    	private final double side31;
    
    	public Triangle(XYPoint p1, XYPoint p2, XYPoint p3) {
    		this.p1 = p1;
    		this.p2 = p2;
    		this.p3 = p3;
    		side12 = XYPoint.distance(p1,p2);
                    side23 = XYPoint.distance(p2,p3);
                    side31 = XYPoint.distance(p3,p1);
    	}
    
    	private Triangle(Triangle original) {
    		this(original.p1,original.p2,original.p3);
    	}
    
    	public Triangle copy() {
    		return new Triangle(this);
    	}
    
    	public double getPerimeter() {
    		return side12 + side23 + side31;
    	}
    
    	public double getArea() {
    		double s = .5 * getPerimeter();
    		return Math.sqrt( s * (s - side12) * (s - side23) * (s - side31) );
    	}
    
    	public boolean isIsoceles() {
    		return side12 == side23 ||
    		       side23 == side31 ||
    		       side31 == side12;
    	}
    
    	public boolean isEquilateral() {
    		return side12 == side23 && side23 == side31;
    	}
    
    	public boolean isRight() {
    		return Math.pow(side12,2) + Math.pow(side23,2) == Math.pow(side31,2) ||
    		       Math.pow(side23,2) + Math.pow(side31,2) == Math.pow(side12,2) ||
    		       Math.pow(side31,2) + Math.pow(side12,2) == Math.pow(side23,2);
    	}
    
    	public String toString() {
    		return "a Triangle with vertices at " + 
    		        p1 + ", " + p2 + ", and " + p3 + 
    		       " and sides of lengths " +
    		        side12 + ", " + side23 + ", and " + side31;
    	}
    
    }
    
  9. Now go back to Main.java, and add the following code:
  10. public class Main {
    
            public static void main(String[] args) {
    
                    XYPoint a = new XYPoint(1,-1);
                    XYPoint b = new XYPoint(-1,5);
                    XYPoint c = new XYPoint(-3,1);
                    Triangle t = new Triangle(a,b,c);
                    System.out.println(t);
    
                    System.out.println("perimeter = " + t.getPerimeter());
                    System.out.println("area = " + t.getArea());
                    System.out.println("is this a right triangle? " + t.isRight());
                    System.out.println("is this an isoceles? " + t.isIsoceles());
                    System.out.println("is this an equilateral triangle? " + t.isEquilateral());
    
                    Triangle s = t.copy();
                    t = null;
                    System.out.println(t);
                    System.out.println(s);
    
    	}
    
    }
    
  11. Compile and run your program. You should get the following output:
  12. a Triangle with vertices at (1,-1), (-1,5), and (-3,1) and sides of lengths
    6.324555320336759, 4.47213595499958, and 4.47213595499958
    perimeter = 15.26882723033592
    area = 10.000000000000005
    is this a right triangle? true
    is this an isoceles? true
    is this an equilateral triangle? false
    null
    a Triangle with vertices at (1,-1), (-1,5), and (-3,1) and sides of lengths
    6.324555320336759, 4.47213595499958, and 4.47213595499958
    
  13. Press enter, as the console window says, and the black console window will disappear.