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. - 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);
-
}
-
-
}
19 7987
Your logic for determining the type is faulty. Rethink it, especially considering if Side1 == Side3, but not Side2.
How's this? - 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 "Equilateral, "Isoscelese" or "Scalene"? I've tried: - 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.
- 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); // This probably isn't doing what you think it is.
-
}
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.
- 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); // This probably isn't doing what you think it is.
-
}
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.
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.
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.
OK, so Side1 = 3, Side2 = 5, and Side3 = 3. So let's walk through this function. - if(Side1 == Side2 && Side2 == Side3)
-
{triangleType = "Equilateral";}
Side1 != Side2, so this test fails. That's correct. - if(Side1 == Side2 && Side2 != Side3)
-
{triangleType = "Isoscelese";}
Again, Side1 != Side2, so this test fails. - if(Side1 != Side2 && Side2 == Side3)
-
{triangleType = "Isoscelese";}
Side1 != Side2, as expected, so the first part passes. But Side2 != Side3, so this test also fails. - if(Side1 != Side2 && Side2 != Side3);
-
{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?
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"
Once you fix that, it should work...If it doesn't let me know, and I'll check through your code again.
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.
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.
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.
Here's the code for the triangle - 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. - 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();
-
}
-
-
}
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"? - 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();
-
}
-
-
}
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 -
triangleType = triangleType(s1, s2, s3);
-
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 -
triangleType = triangleType(s1, s2, s3);
-
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.
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.
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?
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
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 javadkhan |
last post: by
|
2 posts
views
Thread by ad |
last post: by
|
1 post
views
Thread by Leo |
last post: by
| | | | | | | | | | | | | | |