469,301 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

Triangle type issue

14
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.

Expand|Select|Wrap|Line Numbers
  1. class triangle
  2. {
  3. //instance variables
  4.     int Side1;
  5.     int Side2;
  6.     int Side3;
  7.     String triangleType;
  8.  
  9. //constructor method for the three sides of the triangle
  10.     triangle(int s1, int s2, int s3)
  11.     {
  12.         Side1 = s1;
  13.         Side2 = s2;
  14.         Side3 = s3;
  15.     }
  16.  
  17. //instance method to determine the triangle type
  18.     public String triangleType()
  19.     {   
  20.         if(Side1 == Side2 && Side2 == Side3)
  21.         {triangleType = "Equilateral";}
  22.  
  23.         if(Side1 == Side2 && Side2 != Side3)
  24.         {triangleType = "Isoscelese";}
  25.  
  26.         if(Side1 != Side2 && Side2 == Side3)
  27.         {triangleType = "Isoscelese";}
  28.  
  29.         if(Side1 != Side2 && Side2 != Side3);
  30.         {triangleType = "Scalene";}
  31.  
  32.         return triangleType();
  33.      }
  34.  
  35. //print out the values of the three sides and the triangle type
  36.     public void get_values()
  37.     {
  38.         System.out.println();
  39.         System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
  40.         System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
  41.         System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
  42.     }
  43.  
  44. }
Oct 7 '07 #1
19 7381
Ganon11
3,652 Expert 2GB
Your logic for determining the type is faulty. Rethink it, especially considering if Side1 == Side3, but not Side2.
Oct 7 '07 #2
lost1
14
How's this?

Expand|Select|Wrap|Line Numbers
  1. class triangle
  2. {
  3. //instance variables
  4.     int Side1;
  5.     int Side2;
  6.     int Side3;
  7.     String triangleType;
  8.  
  9. //constructor method for the three sides of the triangle
  10.     triangle(int s1, int s2, int s3)
  11.     {
  12.         Side1 = s1;
  13.         Side2 = s2;
  14.         Side3 = s3;
  15.     }
  16.  
  17. //instance method to determine the triangle type
  18.     public String triangleType(String Equilateral, String Isoscelese, String Scalene)
  19.     {   
  20.         if(Side1 == Side2 && Side2 == Side3)
  21.         {triangleType = "Equilateral";}
  22.  
  23.         if(Side1 == Side2 && Side2 != Side3)
  24.         {triangleType = "Isoscelese";}
  25.  
  26.         //if(Side1 != Side2 && Side2 == Side3)
  27.         //{triangleType = "Isoscelese";}
  28.  
  29.         if(Side1 != Side2 && Side2 != Side3);
  30.         {triangleType = "Scalene";}
  31.  
  32.         return triangleType(Equilateral, Isoscelese, Scalene);
  33.      }
  34.  
  35. //print out the values of the three sides and the triangle type
  36.     public void get_values()
  37.     {
  38.         System.out.println();
  39.         System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
  40.         System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
  41.         System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
  42.     }   
  43. }
How do I go about calling the instance method to print "Equilateral, "Isoscelese" or "Scalene"? I've tried:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class testtriangle
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner iscanner = new Scanner(System.in);
  8.  
  9.         System.out.print("Enter side 1: ");
  10.         int side1 = iscanner.nextInt();
  11.  
  12.         System.out.print("Enter side 2: ");
  13.         int side2 = iscanner.nextInt();
  14.  
  15.         System.out.print("Enter side 3: ");
  16.         int side3 = iscanner.nextInt();
  17.  
  18.         triangle triangletype = new triangle(side1, side2, side3);
  19.         triangletype.get_values();
  20.     }
  21.  
  22. }
