Using variables in different methods
Hi all, I'm working on some fairly basic java programs for college (not assessments just learning aids) and have got stuck on a bit of code. I need to work out the perimeter of a triangle from the users input of the opposite and adjacent sides. I've managed to get the hypotenuse of the triangle in one method and to get the perimeter I need to add the hypotenuse, opposite and adjacent together. What I don't understand is why the method getPerimeter() won't recognise the hypot variable from the getHypotenuse() method. When it returns the value of the perimeter it ignores the hypot value. I thought about just repeating the code I used in getHypotenuse() in the getPerimeter() to get the value of hypot again but I know that would be taking the easy route.
Code:
package question4;
import java.lang.Math;
public class Triangle {
static double opposite;
static double adjacent;
static double hypot;
public static double getArea() {
double area = (opposite * adjacent) /2;
return area;
}
public static double getHypotenuse() {
double hypot;
double opSquared;
double adSquared;
double total;
opSquared = opposite * opposite;
adSquared = adjacent * adjacent;
total = opSquared + adSquared;
hypot = Math.sqrt(total);
return hypot;
}
public static double getPerimeter() {
double perimeter = (hypot + opposite + adjacent);
return perimeter;
}
}
Code:
package question4;
import javax.swing.JOptionPane;
public class TriangleTest {
public static void main(String[] args) {
String inputA;
String inputB;
inputA = JOptionPane.showInputDialog("Please enter length of opposite side: ");
inputB = JOptionPane.showInputDialog("Please enter length of adjacent side: ");
Triangle.opposite = Integer.parseInt(inputA);
Triangle.adjacent = Integer.parseInt(inputB);
System.out.printf("Area of triangle: %2.3f \n", Triangle.getArea());
System.out.printf("Hypotenuse of triangle: %2.3f \n", Triangle.getHypotenuse());
System.out.printf("Perimeter of triangle: %2.3f \n", Triangle.getPerimeter());
}
}
I hope my question makes sense and any help and direction will be greatly appreciated.:)-:
I'm still very new to the world of Java and find some of these initial concepts quite difficult to get my head around.
Re: Using variables in different methods
When you declare a variable inside a method, the scope of that variable is only inside that method. You can't access it outside the method. At the end of the method, the variable is no longer available, which means it can be garbage collected.
Also, if you have a variable declared in both a class and a method (like you do with hypot variable), the variable in the method hides the variable in the class. To refer to the class variable you can use the this keyword, but I would doubt you're trying to hide the variable in the first place.
Re: Using variables in different methods
Thanks for the reply. I only added the hypot variable into the class to see if it made it available but it didn't work and forgot to remove it.
So to get the hypot value in the getPerimeter() method I guess I will just have to cut and paste the code from the getHypotenuse() method then?
Edit: Worked it out, I just used the actual method not the variable. You feel so dumb when it finally clicks! :s: