Triangle type issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lost1
    New Member
    • Oct 2007
    • 14

    #1

    Triangle type issue

    Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test class executes the code you see below. What I'm trying to figure out is what I'm doing wrong in getting the triangle type to print. the integers typed in by the user print fine, but I keep getting "null" for the triangle type because I don't know how to call that part of the method correctly. I have triangleType as a String in the instance variables, is that correct? I also made triangleType part of the instance method, is that correct or should it be separate? Any help would be appreciated.

    Code:
    class triangle
    {
    //instance variables
        int Side1;
        int Side2;
        int Side3;
        String triangleType;
    
    //constructor method for the three sides of the triangle
        triangle(int s1, int s2, int s3)
        {
            Side1 = s1;
            Side2 = s2;
            Side3 = s3;
        }
    
    //instance method to determine the triangle type
        public String triangleType()
        {   
            if(Side1 == Side2 && Side2 == Side3)
            {triangleType = "Equilateral";}
            
            if(Side1 == Side2 && Side2 != Side3)
            {triangleType = "Isoscelese";}
            
            if(Side1 != Side2 && Side2 == Side3)
            {triangleType = "Isoscelese";}
            
            if(Side1 != Side2 && Side2 != Side3);
            {triangleType = "Scalene";}
            
            return triangleType();
         }
    
    //print out the values of the three sides and the triangle type
        public void get_values()
        {
            System.out.println();
            System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
            System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
            System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
        }
            
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Your logic for determining the type is faulty. Rethink it, especially considering if Side1 == Side3, but not Side2.

    Comment

    • lost1
      New Member
      • Oct 2007
      • 14

      #3
      How's this?

      Code:
      class triangle
      {
      //instance variables
          int Side1;
          int Side2;
          int Side3;
          String triangleType;
      
      //constructor method for the three sides of the triangle
          triangle(int s1, int s2, int s3)
          {
              Side1 = s1;
              Side2 = s2;
              Side3 = s3;
          }
      
      //instance method to determine the triangle type
          public String triangleType(String Equilateral, String Isoscelese, String Scalene)
          {   
              if(Side1 == Side2 && Side2 == Side3)
              {triangleType = "Equilateral";}
              
              if(Side1 == Side2 && Side2 != Side3)
              {triangleType = "Isoscelese";}
              
              //if(Side1 != Side2 && Side2 == Side3)
              //{triangleType = "Isoscelese";}
              
              if(Side1 != Side2 && Side2 != Side3);
              {triangleType = "Scalene";}
              
              return triangleType(Equilateral, Isoscelese, Scalene);
           }
      
      //print out the values of the three sides and the triangle type
          public void get_values()
          {
              System.out.println();
              System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
              System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
              System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
          }   
      }
      How do I go about calling the instance method to print "Equilatera l, "Isoscelese " or "Scalene"? I've tried:

      Code:
      import java.util.Scanner;
      
      public class testtriangle
      {
          public static void main(String[] args)
          {
              Scanner iscanner = new Scanner(System.in);
              
              System.out.print("Enter side 1: ");
              int side1 = iscanner.nextInt();
      
              System.out.print("Enter side 2: ");
              int side2 = iscanner.nextInt();
        
              System.out.print("Enter side 3: ");
              int side3 = iscanner.nextInt();
      
              triangle triangletype = new triangle(side1, side2, side3);
              triangletype.get_values();
          }
      
      }
      and I keep getting "null" for the triangle type.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        [CODE=java]public String triangleType(St ring Equilateral, String Isoscelese, String Scalene)
        {
        if(Side1 == Side2 && Side2 == Side3)
        {triangleType = "Equilatera l";}

        if(Side1 == Side2 && Side2 != Side3)
        {triangleType = "Isoscelese ";}

        //if(Side1 != Side2 && Side2 == Side3)
        //{triangleType = "Isoscelese ";}

        if(Side1 != Side2 && Side2 != Side3);
        {triangleType = "Scalene";}

        return triangleType(Eq uilateral, Isoscelese, Scalene); // This probably isn't doing what you think it is.
        }[/CODE]

        I just want you to focus on this function right now. Walk me through your logic - explain to me what you think your function is doing. I think you're missing a possibility or two here.

        Comment

        • lost1
          New Member
          • Oct 2007
          • 14

          #5
          Originally posted by Ganon11
          [CODE=java]public String triangleType(St ring Equilateral, String Isoscelese, String Scalene)
          {
          if(Side1 == Side2 && Side2 == Side3)
          {triangleType = "Equilatera l";}

          if(Side1 == Side2 && Side2 != Side3)
          {triangleType = "Isoscelese ";}

          if(Side1 != Side2 && Side2 == Side3)
          {triangleType = "Isoscelese ";}

          if(Side1 != Side2 && Side2 != Side3);
          {triangleType = "Scalene";}

          return triangleType(Eq uilateral, Isoscelese, Scalene); // This probably isn't doing what you think it is.
          }[/CODE]

          I just want you to focus on this function right now. Walk me through your logic - explain to me what you think your function is doing. I think you're missing a possibility or two here.
          It's giving the if statements for the sides of an instance of a triangle and giving the correspsonding triangle type based on those if statements after the user inputs all 3 integers. I'm not worried about negative numbers or error checking right now. There are three type of triangles. 3 sides equal: Equilateral, 2 sides equal: Isoscelese and no sides equal: Scalene.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            OK, I see that your checking for equilateral correctly, but suppose Side1 == Side3, but Side1 != Side2 (e.g. Side1 = 3, Side2 = 5, Side3 = 3). What type of triangle is this? Walk through your code very carefully and tell me what your function will think this is.

            Comment

            • lost1
              New Member
              • Oct 2007
              • 14

              #7
              Originally posted by Ganon11
              OK, I see that your checking for equilateral correctly, but suppose Side1 == Side3, but Side1 != Side2 (e.g. Side1 = 3, Side2 = 5, Side3 = 3). What type of triangle is this? Walk through your code very carefully and tell me what your function will think this is.
              if only 2 of the sides are equal it's an Isoscelese triangle. I'm losing focus from my original question.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                OK, so Side1 = 3, Side2 = 5, and Side3 = 3. So let's walk through this function.

                [CODE=java]if(Side1 == Side2 && Side2 == Side3)
                {triangleType = "Equilatera l";}[/CODE]

                Side1 != Side2, so this test fails. That's correct.

                [CODE=java] if(Side1 == Side2 && Side2 != Side3)
                {triangleType = "Isoscelese ";}[/CODE]

                Again, Side1 != Side2, so this test fails.

                [CODE=java] if(Side1 != Side2 && Side2 == Side3)
                {triangleType = "Isoscelese ";}[/CODE]

                Side1 != Side2, as expected, so the first part passes. But Side2 != Side3, so this test also fails.

                [CODE=java] if(Side1 != Side2 && Side2 != Side3);
                {triangleType = "Scalene";}[/CODE]

                Here, Side1 != Side2. The first half passes. Side2 != Side3, so the second half passes. Thus, this triangle is scalene.

                Wait, Side1 == Side3, so it should be isosceles, right? Right.

                See what I'm getting at now?

                Comment

                • lost1
                  New Member
                  • Oct 2007
                  • 14

                  #9
                  Yeah, I could add more if statements to make sure I dont get errors.

                  I need help calling the triangleType method to print the type instead of "null"

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    Once you fix that, it should work...If it doesn't let me know, and I'll check through your code again.

                    Comment

                    • lost1
                      New Member
                      • Oct 2007
                      • 14

                      #11
                      Originally posted by Ganon11
                      Once you fix that, it should work...If it doesn't let me know, and I'll check through your code again.
                      What should work? The method call doesn't work, no matter what numbers I enter or what order I enter them in. I've put in 10, 10 and 10 and gotten "null"

                      both the class and the test class compile without a problem, I just don't know how to fix the "null" print out.

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Got it. There are a few problems here:

                        1) Your method for determining the triangle type has 3 arguments. Why? What are they for? Where do you use those arguments?

                        2) Your method returns a string - but you are instead calling the function again. This is called 'recursion', and is a slightly more advanced programming technique. However, because your method is not designed for recursion, when you call this function, it will result in an endless loop, never terminating, always calling itself with the same nonsense arguments.

                        3) You never actually call this method! You should call it in your constructor, right after you assign values to Side1, Side2, and Side3. To that end, this method should be private, and should not have a return type.

                        Comment

                        • lost1
                          New Member
                          • Oct 2007
                          • 14

                          #13
                          Originally posted by Ganon11
                          Got it. There are a few problems here:

                          1) Your method for determining the triangle type has 3 arguments. Why? What are they for? Where do you use those arguments?

                          2) Your method returns a string - but you are instead calling the function again. This is called 'recursion', and is a slightly more advanced programming technique. However, because your method is not designed for recursion, when you call this function, it will result in an endless loop, never terminating, always calling itself with the same nonsense arguments.

                          3) You never actually call this method! You should call it in your constructor, right after you assign values to Side1, Side2, and Side3. To that end, this method should be private, and should not have a return type.
                          The user is prompted to enter three integers in a test class, this test class has the main method and executes what I'm trying to figure out.

                          Where is the syntax am I calling the function instead of the method? I'm not going to pick up on this unless someone shows me and lets me apply it to my own. I don't understand.

                          #3 is the reason I was here. I don't know how to call the method properly. I'm new to this.

                          Comment

                          • lost1
                            New Member
                            • Oct 2007
                            • 14

                            #14
                            Here's the code for the triangle

                            Code:
                            class triangle
                            {
                            //instance variables
                                int Side1;
                                int Side2;
                                int Side3;
                                String triangleType;
                            
                            //constructor method for the three sides of the triangle
                                triangle(int s1, int s2, int s3)
                                {
                                    Side1 = s1;
                                    Side2 = s2;
                                    Side3 = s3;
                                }
                            
                            //instance method to determine the triangle type
                                public String triangleType(String Equilateral, String Isoscelese, String Scalene)
                                {   
                                    if(Side1 == Side2 && Side2 == Side3)
                                    {triangleType = "Equilateral";}
                                    
                                    if(Side1 == Side2 && Side2 != Side3)
                                    {triangleType = "Isoscelese";}
                                    
                                    if(Side1 != Side2 && Side2 == Side3)
                                    {triangleType = "Isoscelese";}
                                    
                                    if(Side1 != Side2 && Side2 != Side3);
                                    {triangleType = "Scalene";}
                                    
                                    return triangleType(Equilateral, Isoscelese, Scalene);
                                 }
                            
                            //print out the values of the three sides and the triangle type
                                public void get_values()
                                {
                                    System.out.println();
                                    System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
                                    System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
                                    System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
                                }   
                            }
                            and here is the code for the test class that the user actually executes to test the class above.

                            Code:
                            import java.util.Scanner;
                            
                            public class testtriangle
                            {
                                public static void main(String[] args)
                                {
                                    Scanner iscanner = new Scanner(System.in);
                                    
                                    System.out.print("Enter side 1: ");
                                    int side1 = iscanner.nextInt();
                            
                                    System.out.print("Enter side 2: ");
                                    int side2 = iscanner.nextInt();
                              
                                    System.out.print("Enter side 3: ");
                                    int side3 = iscanner.nextInt();
                            
                                    triangle triangletype = new triangle(side1, side2, side3);
                                    triangletype.get_values();
                                }
                            
                            }

                            Comment

                            • lost1
                              New Member
                              • Oct 2007
                              • 14

                              #15
                              Ok I put everything in one application to make it easier to deal with and I updated my if statements. Based on what I have below can you show me how to fix this so the method call gives me the triangle type instead of "null"?

                              Code:
                              import java.util.Scanner;
                              
                              class triangle
                              {
                              //instance variables
                                  int side1;
                                  int side2;
                                  int side3;
                                  String triangleType;
                              
                              //constructor method for the three sides of the triangle
                                  triangle(int s1, int s2, int s3)
                                  {
                                      side1 = s1;
                                      side2 = s2;
                                      side3 = s3;
                                  }
                              
                              //instance method to determine the triangle type
                                  public String triangleType()
                                  {   
                                      String type;
                                              
                                      if(side1 == side2 && side2 == side3)
                                      type = "Equilateral";
                                      
                                      else if(side1 == side2 && side2 != side3)
                                      type = "Isoscelese";
                                      
                                      else if(side1 == side3 && side2 != side3)
                                      type = "Isoscelese";
                                      
                                      else if(side1 != side3 && side2 == side3)
                                      type = "Isoscelese";
                                              
                                      else
                                      type = "Scalene";
                              
                                      return type;
                                  }
                              
                              //print out the values of the three sides and the triangle type
                                  public void get_values()
                                  {
                                      System.out.println();
                                      System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
                                      System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
                                      System.out.println( side1 + "\t" +  side2 + "\t" +  side3 + "\t" + triangleType);
                                  }   
                              }
                              
                              //test class starts here, using the impotered scanenr util
                              //the user is prompted to enter three integers
                              
                              public class testtriangle
                              {
                                  public static void main(String[] args)
                                  {
                                      Scanner iscanner = new Scanner(System.in);
                                      
                                      System.out.print("Enter side 1: ");
                                      int side1 = iscanner.nextInt();
                              
                                      System.out.print("Enter side 2: ");
                                      int side2 = iscanner.nextInt();
                                
                                      System.out.print("Enter side 3: ");
                                      int side3 = iscanner.nextInt();
                              
                                      triangle type = new triangle(side1, side2, side3);
                                      type.get_values();
                                  }
                              
                              }

                              Comment

                              Working...