• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement the constructor of a Triangle class?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


Sample code:


Sample code:


This is the Point class:


This is the Triangle class:

This is the Main class:



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:


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?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get a degenerate Triangle if two or more points are the same, or that the three points are collinear, i.e. ly on the same line.

It is handy to hane some helper-methods in your Point class, like add/subtract(Point p). Also handy is to have a method 'determinant(Point p, Point q). If that is 0, then p and q are collinear with a line through the origin. If you have a Triangle p, q, r then you can apply this on the vectors p - q and q - r.
 
Ranch Hand
Posts: 133
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have the tool you need in terms of the area computation.  I would move the computation into your constructor.   If the area is very close to zero, then you have a degenerate triangle.

Note that the area computation can be positive or negative depending on whether the three vertices specify the triangle boundary in clockwise or counterclockwise order.  So your test would have to include the absolute value method....   Perhaps you could do something like the following (the threshold value 1.0e-9is arbitrary):



 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I would prefer the tests for whether two of the corners are at equal Points, and collinearity, fof a student assignment. That will also allow different forms of the throw statement depending on the cause.
It would be necessary for the Point objects to have correctly overridden equals() and hashCode() methods.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cris Marinescu wrote:. . . "  Operator '+' cannot be applied to 'com.epam.rd.autotasks.triangle.Point', 'com.epam.rd.autotasks.triangle.Point' " . . .

The + operator can be applied to numbers for addition, or for String concatenation. It cannot be used on any other types of varables or values. More information in the Java® Language Specification (=JLS). As Piet suggests, you can wite methods in Point instead of using +.
reply
    Bookmark Topic Watch Topic
  • New Topic