1. Member
Join Date
Apr 2017
Posts
5
Rep Power
0

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

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

First, you need to know what makes a triangle congruent. You can look that up on the network. Then you need to read in the data.
Let's say that it's two triangles a and b, and you read in the lengths of the sides of them. If the sides lengths match up, then the triangle
is congruent. I presume that since your teacher has assigned this, that s/he has also covered the necessary features of Java to write the
program.

Regards,
Jim

3. Member
Join Date
Apr 2017
Posts
5
Rep Power
0

## Re: help please congruent triangles

hi jim,

i wrote my own code but i got stuck and i cant undestand why, ca u help me please?

here the code:

import java.util.Scanner;

public class Congruent
{
public static void main(String[] args){

Scanner scan = new Scanner(System.in);

System.out.println("Please enter 6 pair ofcoordinates ");
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int x2 = scan.nextInt();
int y2 = scan.nextInt();
int x3 = scan.nextInt();
int y3 = scan.nextInt();
int x4 = scan.nextInt();
int y4 = scan.nextInt();
int x5 = scan.nextInt();
int y5 = scan.nextInt();
int x6 = scan.nextInt();
int y6 = scan.nextInt();

double AB = Math.sqrt(Math.pow(x2-x1, 2) + (Math.pow(y2-y1, 2)));
double AC = Math.sqrt(Math.pow(x3-x1, 2) + (Math.pow(y3-y1, 2)));
double BC = Math.sqrt(Math.pow(x3-x2, 2) + (Math.pow(y3-y2, 2)));
double DE = Math.sqrt(Math.pow(x5-x4, 2) + (Math.pow(y5-y4, 2)));
double DF = Math.sqrt(Math.pow(x6-x4, 2) + (Math.pow(y6-y4, 2)));
double EF = Math.sqrt(Math.pow(x5-x6, 2) + (Math.pow(y5-y6, 2)));

if((AB=DE || AB=DF || AB=EF) && (AC=DE || AC=DF || AC=EF) && (BC=DE || BC=DF || BC=EF))
System.out.println("The triangles are congruent.");

else
System.our.println("The triangles are not congruent.");

}

}

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

First, when you post code, use code tags to format the code. See the BB codes section at the end of this thread. I have done it for you here. The # menu option in the advanced editor will put them in for you once you highlight you code.

Java Code:
```import java.util.Scanner;

public class Congruent
{
public static void main(String[] args){

Scanner scan = new Scanner(System.in);

System.out.println("Please enter 6 pair ofcoordinates ");
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int x2 = scan.nextInt();
int y2 = scan.nextInt();
int x3 = scan.nextInt();
int y3 = scan.nextInt();
int x4 = scan.nextInt();
int y4 = scan.nextInt();
int x5 = scan.nextInt();
int y5 = scan.nextInt();
int x6 = scan.nextInt();
int y6 = scan.nextInt();

double AB = Math.sqrt(Math.pow(x2-x1, 2) + (Math.pow(y2-y1, 2)));
double AC = Math.sqrt(Math.pow(x3-x1, 2) + (Math.pow(y3-y1, 2)));
double BC = Math.sqrt(Math.pow(x3-x2, 2) + (Math.pow(y3-y2, 2)));
double DE = Math.sqrt(Math.pow(x5-x4, 2) + (Math.pow(y5-y4, 2)));
double DF = Math.sqrt(Math.pow(x6-x4, 2) + (Math.pow(y6-y4, 2)));
double EF = Math.sqrt(Math.pow(x5-x6, 2) + (Math.pow(y5-y6, 2)));

if((AB=DE || AB=DF || AB=EF) && (AC=DE || AC=DF || AC=EF) && (BC=DE || BC=DF || BC=EF))
System.out.println("The triangles are congruent.");

else
System.our.println("The triangles are not congruent.");
}
}```
A couple observations.

1. AB = DF is an assignment, not a comparison for equality. Use ==
2. Your last print statement has our and not out.

Now, your algorithm is incorrect. Try this example

AB = 5;
AC = 5;
BC = 5;
DE = 5;
DF= 10;
EF = 10;

All of your || expressions will return true so the entire expression will be true. But the triangles are not congruent. So you need to work it out on paper. A hint: once you find a pair that are equal, then test the other sides. This could result in multiple nested if else statements.

And finally, nothing wrong with your method of calculating the line length but you may want to check out Math.hypot(). Just remember that that method presumes that the line starts at the origin (0,0).

Regards,
Jim

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

A couple of extra suggestions.

1. Don't keep prompting for input. It makes debugging difficult and slow. Either hard code the coordinates (or side lengths) or read them in from a file.
2. Keep in mind that if you could possible sort the sides of triangle A and then triangle B, there should be a one-to-one match if the
triangles are congruent.

Regards,
Jim

6. Member
Join Date
Apr 2017
Posts
5
Rep Power
0

## Re: help please congruent triangles

Originally Posted by jim829
A couple of extra suggestions.

1. Don't keep prompting for input. It makes debugging difficult and slow. Either hard code the coordinates (or side lengths) or read them in from a file.
2. Keep in mind that if you could possible sort the sides of triangle A and then triangle B, there should be a one-to-one match if the
triangles are congruent.

Regards,
Jim
thant you jim, you helped a lot.
i only don`t understand why the algorithm is incorrect?
i put in the coordinates to get if there congruent or not.

is there another thing that i can do to create a better program(using only the main method)?

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

The algorithm (if statement) for testing congruency is not correct. As I said, consider the following sides:
Java Code:
```// Triangle 1
double AB = 5;
double AC = 5;
double BC = 5;
//Triangle 2
double DE = 5;
double DF= 10;
double EF = 10;

if((AB==DE || AB==DF || AB==EF) && (AC==DE || AC==DF || AC==EF) && (BC==DE || BC==DF || BC==EF)) {
System.out.println("The triangles are congruent.");
} else {
System.our.println("The triangles are not congruent.");
}```
Since AB == DE is true and AC == DE is true and BC == DE is true Your if statement evaluates to:

if (true && true && true) which also evaluates to true so you claim the triangles are congruent (which they are not).

Regards,
Jim

8. ## Re: help please congruent triangles

9. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

Ugh!! I'm not going to even bother to see how much duplicate information I provided.

Regards,
Jim

10. ## Re: help please congruent triangles

Note that the 'triangle' (mind the quotes) 5, 10, 10 isn't a triangle and the formula doesn't have to make sense here ...
(for a triangle a, b, c to be a real triangle, a < b+c && b < a+c && c < a+b)

kind regards,

Jos

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: help please congruent triangles

What doe you mean that 5, 10, 10 isn't a triangle? It is an isosceles triangle. Two sides of length 10 connected by a side of length 5.
And my triangle (notice lack of quotes) satisfies your formula. Perhaps you were thinking of 5, 5 10?

Regards,
Jim
Last edited by jim829; 04-08-2017 at 05:23 PM.

12. ## Re: help please congruent triangles

Erm, yep, but that is because I have my brain wired all backwards in the weekend; blame it on the complete lack of Grolsch. sorry, nothing to see here, please keep walking ...

kind regards,

Jos