and I keep getting "null" for the triangle type.
Oct 7 '07 #3
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. public String triangleType(String Equilateral, String Isoscelese, String Scalene)
  2.     {   
  3.         if(Side1 == Side2 && Side2 == Side3)
  4.         {triangleType = "Equilateral";}
  5.  
  6.         if(Side1 == Side2 && Side2 != Side3)
  7.         {triangleType = "Isoscelese";}
  8.  
  9.         //if(Side1 != Side2 && Side2 == Side3)
  10.         //{triangleType = "Isoscelese";}
  11.  
  12.         if(Side1 != Side2 && Side2 != Side3);
  13.         {triangleType = "Scalene";}
  14.  
  15.         return triangleType(Equilateral, Isoscelese, Scalene); // This probably isn't doing what you think it is.
  16.      }
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.
Oct 7 '07 #4
lost1
14
Expand|Select|Wrap|Line Numbers
  1. public String triangleType(String Equilateral, String Isoscelese, String Scalene)
  2.     {   
  3.         if(Side1 == Side2 && Side2 == Side3)
  4.         {triangleType = "Equilateral";}
  5.  
  6.         if(Side1 == Side2 && Side2 != Side3)
  7.         {triangleType = "Isoscelese";}
  8.  
  9.         if(Side1 != Side2 && Side2 == Side3)
  10.         {triangleType = "Isoscelese";}
  11.  
  12.         if(Side1 != Side2 && Side2 != Side3);
  13.         {triangleType = "Scalene";}
  14.  
  15.         return triangleType(Equilateral, Isoscelese, Scalene); // This probably isn't doing what you think it is.
  16.      }
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.
Oct 7 '07 #5
Ganon11
3,652 Expert 2GB
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.
Oct 7 '07 #6
lost1
14
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.
Oct 7 '07 #7
Ganon11
3,652 Expert 2GB
OK, so Side1 = 3, Side2 = 5, and Side3 = 3. So let's walk through this function.

Expand|Select|Wrap|Line Numbers
  1. if(Side1 == Side2 && Side2 == Side3)
  2.         {triangleType = "Equilateral";}
Side1 != Side2, so this test fails. That's correct.

Expand|Select|Wrap|Line Numbers
  1.         if(Side1 == Side2 && Side2 != Side3)
  2.         {triangleType = "Isoscelese";}
Again, Side1 != Side2, so this test fails.

Expand|Select|Wrap|Line Numbers
  1.         if(Side1 != Side2 && Side2 == Side3)
  2.         {triangleType = "Isoscelese";}
Side1 != Side2, as expected, so the first part passes. But Side2 != Side3, so this test also fails.

Expand|Select|Wrap|Line Numbers
  1.         if(Side1 != Side2 && Side2 != Side3);
  2.         {triangleType = "Scalene";}
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?
Oct 7 '07 #8
lost1
14
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"
Oct 7 '07 #9
Ganon11
3,652 Expert 2GB
Once you fix that, it should work...If it doesn't let me know, and I'll check through your code again.
Oct 7 '07 #10
lost1
14
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.
Oct 7 '07 #11
Ganon11
3,652 Expert 2GB
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.
Oct 7 '07 #12
lost1
14
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.
Oct 7 '07 #13
lost1
14
Here's the code for the triangle

Expand|Select|Wrap|Line Numbers
  1. class triangle
  2. {
  3. //instance variables
  4.     int Side1;
  5.     int Side2;
  6.     int Side3;
  7.     String triangleType;
  8.  
  9. //constructor method for the three sides of the triangle
  10.     triangle(int s1, int s2, int s3)
  11.     {
  12.         Side1 = s1;
  13.         Side2 = s2;
  14.         Side3 = s3;
  15.     }
  16.  
  17. //instance method to determine the triangle type
  18.     public String triangleType(String Equilateral, String Isoscelese, String Scalene)
  19.     {   
  20.         if(Side1 == Side2 && Side2 == Side3)
  21.         {triangleType = "Equilateral";}
  22.  
  23.         if(Side1 == Side2 && Side2 != Side3)
  24.         {triangleType = "Isoscelese";}
  25.  
  26.         if(Side1 != Side2 && Side2 == Side3)
  27.         {triangleType = "Isoscelese";}
  28.  
  29.         if(Side1 != Side2 && Side2 != Side3);
  30.         {triangleType = "Scalene";}
  31.  
  32.         return triangleType(Equilateral, Isoscelese, Scalene);
  33.      }
  34.  
  35. //print out the values of the three sides and the triangle type
  36.     public void get_values()
  37.     {
  38.         System.out.println();
  39.         System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
  40.         System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
  41.         System.out.println( Side1 + "\t" +  Side2 + "\t" +  Side3 + "\t" + triangleType);
  42.     }   
  43. }
and here is the code for the test class that the user actually executes to test the class above.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class testtriangle
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner iscanner = new Scanner(System.in);
  8.  
  9.         System.out.print("Enter side 1: ");
  10.         int side1 = iscanner.nextInt();
  11.  
  12.         System.out.print("Enter side 2: ");
  13.         int side2 = iscanner.nextInt();
  14.  
  15.         System.out.print("Enter side 3: ");
  16.         int side3 = iscanner.nextInt();
  17.  
  18.         triangle triangletype = new triangle(side1, side2, side3);
  19.         triangletype.get_values();
  20.     }
  21.  
  22. }
