Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# are triangles congruent?

Greenhorn
Posts: 6
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:

Marshal
Posts: 5593
384
• 1
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?

lowercase baba
Bartender
Posts: 12613
50
• 1
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

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
Marshal
Posts: 5593
384
• 1

Indendation good

Formatting good

Leon finkel
Greenhorn
Posts: 6

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
Marshal
Posts: 5593
384
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
thank you Liutauras Vilda

i will repair it, if you have more tips it will help

fred rosenberger
lowercase baba
Bartender
Posts: 12613
50

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.

Saloon Keeper
Posts: 4065
48
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.