Results 1 to 16 of 16
Thread: Help with Methods and Logic
 02272012, 01:23 AM #1Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Help with Methods and Logic
Thank you for viewing this thread. I'm working on a program that call methods for a Triangle Program. This program returns the type of triangle after user input (sidea,sideb,sidec), then returns the perimeter and area.
I'm have a issue getting the user input to override the constructor method that has to be set as 1.
So instead of the output calculating user input appropriately. My output is this
What type of triangle: equilateral triangle < being returned
The area of the triangle is: 1.0 < being returned
The perimeter of the triangle is: 3.0 < being returned
Any help is greatly appreciated!
Java Code:public class TriangleTester { public static void main(String args[]) { Triangle test = new Triangle(1,1,1); Triangle test2 = new Triangle(); double sidea; double sideb; double sidec; System.out.println("What type of triangle: " + test.test2()); System.out.println("The area of the triangle is: " + test.getArea()); System.out.println("The perimeter of the triangle is: " + test.getPerimeter()); } }

Java Code:public class Triangle { private double sidea = 1.0; private double sideb = 1.0; private double sidec = 1.0; public Triangle() { } public Triangle(double sidea, double sideb, double sidec) { this.sidea = sidea; this.sideb = sideb; this.sidec = sidec; Scanner in = new Scanner(System.in); System.out.println("Enter the first side of a triangle: "); sidea = in.nextDouble(); System.out.println("Enter the second side of a triangle: "); sideb = in.nextDouble(); System.out.println("Enter the third side of a triangle: "); sidec = in.nextDouble(); } public double getArea() { return sidea * sideb * sidec; } public double getPerimeter() { return sidea + sideb + sidec; } public String test2() { if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec)) { return "right triangle"; } if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb)) { return "right triangle"; } if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea)) { return "right triangle"; } if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec)) { return "equilateral triangle"; } if (((sidea == sideb)  (sideb == sidec)  (sidea == sidec)) && !((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) { return "isosceles triangle"; } if (!((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) { return "scalene triangle"; } else return "not a triangle"; } }

Re: Help with Methods and Logic
You'll want to get the Scanner code out of the Triangle constructor as it's throwing out the data entered in via the constructor's parameters. In fact the Triangle class shouldn't have any Scanner code or be concerned with direct user interaction as that type of code should be in other classes (likely your main method).
 02272012, 02:02 AM #3Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
public class TriangleTester
{
public static void main(String args[])
{
Triangle test = new Triangle(1,1,1);
Triangle test2 = new Triangle();
double sidea;
double sideb;
double sidec;
Scanner in = new Scanner(System.in);
System.out.println("Enter the first side of a triangle: ");
sidea = in.nextDouble();
System.out.println("Enter the second side of a triangle: ");
sideb = in.nextDouble();
System.out.println("Enter the third side of a triangle: ");
sidec = in.nextDouble();
System.out.println("What type of triangle: " + test.test2());
System.out.println("The area of the triangle is: " + test.getArea());
System.out.println("The perimeter of the triangle is: " + test.getPerimeter());
}
}
What type of triangle: equilateral triangle < returned
The area of the triangle is: 1.0 < returned
The perimeter of the triangle is: 3.0 < returned

Re: Help with Methods and Logic
That's the output that I would expect given your current code. What do you expect the output to be?

Re: Help with Methods and Logic
But note that your formula for area of a triangle is not the correct one given in most geometry textbooks.
Also note that you're accepting input from the user and then discarding the input unused. I'm not sure what you want to do with it.
 02272012, 02:23 AM #6Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
I was hoping for the user input to be run with test2,getArea and getPerimeter how could I accomplish this with my current code?

Re: Help with Methods and Logic
Suggestions:
 Get the user input before creating your Triangle object.
 Use the numbers that the user gives you when calling the Triangle's constructor. Currently you're calling one constructor with hardcode values of 1.
 You probably only need to create one Triangle object, not two like you're currently doing.
 02272012, 02:49 AM #8Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
Could you give me an example, I'm moving things around and its not working for me.

Re: Help with Methods and Logic
 02272012, 03:13 AM #10Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
No problem here is my latest attempt, I appreciate the help!
Java Code:import java.util.Scanner; public class Triangle { private double sidea = 1.0; private double sideb = 1.0; private double sidec = 1.0; public Triangle() { } public Triangle(double sidea, double sideb, double sidec) { this.sidea = sidea; this.sideb = sideb; this.sidec = sidec; } public double getArea() { return sidea * sideb * sidec; } public double getPerimeter() { return sidea + sideb + sidec; } public String Triangle() { if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec)) { return "right triangle"; } if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb)) { return "right triangle"; } if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea)) { return "right triangle"; } if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec)) { return "equilateral triangle"; } if (((sidea == sideb)  (sideb == sidec)  (sidea == sidec)) && !((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) { return "isosceles triangle"; } if (!((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) { return "scalene triangle"; } else return "not a triangle"; } }
Java Code:import java.io.*; import java.util.*; public class TriangleTester { public static void main(String args[]) { Triangle test = new triangle(); double sidea; double sideb; double sidec; Scanner in = new Scanner(System.in); System.out.println("Enter the first side of a triangle: "); sidea = in.nextDouble(); System.out.println("Enter the second side of a triangle: "); sideb = in.nextDouble(); System.out.println("Enter the third side of a triangle: "); sidec = in.nextDouble(); System.out.println("What type of triangle: " + test.triangle()); System.out.println("The area of the triangle is: " + test.getArea()); System.out.println("The perimeter of the triangle is: " + test.getPerimeter()); } }

Re: Help with Methods and Logic
First of all your code won't compile as written, will it? If you get compilation errors you must tell us.
And you're still trying to create your Triangle before you get your input. You should get sidea, sideb, and sidec from the user first, then you create your Triangle object, but don't use the default constructor that takes no parameters, use the constructor that takes 3 numbers. What numbers should you be passing into your constructor?
Also and again you will want to change your Triangle's getArea() method as your method is way too simplistic, and worse, it's wrong: that's not how you calculate the area of a triangle. Google this and you'll see the correct formula.
 02272012, 03:29 AM #12Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
Sorry I meant to add this
Java Code:TriangleTester.java:9: error: cannot find symbol Triangle test = new triangle(); ^ symbol: class triangle location: class TriangleTester TriangleTester.java:23: error: cannot find symbol System.out.println("What type of triangle: " + test.triangle()); ^ symbol: method triangle()
 02272012, 03:32 AM #13Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
I want to pass the user input of sidea,sideb,sidea into my constructor

Re: Help with Methods and Logic
So you're trying to create a Triangle object by calling a constructor named triangle. Capitalization matters and the constructor name must match the class name exactly, so your compiler is correct to complain.
Exactly. So that's what you should do.
 02272012, 04:06 AM #15Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: Help with Methods and Logic
I figured it out!! thank you

Similar Threads

Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04162012, 11:00 PM 
Java Noob, trying to call methods from another methods
By gabrielpr12 in forum New To JavaReplies: 8Last Post: 11172011, 10:07 PM 
difference between static methods and instantce methods?
By venkatch in forum New To JavaReplies: 1Last Post: 10232011, 12:37 PM 
Incorporating IfElse into Methods + Private Helper Methods?
By 5minutes in forum New To JavaReplies: 1Last Post: 10052011, 12:15 AM 
Need help in logic
By nn12 in forum New To JavaReplies: 3Last Post: 03232011, 07:44 PM
Bookmarks