Results 1 to 12 of 12
  1. #1
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Simple calculation method problem.

    Java Code:
    public class CalculationExample{
    	
    	//declare global variable
    	static int y;
    	
    	public static void main(String[] args){
    		
    		addition(4, 3); //method called with two values assigned
    	}//end main
    	
    	public int addition( int a, int b ){
    		y=a+b;
    		return y
    	}//end addition method
    }//end class
    In the above class, the addition method takes an argument of two integer whole numbers
    adds both numbers and returns that value as y=7.

    My question is, is it possible to work to re-use y equalling to 7 or do I have to recall the entire
    method to get the 7 value.

    Below is an example of what I mean.

    Java Code:
    public class CalculationExample{
    	
    	//dclare global variable
    	int y;
    	
    	public static void main(String[] args){
    	
    		addition(4, 3);
    		System.out.print(y); //is it correct that this line prints 7 or does y only obtain a value
    								when it is beign called in a method form.
    	}//end main
    	
    	public int addition( int a, int b ){
    		y=a+b;
    		return y
    	}//end addition method
    	
    }//end class
    I'm just trying to figure if after the addition method is run would y obtain the value seven and store it
    or if I wanna pull back y as 7 I need to re-enter the entire addition method.

    Hope I made myself clear.
    Last edited by Eranga; 12-28-2010 at 01:56 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Your addition() method returns an int (it must be a static method as well, non-static cannot invoke from static). So assign the return value to a int type.

    Java Code:
    int y = addition(4, 3);
    Then display it.

    Java Code:
    System.out.println(y);
    Last edited by Eranga; 12-28-2010 at 02:24 AM.

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Begin by making your variable static. The answer is yes y will have value 7 or any value you assign to it. Try the following code:

    public class CalculationExample{

    Java Code:
    	static int y;
    	
    	public static void main(String[] args){
    		
    		y = addition(4, 3);
    		System.out.print(y); 
    	}
    	
    	public static int addition( int a, int b ){
    		return a + b;
    	}
    --user0--

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    if you declare the addition function without static, then you need to instantiate a CalculationExample object in order to use it
    Java Code:
    public class CalculationExample{
        public static void main(String[] args){
            int y;
            CalculationExample add = new CalculationExample();
            y = add.addition(4, 3);
            System.out.print(y); //is it correct that this line prints 7 or does y only obtain a value
        //                          when it is beign called in a method form.
        }//end main
    
        public int addition( int a, int b ){
            return a+b;
        }//end addition method
    
    }//end class
    otherwise, change your addition method to static
    Java Code:
        public static int addition( int a, int b ){
            return a+b;
        }//end addition method
    also, declare a global variable when you really need to, otherwise, use local variables.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by user0 View Post
    Begin by making your variable static. The answer is yes y will have value 7 or any value you assign to it. Try the following code:

    public class CalculationExample{

    Java Code:
    	static int y;
    	
    	public static void main(String[] args){
    		
    		y = addition(4, 3);
    		System.out.print(y); 
    	}
    	
    	public static int addition( int a, int b ){
    		return a + b;
    	}
    Really no need to define as static. Avoid global variables as much as possible.

    Within the scope the value never changed.

  7. #7
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I appreciate that help, I knew I forgot something turns out you all were right static wasn't stated so the variable didn't keep the value after calculation.

    Thanks again.

    BTW, what exactly do you mean to put the code in tags...

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Hey I thought that would have worked, however after work I went and tried declaring it both;
    static int y;
    and
    public static int y;

    neither of which worked.

    Is there something else I should have done?

    It is storing a value but not the accurate for some reason it is giving me a completely different figure.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    oh btw, I'm sorry I havn't been using tags but i'm a beginner to java and we havn't touched HTML as yet.

    Hope that isn't too much of a problem..

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by carman12 View Post
    oh btw, I'm sorry I havn't been using tags but i'm a beginner to java and we havn't touched HTML as yet.

    Hope that isn't too much of a problem..
    Of course not, and that's not HTML. Read it carefully and give a try in next post.

Similar Threads

  1. Problem with calculation
    By ibrafiqb in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 06:02 PM
  2. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 01:07 PM
  3. Two problem for my rssi calculation program
    By iamchoilan in forum Java 2D
    Replies: 12
    Last Post: 04-27-2008, 08:56 PM
  4. Two problem for my rssi calculation program
    By iamchoilan in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2008, 04:49 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 03:42 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
  •