Results 1 to 12 of 12
Thread: help please congruent triangles
 04072017, 03:45 PM #1Member
 Join Date
 Apr 2017
 Posts
 5
 Rep Power
 0
help please congruent triangles
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
 04072017, 03:54 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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.
But we don't write software here, we just help you debug your own.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04072017, 05:31 PM #3Member
 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 ");
System.out.println("Please enter x1: ");
int x1 = scan.nextInt();
System.out.println("Please enter y1: ");
int y1 = scan.nextInt();
System.out.println("Please enter x2: ");
int x2 = scan.nextInt();
System.out.println("Please enter y2: ");
int y2 = scan.nextInt();
System.out.println("Please enter x3: ");
int x3 = scan.nextInt();
System.out.println("Please enter y3: ");
int y3 = scan.nextInt();
System.out.println("Please enter x4: ");
int x4 = scan.nextInt();
System.out.println("Please enter y4: ");
int y4 = scan.nextInt();
System.out.println("Please enter x5: ");
int x5 = scan.nextInt();
System.out.println("Please enter y5: ");
int y5 = scan.nextInt();
System.out.println("Please enter x6: ");
int x6 = scan.nextInt();
System.out.println("Please enter y6: ");
int y6 = scan.nextInt();
double AB = Math.sqrt(Math.pow(x2x1, 2) + (Math.pow(y2y1, 2)));
double AC = Math.sqrt(Math.pow(x3x1, 2) + (Math.pow(y3y1, 2)));
double BC = Math.sqrt(Math.pow(x3x2, 2) + (Math.pow(y3y2, 2)));
double DE = Math.sqrt(Math.pow(x5x4, 2) + (Math.pow(y5y4, 2)));
double DF = Math.sqrt(Math.pow(x6x4, 2) + (Math.pow(y6y4, 2)));
double EF = Math.sqrt(Math.pow(x5x6, 2) + (Math.pow(y5y6, 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.");
}
}
 04072017, 06:43 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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 "); System.out.println("Please enter x1: "); int x1 = scan.nextInt(); System.out.println("Please enter y1: "); int y1 = scan.nextInt(); System.out.println("Please enter x2: "); int x2 = scan.nextInt(); System.out.println("Please enter y2: "); int y2 = scan.nextInt(); System.out.println("Please enter x3: "); int x3 = scan.nextInt(); System.out.println("Please enter y3: "); int y3 = scan.nextInt(); System.out.println("Please enter x4: "); int x4 = scan.nextInt(); System.out.println("Please enter y4: "); int y4 = scan.nextInt(); System.out.println("Please enter x5: "); int x5 = scan.nextInt(); System.out.println("Please enter y5: "); int y5 = scan.nextInt(); System.out.println("Please enter x6: "); int x6 = scan.nextInt(); System.out.println("Please enter y6: "); int y6 = scan.nextInt(); double AB = Math.sqrt(Math.pow(x2x1, 2) + (Math.pow(y2y1, 2))); double AC = Math.sqrt(Math.pow(x3x1, 2) + (Math.pow(y3y1, 2))); double BC = Math.sqrt(Math.pow(x3x2, 2) + (Math.pow(y3y2, 2))); double DE = Math.sqrt(Math.pow(x5x4, 2) + (Math.pow(y5y4, 2))); double DF = Math.sqrt(Math.pow(x6x4, 2) + (Math.pow(y6y4, 2))); double EF = Math.sqrt(Math.pow(x5x6, 2) + (Math.pow(y5y6, 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."); } }
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04072017, 06:57 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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 onetoone match if the
triangles are congruent.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04072017, 07:20 PM #6Member
 Join Date
 Apr 2017
 Posts
 5
 Rep Power
 0
Re: help please congruent triangles
 04072017, 07:46 PM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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."); }
if (true && true && true) which also evaluates to true so you claim the triangles are congruent (which they are not).
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04082017, 04:28 PM #8
Re: help please congruent triangles
Cross posted: https://coderanch.com/t/678191/java/trianglescongruent
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 04082017, 04:44 PM #9Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: help please congruent triangles
Ugh!! I'm not going to even bother to see how much duplicate information I provided.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04082017, 05:01 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 04082017, 05:10 PM #11Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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,
JimLast edited by jim829; 04082017 at 05:23 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04082017, 05:31 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
Similar Threads

Making Triangles
By truegamer95 in forum New To JavaReplies: 2Last Post: 02082013, 04:41 PM 
Two triangles of stars next to each other
By Aero in forum New To JavaReplies: 3Last Post: 09262011, 07:20 PM 
Producing Congruent Numbers
By freddy92 in forum New To JavaReplies: 2Last Post: 07042011, 01:54 PM 
Triangles
By CodeDog in forum New To JavaReplies: 9Last Post: 10142008, 09:18 PM 
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01122008, 07:42 PM
Bookmarks