• Post Reply Bookmark Topic Watch Topic
  • New Topic

are triangles congruent?  RSS feed

 
Leon finkel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i`m a begginer in java.
i need to write a program using only the main metod to calculate if triangles are congruent,
using boolean if triangles are congruent it will give me true
else it give me faulse.

please if someone can help me with the writing i will Appreciate

i wrote my own code but i got stuck and i cant undestand why?

here the code:

 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Indentation
2. Formatting

3. You can't check equality with a single '=', to evaluate boolean type you need to use '==' (without single quotes).

Have you learned about how to define and use methods yet?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point values are not precise.  If you ever try adding 0.01 to itself 10 times, you might be surprised when you print out the final sum...

You'll might have the same issue when you calculate your square roots and compare them - i'm not sure.

this statement is attrocious, even if it were written correctly:



it is not obvious at ALL what you are trying to do here. I'm guessing you're trying to prove the two triangles are congruent with the SSS theorem. However, I think your logic is flawed..

Let AB = 3, BC = 3, AC = 3.

Let DE = 3, DF = 5, DF = 3

so now, AB == DE is true,  AC == DE true , and BC == DE is true.  Your boolean will evaluate to true, but clearly the triangles are not congruent.
 
Leon finkel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. Indentation
2. Formatting

3. You can't check equality with a single '=', to evaluate boolean type you need to use '==' (without single quotes).

Have you learned about how to define and use methods yet?


i only started few days ago, what do you mean by methods?
any way i was told to create this program using only main method.

i didn`t fully understand about Indentation, if you can give me any tips it will ba Awesome
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indentaion bad (your way)

Indendation good


Formatting bad (your way)

Formatting good
 
Leon finkel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Floating point values are not precise.  If you ever try adding 0.01 to itself 10 times, you might be surprised when you print out the final sum...

You'll might have the same issue when you calculate your square roots and compare them - i'm not sure.

this statement is attrocious, even if it were written correctly:



it is not obvious at ALL what you are trying to do here. I'm guessing you're trying to prove the two triangles are congruent with the SSS theorem. However, I think your logic is flawed..

Let AB = 3, BC = 3, AC = 3.

Let DE = 3, DF = 5, DF = 3

so now, AB == DE is true,  AC == DE true , and BC == DE is true.  Your boolean will evaluate to true, but clearly the triangles are not congruent.


how can i rewrite so it wiil be more correctly?
Maybe use the "NOT !" boolean?
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't get indentation and formatting right, or worse, don't want to - need to consider if you want to continue.
Other tasks are way more complex and way more complicated. Indentation and formatting are the easiest things you can assure are correct in your code.
 
Leon finkel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Liutauras Vilda

i will repair it, if you have more tips it will help
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leon finkel wrote:
how can i rewrite so it wiil be more correctly?
Maybe use the "NOT !" boolean?

Maybe...but guessing it the worst way to write code. There are millions of ways to code this incorrectly, so if you guess, you're going to be spending a lot of time coding wrong guesses, and may never find a correct one.

Writing code is about 90% thinking, and 10% typing.  So the best advice would be to turn off your computer.  Get some paper and a few pencils.  An eraser is a very handy tool. Start writing out how YOU, personally, would proove two triangles are congruent.  Write out every single step.

Then, show that list to a 10year old. See if they can follow your instructions. If not, revise them, making each step simpler and simpler (and probably creating more steps as you go).

Once you can do that, THEN you can start writing the code.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two thoughts on your code.

1) If you are taking int's as input and want to compare lengths precisely you should be able to do it using only integer arithmetic. Something like
and then compare the squared values to see if they are the same. Using int's has the drawback of not being able to specify arbitrary orientation, ie rotated at any angle. This would probably suffice for your assignment.

2) You can use doubles for both your input and your calculations. That will solve the arbitrary rotation problem but introduce the problem of comparing two doubles for "equality" while taking rounding errors into account. This is an advanced topic.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!