Free Response Practice Question (with solution)- Right-angled Triangle Project


<-- Back to Zodiac Sign Project Home to Additional FRQ Practice-->





Right-angled Triangle Project


Part 1: Point.java

Write the complete class definition for the Point class. The Point class consists of the following:


Additionally,


Part 2: Triangle.java

Write the complete class definition for the Triangle class. The Triangle class consists of the following:


Additionally,


Part 3: TriangleRunner.java

Write the complete class definition for the runner class named TriangleRunner class, that makes use of the Triangle class. The main method of the TriangleRunner class should do the following:


Note:


Sample input:

Create triangle with points P1(0, 0), P2(0, 3), P3(4, 0)

This triangle is right-triangle.


Create triangle with points P1(6, 1), P2(0, 4), P3(-1, -7)

This triangle is NOT right-triangle.


Solution:


Part 1: Point.java

public class Point {

    int x;
    int y;
    
    public Point()
    {
        this.x = 0;
        this.y = 0;
    }
    
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public int getX()
    {
        return x;
    }
    
    public int getY()
    {
        return y;
    }
    
    public void setX(int x)
    {
        this.x=x;
    }
    
    public void setY(int y)
    {
        this.y=y;
    }
}


Part 2:Triangle.java


public class Triangle {

    Point p1;
    Point p2;
    Point p3;
    
    public Triangle()
    {
        p1 = new Point();
        p2 = new Point();
        p3 = new Point();
    }
    
    public Triangle(Point p1, Point p2, Point p3)
    {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }
    
    public boolean isRightTriangle()
    {
        if (!genericTriangle())
        {
        double d1 = getDistance(p1, p2);
        double d2 = getDistance(p2, p3);
        double d3 = getDistance(p1, p3);

        // check if any pair fulfills Pythagorean Theorem
        if ((d1*d1) + (d2*d2) == (d3 * d3))
            return true;
        else
            if ((d1*d1) + (d3*d3) == (d2 * d2))
                return true;
            else
                if ((d2*d2) + (d3*d3) == (d1 * d1))
                    return true;
        }
         return false;
    }
    
    private double getDistance(Point a, Point b)
    {
        double n1 = (b.getX()-a.getX()) * (b.getX()-a.getX());
        double n2 = (b.getY()-a.getY()) * (b.getY()-a.getY());
        
        return Math.sqrt(n1+n2);
                
    }
    
    private boolean genericTriangle()
    {
        if ((p1.x == 0) && (p1.y == 0) && (p2.x == 0) && (p2.y == 0) && (p3.x == 0) && (p3.y == 0))
        return true;
        else return false;
    }
    
}


Part 3: TriangleRunner.java


public class TriangleRunner {

    public static void main(String args[])
    {
    Triangle genTri = new Triangle();
    System.out.println("Is genTri right triangle or not?: "+ genTri.isRightTriangle());
    
    Triangle oneTri = new Triangle(new Point(0, 0), new Point(0, 3), new Point(4, 0));
    System.out.println("Is oneTri right triangle or not?: "+ oneTri.isRightTriangle());
    
    Triangle twoTri = new Triangle(new Point(0, 5), new Point(0, 3), new Point(4, 1));
    System.out.println("Is twoTri right triangle or not?: "+ twoTri.isRightTriangle());

    }
}




Java project files (with Runner code):


<-- Back to Zodiac Sign Project Home to Additional FRQ Practice-->