Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to solve the Triangle problem. Define a class of objects called

ID: 3854295 • Letter: W

Question

Write a program to solve the Triangle problem.

Define a class of objects called Triangle. A Triangle should be a set of three Lines (which for the purpose of this problem should be a very adequate representation). However, a Triangle is created by specifying three Points (which are located in the plane as discussed above). UsingHeron's formula every Triangle should be able to calculate and report its area. (If the three Points are collinear the Triangle is extremely flat, its area is 0 (zero), and that should be acceptable.) Here's an example:

should produce:

Explanation / Answer

Create two more classess

Point.java

public class Point {
int x,y; //variable to store co-ordinates
public Point(int x,int y){
this.x=x;
this.y=y;
}
}

Triangle.java

public class Triangle {
double areaOfTriangle=0;//variable to store area
public Triangle(Point A,Point B,Point C){//parameterized constructor
//find length of sides of triangle
areaOfTriangle = Math.abs((A.x*(B.y -C.y) + B.x*(C.y -A.y) + C.x*(A.y - B.y))/2);//calculating area of triangle
  
}
public String area(){
return ("Area is: " + areaOfTriangle);//returning triangle area
}
  
}

Experiment.java

class Experiment {
public static void main(String[] args) {
Triangle a = new Triangle(new Point(0, 0),
new Point(0, 3),
new Point(4, 0));
System.out.println(a.area()); // prints 3 * 4 / 2 (that is: 6 (six))
}
}

Output:

Area is: 6.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote