Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

Describe the class representing the triangle

+Pie Number of slices to send: Send
Hello! Please help me write a console application in the Java language.
Describe the class representing the triangle. Provide methods for creating objects, moving on a plane, resizing and rotating at a given angle. Describe properties to get the state of an object. If it is not possible to construct a triangle, an exception is thrown.
+Pie Number of slices to send: Send
Welcome to the Ranch

We don't give out complete solutions; please show us what you have achieved so far.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Welcome to the Ranch

We don't give out complete solutions; please show us what you have achieved so far.



+Pie Number of slices to send: Send
 

Mike Vozovski wrote:

Campbell Ritchie wrote:Welcome to the Ranch

We don't give out complete solutions; please show us what you have achieved so far. . . .


Here is the code. But it does not work

 
+Pie Number of slices to send: Send
Why have you got all those setXXX() methods?
Why aren't you instantiating your objects with their correct values from their constructors?
You are throwing the wrong sort of exception; you should throw an IllegalArgumentException.
We can only help if we know the full details.
+Pie Number of slices to send: Send
"it does not work" doesn't tell us a great deal.
You need to explain what exactly doesn't work.

Do you get a compilation error, if so what is it exactly?
Do you get an exception thrown when you run it, if so (again) what is it exactly (including the stack trace).
Does it not return the correct values, if so then what test data do you use and what results do you expect and what results do you get?
+Pie Number of slices to send: Send
Welcome again to the Ranch, Mike Vozovski!

Just a note on how you are posting: it is unnecessary to quote the entire previous reply.  Only quote if the context is not obvious.  Usually you can just press the reply button.  See WhenToQuote (that's a link).
+Pie Number of slices to send: Send

One way to check the correctness of the input vertices is to compute the area of the triangle:

   double h = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y);
   area = h/2

This calculation will produce a positive area if the vertices are given in counter-clockwise order, a negative area if they are given in clockwise order.  But if the inputs are bad, it will give a value very close to zero.  So I suggest something like:

  if(Math.abs(area)<1.0e-6){
      throw new IllegalArgumentException("Vertices are colinear or specify a degenerate triangle");
  }

As noted in the above post, IllegalArgumentException is an appropriate exception.   Throwing an Error is seldom a good idea.


Also, a warning about something that might cause you trouble later on...  Your rotation code would rotate the triangle about the origin.  For graphics purposes, this is fine if the coordinates of the triangle are close to the origin. But  if the triangle is not close to the origin, you could end up rotating it right out of your display area.  I have no idea what problem you're trying to solve here, but keep this in mind as a potential cause if you're debugging and get a result you don't understand.

+Pie Number of slices to send: Send
You tend to overuse the word 'this' when it would have been the default anyway. This makes your code harder to read.

There is a lot of redundancy in your code that could be collapsed by moving more responsibility to the Point class.  Here is your rotate() method after adding a corresponding rotate() method to your Point class.


+Pie Number of slices to send: Send
Assuming the three Points are vertices, then isn't it easier to check whether or not they are colinear to see if they define a triangle? That is, if the x's are equal or the y's are equal, then the points are colinear and don't define a triangle.
+Pie Number of slices to send: Send
 

Carey Brown wrote:This makes your code harder to read.


"this makes your code harder to read," too.
+Pie Number of slices to send: Send
 

I stupidly wrote:Assuming the three Points are vertices, then isn't it easier to check whether or not they are colinear to see if they define a triangle? That is, if the x's are equal or the y's are equal, then the points are colinear and don't define a triangle.


Duh, I just realized it's really not as simple as Xs or Ys being equal. That only takes care of horizontal or vertical lines. Anyway...
+Pie Number of slices to send: Send
If you created a Line class that encapsulated the calculation for its slope, then you could write this:

Actually, you could write another method on Line:

Both methods can use the same calculations.
+Pie Number of slices to send: Send
 

Carey Brown wrote:Here is your rotate() method after adding a corresponding rotate() method to your Point class.


That's kind of misplaced, isn't it? You can't actually rotate a point per se because, well, it's a point. You can rotate a line a certain angle though. You can rotate a Point in reference to another point but that still brings you back to rotating a line really. To rotate a triangle ABC, you'd fix one vertex and rotate the other vertices the same angle with reference to the fixed vertex. In other words, to rotate △ABC with point A fixed, then rotate AB and AC the same angle.
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:

Carey Brown wrote:Here is your rotate() method after adding a corresponding rotate() method to your Point class.


That's kind of misplaced, isn't it? You can't actually rotate a point per se because, well, it's a point. You can rotate a line a certain angle though. You can rotate a Point in reference to another point but that still brings you back to rotating a line really. To rotate a triangle ABC, you'd fix one vertex and rotate the other vertices the same angle with reference to the fixed vertex. In other words, to rotate △ABC with point A fixed, then rotate AB and AC the same angle.

Unless otherwise specified the rotation of a point will rotate it around the origin.
+Pie Number of slices to send: Send
 

Carey Brown wrote:Unless otherwise specified the rotation of a point will rotate it around the origin.


[pedanticMode=on]

Sure. That's still in reference to another point (the origin is a Point), so basically, you're back to rotating a line. Even rotating a line is relative to a point because the displacement of any point on the line will vary depending on what the point of rotation is. Again, it really has a lot to do with the semantics you use and whether or not you have an assumed reference point.

[/pedanticMode=off?]
+Pie Number of slices to send: Send
Indeed, rotation in R2 is around a Point, can be any Point, as Gary explained. Fun is to rotate the Triangle around the center of its surrounding circle (the point where the contour lines cross)

@Junilu
if you have three different points in the R2, say a, b, c, then they are collinear if and only if the Matrix of (a - b) and (b - c) has determinant 0.    

@OP
a static method distance(Point p, Point q) is to be advised.
+Pie Number of slices to send: Send
 

Piet Souris wrote:@OP
a static method distance(Point p, Point q) is to be advised.


Or non-static

+Pie Number of slices to send: Send
 

Piet Souris wrote:
@Junilu
if you have three different points in the R2, say a, b, c, then they are collinear if and only if the Matrix of (a - b) and (b - c) has determinant 0.    


I think I see what you did there.  
+Pie Number of slices to send: Send
Good point on the determinant.  It's kind of a reminder that, if the three points define a valid triangle, they can also be used to construct the axes for a 3D coordinate system.

Incidentally, the area computation I posted earlier is algebraically equivalent to the determinant (with an appropriate assignment of variables to the vertices).

reply
reply
This thread has been viewed 888 times.
Similar Threads
MVC is not object oriented??
Passed Part One with 87%
Triangles program
Code to restore jPane from minimized down in toolbar?
long post IBM.158
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 05:36:47.