Click here to Skip to main content
15,609,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
...
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:
Java
...
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:

Java
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:

Java
class Triangle {
 
    Point a;
    Point b;
    Point c;
 
    public Triangle(Point a, Point b, Point c) {
        //TODO
        this.a = a;
        this.b = b;
        this.c = c;
 
    }
 
 
    public double area() {
        //TODO
 
        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(){
        //TODO
 
        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:


Java
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:


Java
<pre>public Triangle(Point a, Point b, Point c) {
        //TODO
        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?
Posted
Updated 30-Nov-22 23:35pm

1 solution

The error message is (unsurprisingly) correct: You are using Points where lengths are expected.

You need to implement a (possibly static) method giving the length of the segment connecting two given points (i.e. the distance between the points), e.g.
Java
public double length(Point a, Point b)
{
  // here your implementation...
}

Then you're allowed to write
Java
public Triangle(Point a, Point b, Point c)
{
     double lab, lac, lbc;
     lab = length(a , b);
     lac = length(a , c);
     lbc = length(b , c);

     if ((lab + lac) <= lbc || (lab + lbc) <= lac || (lbc + lac) <= lab)
     {
         throw new IllegalArgumentException();
     }
     this.a = a;
     this.b = b;
     this.c = c;
 }


[update]
Java
public class Triangle
{
    Point a;
    Point b;
    Point c;

    public Triangle(Point a, Point b, Point c) {
        double lab, lac, lbc;
        lab = length(a, b);
        lac = length(a, c);
        lbc = length(b, c);

        if ((lab + lac) <= lbc || (lab + lbc) <= lac || (lbc + lac) <= lab) {
            throw new IllegalArgumentException();
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public static double length (Point a, Point b)
    {
      double dx = a.getX() - b.getX();
      double dy = a.getY() - b.getY();
      return Math.sqrt(dx * dx + dy * dy);
    }
  //...
[/update]
 
Share this answer
 
v4
Comments
Cris29M 1-Dec-22 7:12am    
I didn't know how to implement in a single method.
I implemented 3 methods for every length of the segment:
public static double length (Point a, Point b)
    {
        return sqrt(pow(b.getX() - a.getX(), 2) + pow(b.getY() - a.getY(), 2));
    }
    public static double length2 (Point a, Point c)
    {
        return sqrt(pow(c.getX() - a.getX(), 2) + pow(c.getY() - a.getY(), 2));
    }
    public static double length3 (Point b, Point c)
    {
        return sqrt(pow(c.getX() - b.getX(), 2) + pow(c.getY() - b.getY(), 2));
    }


This is all my Triangle class:
class Triangle {

    Point a;
    Point b;
    Point c;

    public Triangle(Point a, Point b, Point c) {
        //TODO
        double lab, lac, lbc;
        lab = length(a, b);
        lac = length2(a, c);
        lbc = length3(b, c);

        if ((lab + lac) > lbc || (lab + lbc) > lac || (lbc + lac) > lab) {
            throw new IllegalArgumentException();
        }
        this.a = a;
        this.b = b;
        this.c = c;

    }
    public static double length (Point a, Point b)
    {
        return sqrt(pow(b.getX() - a.getX(), 2) + pow(b.getY() - a.getY(), 2));
    }
    public static double length2 (Point a, Point c)
    {
        return sqrt(pow(c.getX() - a.getX(), 2) + pow(c.getY() - a.getY(), 2));
    }
    public static double length3 (Point b, Point c)
    {
        return sqrt(pow(c.getX() - b.getX(), 2) + pow(c.getY() - b.getY(), 2));
    }

    public double area() {
        //TODO

        return Math.abs((a.getX() - c.getX()) * (b.getY() - a.getY()) - (a.getX() - b.getX()) * (c.getY() - a.getY())) /2;

    }

    public Point centroid(){
        //TODO

        double x = (a.getX() + b.getX() + c.getX()) / 3;
        double y = (a.getY() + b.getY() + c.getY()) / 3;

        return new Point(x, y);

    }
}


I suppose it's not good implemented because when I'm running the code I receive this error
"Exception in thread "main" java.lang.IllegalArgumentException"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900