Results 1 to 3 of 3
  1. #1
    squirmytoad is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

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

    Java 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;
    	}
    		
    	
    }
    Java 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    squirmytoad is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default 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!
    Last edited by squirmytoad; 01-10-2012 at 09:22 PM.

Similar Threads

  1. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 04:44 AM
  2. Static variables and methods
    By sudharani in forum New To Java
    Replies: 5
    Last Post: 04-25-2011, 03:07 PM
  3. Using variables inside methods?
    By Forty0ztoFreedom in forum New To Java
    Replies: 2
    Last Post: 04-08-2011, 08:36 PM
  4. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 05:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •