I need to implement methods of class Triangle:
constructor, which has three points as parameters.
Make sure that these points refer to vertices of the triangle.
Ensure that the created triangle exists and it is not degenerative.
If it is, use throw new IllegalArgumentException() to raise an error.
double area()
Return the area of the triangle.
Point centroid()
Return the centroid of the triangle.
Class Point is already there.
Examples
You may use Main class to try your code. There are some examples below.
Sample code:
<pre lang="Java">
...
new Triangle(new Point(0,0), new Point(1, 0), new Point(2, 0));
Result: Exception because such a triangle would be degenerative.
Sample code:
...
double area = new Triangle(new Point(0,0), new Point(3, 0), new Point(0, 4)).area();
System.out.println(area);
Output:
6
Sample code:
...
Point centroid = new Triangle(new Point(0,0), new Point(3, 0), new Point(0, 3)).centroid();
System.out.println(centroid.getX());
System.out.println(centroid.getY());
Output:
1
1
This is the Point class:
class Point {
private double x;
private double y;
public Point(final double x, final double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
This is the Triangle class:
class Triangle {
Point a;
Point b;
Point c;
public Triangle(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
}
public double area() {
double area = Math.abs((a.getX() - c.getX()) * (b.getY() - a.getY()) - (a.getX() - b.getX()) * (c.getY() - a.getY())) /2;
return area;
}
public Point centroid(){
double x = (a.getX() + b.getX() + c.getX()) / 3;
double y = (a.getY() + b.getY() + c.getY()) / 3;
return new Point(x, y);
}
}
This is the Main class:
public class Main {
public static void main(String[] args) {
{
double area = new Triangle(new Point(0, 0), new Point(0, 2), new Point(0, 5)).area();
System.out.println((int) area);
}
{
Point centroid = new Triangle(new Point(0, 0), new Point(3, 0), new Point(0, 3)).centroid();
System.out.println((int) centroid.getX());
System.out.println((int) centroid.getY());
}
}
}
What I have tried:
I don;t know how to implement this statement: "Ensure that the created triangle exists and it is not degenerative.
If it is, use throw new IllegalArgumentException() to raise an error."
What I understand is that if triangle is degenerative I must throw new IllegalArgumentException().
I googled about that and I found that if a, b, and c are the lengths of the three sides of a triangle, then a + b > c, a + c > b, b + c > a. If any one of these inequalities is not true, then we get a degenerate triangle.
I tried to put in constructor like this:
<pre>public Triangle(Point a, Point b, Point c) {
if ((a + b) > c || (a + c) > b || (b + c) > a) {
throw new IllegalArgumentException();
}
this.a = a;
this.b = b;
this.c = c;
}
I get an error: " Operator '+' cannot be applied to 'com.epam.rd.autotasks.triangle.Point', 'com.epam.rd.autotasks.triangle.Point' "
Do you know why doesn't let me to continue further?