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:
- int x: represents the x coordinate of a point.
- int y: represents the y coordinate of a point.
Additionally,
- The Point class contains a default constructor, that initializes a generic point with all value set to 0 (zero).
- The class also consists of a parameterised constructor Point (int x, int y), to initialize the x and y coordinates of a point to the given values.
- Setter and getter methods for all the properties of the Point class.
Part 2: Triangle.java
Write the complete class definition for the Triangle class. The Triangle class consists of the following:
- Point p1: represents the first point coordinate of the triangle object.
- Point p2: represents the second point coordinate of the triangle object.
- Point p3: represents the third point coordinate of the triangle object.
Additionally,
- The Triangle class contains a default constructor, that initializes a generic triangle with all sides set to a value of 0 (zero).
- A parameterised constructor, Triangle(Point p1, Point p2, Point p3), to create a triangle using the values given by the user.
- A method (isRightTriangle()) to check whether the triangle is a right-angled triangle or not.
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:
- Use the generic constructor to initialize a triangle object (genTri).
- Use the parameterised constructors to initialize few triangle objects.
- Use the helper method (isRightTriangle()) to check if the triangle is a right-triangle or not.
Note:
- Use the formula given below to calculate the distance between two points on the coordinate plane:
- Calculate the distance between all the three points of the triangle. This will give you the three sides of the triangle.
- Use this to calculate whether any combination of the three sides follow the Pythagorean Theorem.
- If yes, the triangle is a right-triangle, else not.
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--> |