Results 1 to 12 of 12
  1. #1
    lolik is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    lolik is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default 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(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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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(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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    lolik is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: help please congruent triangles

    Quote Originally Posted by jim829 View Post
    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. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: help please congruent triangles

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: help please congruent triangles

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

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Making Triangles
    By truegamer95 in forum New To Java
    Replies: 2
    Last Post: 02-08-2013, 04:41 PM
  2. Two triangles of stars next to each other
    By Aero in forum New To Java
    Replies: 3
    Last Post: 09-26-2011, 07:20 PM
  3. Producing Congruent Numbers
    By freddy92 in forum New To Java
    Replies: 2
    Last Post: 07-04-2011, 01:54 PM
  4. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •