All pastes #1626195 Raw Edit

Triangle

public java v1 · immutable
#1626195 ·published 2009-10-18 01:17 UTC
rendered paste body
/** * Create a class Triangle that can calculate length, angles, perimeter, and area. */public class Triangle{    /**     * A constructor that uses formulas to calculate lengths and angles.     * @param x1 The coordinate location along the x-axis for the first vertice of the triangle.     * @param y1 The coordinate location along the y-axis for the first vertice of the triangle.     * @param x2 The coordinate location along the x-axis for the second vertice of the triangle.     * @param y2 The coordinate location along the y-axis for the second vertice of the triangle.     * @param x3 The coordinate location along the x-axis for the third vertice of the triangle.     * @param y3 The coordinate location along the y-axis for the third vertice of the triangle.     */    public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)    {        sideAB = Math.sqrt(Math.pow(Math.abs(x2 - x1), 2) + Math.pow(Math.abs(y2 - y1), 2));                sideBC = Math.sqrt(Math.pow(Math.abs(x3 - x2), 2) + Math.pow(Math.abs(y3 - y2), 2));                sideAC = Math.sqrt(Math.pow(Math.abs(x3 - x1), 2) + Math.pow(Math.abs(y3 - y1), 2));                angleC = Math.toDegrees(Math.acos((sideAB * sideAB + sideBC * sideBC - sideAC * sideAC) / (2 * sideAB * sideBC)));                angleA = Math.toDegrees(Math.asin((sideAB * Math.sin(angleC)) / sideAC));                angleB = 180 - angleC - angleA;    }    /**     * Returns the calculated length of side AB.     */    public double getLengthAB()    {        return sideAB;    }        /**     * Returns the calculated length of side BC.     */    public double getLengthBC()    {        return sideBC;    }        /**     * Returns the calculated length of side AC.     */    public double getLengthAC()    {        return sideAC;    }        /**     * Calculates and returns the perimeter of the triangle.     */    public double getPerimeter()    {        double perimeter = sideAB + sideBC + sideAC;        return perimeter;    }        /**     * Calculates the area of the triangle using Heron's formula and returns it.     */    public double getArea()    {        double z = (sideAB + sideBC + sideAC) / 2;        double area = Math.sqrt((z)*(z - sideAB)*(z - sideBC)*(z - sideAC));        return area;    }        /**     * Returns the calculated measurement of angle A in degrees.     */    public double getAngleA()    {        return angleA;    }        /**     * Returns the calculated measurement of angle B in degrees.     */    public double getAngleB()    {        return angleB;    }        /**     * Returns the calculated measurement of angle C in degrees.     */    public double getAngleC()    {        return angleC;    }        private double sideAB;    private double sideBC;    private double sideAC;    private double angleA;    private double angleB;    private double angleC;}