Oct 8 '07 #14
lost1
14
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"?

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. class triangle
  4. {
  5. //instance variables
  6.     int side1;
  7.     int side2;
  8.     int side3;
  9.     String triangleType;
  10.  
  11. //constructor method for the three sides of the triangle
  12.     triangle(int s1, int s2, int s3)
  13.     {
  14.         side1 = s1;
  15.         side2 = s2;
  16.         side3 = s3;
  17.     }
  18.  
  19. //instance method to determine the triangle type
  20.     public String triangleType()
  21.     {   
  22.         String type;
  23.  
  24.         if(side1 == side2 && side2 == side3)
  25.         type = "Equilateral";
  26.  
  27.         else if(side1 == side2 && side2 != side3)
  28.         type = "Isoscelese";
  29.  
  30.         else if(side1 == side3 && side2 != side3)
  31.         type = "Isoscelese";
  32.  
  33.         else if(side1 != side3 && side2 == side3)
  34.         type = "Isoscelese";
  35.  
  36.         else
  37.         type = "Scalene";
  38.  
  39.         return type;
  40.     }
  41.  
  42. //print out the values of the three sides and the triangle type
  43.     public void get_values()
  44.     {
  45.         System.out.println();
  46.         System.out.println("Side1" + "\t" + "Side2" + "\t" + "Side3" + "\t" + "Triangle Type");
  47.         System.out.println("===" + "\t" + "===" + "\t" + "===" + "\t" + "==========");
  48.         System.out.println( side1 + "\t" +  side2 + "\t" +  side3 + "\t" + triangleType);
  49.     }   
  50. }
  51.  
  52. //test class starts here, using the impotered scanenr util
  53. //the user is prompted to enter three integers
  54.  
  55. public class testtriangle
  56. {
  57.     public static void main(String[] args)
  58.     {
  59.         Scanner iscanner = new Scanner(System.in);
  60.  
  61.         System.out.print("Enter side 1: ");
  62.         int side1 = iscanner.nextInt();
  63.  
  64.         System.out.print("Enter side 2: ");
  65.         int side2 = iscanner.nextInt();
  66.  
  67.         System.out.print("Enter side 3: ");
  68.         int side3 = iscanner.nextInt();
  69.  
  70.         triangle type = new triangle(side1, side2, side3);
  71.         type.get_values();
  72.     }
  73.  
  74. }
Oct 8 '07 #15
Laharl
849 Expert 512MB
Your problem is that you never actually assign triangleType, the string data, to be equal to the return value of triangleType(), the function. So every time you print triangleType the variable, you get null since you never defined it to be anything.

Make triangleType private and call it as part of your constructor, with a statement like

Expand|Select|Wrap|Line Numbers
  1. triangleType = triangleType(s1, s2, s3);
  2.  
Oct 8 '07 #16
lost1
14
Your problem is that you never actually assign triangleType, the string data, to be equal to the return value of triangleType(), the function. So every time you print triangleType the variable, you get null since you never defined it to be anything.

Make triangleType private and call it as part of your constructor, with a statement like

Expand|Select|Wrap|Line Numbers
  1. triangleType = triangleType(s1, s2, s3);
  2.  
I tried changing triangleType to private and I get an error and the same result "null". How do I go about making it private? Yeah, I know you change public to private, but I apparently can't do anything right with this.
Oct 8 '07 #17
r035198x
13,262 8TB
I tried changing triangleType to private and I get an error and the same result "null". How do I go about making it private? Yeah, I know you change public to private, but I apparently can't do anything right with this.
Changing the access modifiers has nothing to do with it.
Go through your code and dry run it. At each statement write down the values of all the variables.
Oct 8 '07 #18
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;
[/code]
what do != and == mean?
Oct 21 '08 #19
Nepomuk
3,112 Expert 2GB
what do != and == mean?
a != b means "a is not equal to b" while "a == b" is the exact opposite. You can however only reliably compare primitive objects (e.g. int, double, boolean,...) with these, not Objects (e.g. String, Thread, Exception,...).

Just a tip: To compare Objects, you would have to use the equals method. This article shows you how to do that for your own classes.

Greetings,
Nepomuk
Oct 21 '08 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by coinjo | last post: by
16 posts views Thread by VISHNU VARDHAN REDDY UNDYALA | last post: by
2 posts views Thread by ad | last post: by
geo039
reply views Thread by geo039 | last post: by
2 posts views Thread by nuimstudent | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.