Results 1 to 6 of 6
  1. #1
    delirek is offline Member
    Join Date
    Jul 2012
    Location
    Hungary, Budapest
    Posts
    3
    Rep Power
    0

    Default Using a variable from Class A, A method in B class B method

    Hi all!

    I just registered to this forum to asking you guys some questions. I started learning java 1 weeks ago. I'm trying to understand how method's value passing works.
    My problem is the following: Lets say I have Class A with a A method and a Class B with a B method and a Main class. I ask the user for input to give an integer. I want this value from A method to use it in B method. The B method in the B class calculate the the sum of the two number and return with the sum to Main class. Is this possible somewhow? The Class A and Class B are in the same package. I cant get it work. I really want to understand this method thing. Thank you all for the help. I didnt want to implement exceptions and loops while I cant resolve this. Here are the class files:

    GettingInput.java

    Java Code:
    package simplecalc;
    
    import java.util.Scanner;
    
    public class GettingInput {
        
        int fnum;
        int snum;
    
        public int getFirstNumber(int fnum){
            Scanner scr = new Scanner(System.in);
            System.out.println("Please type the first number: ");
            this.fnum=scr.nextInt();
            return fnum;
        }
        
            public int getSecondNumber(int snum){
            Scanner scr = new Scanner(System.in);
            System.out.println("Please type the second number: ");
            this.snum=scr.nextInt();
            return snum;
        }
    }
    Calculator.java

    Java Code:
    package simplecalc;
    
    public class Calculator {
    
        int Sum;
    
        public int calculating(int Sum) {
            GettingInput object = new GettingInput();
            int FirstNumber = object.fnum;
            int SecondNumber = object.snum;
            this.Sum = object.fnum + object.snum;
            return Sum;
    
        }
    }

    SimpleCalc.java


    Java Code:
    package simplecalc;
    
    public class SimpleCalc {
    
        public static void main(String[] args) {
            Calculator calc = new Calculator();
            GettingInput input = new GettingInput();
            input.getFirstNumber(input.fnum);
            input.getSecondNumber(input.snum);
            calc.calculating(calc.Sum);
            System.out.println("The sum of the 2 number: " + calc.Sum);
        }
    }
    Last edited by delirek; 07-12-2012 at 07:04 PM.

  2. #2
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Using a variable from Class A, A method in B class B method

    Just a little hint for posting on this forum: try putting your code in between of [code] brackets. This makes it easier to read and thus easier to help.
    ~ Waflix

  3. #3
    delirek is offline Member
    Join Date
    Jul 2012
    Location
    Hungary, Budapest
    Posts
    3
    Rep Power
    0

    Default Re: Using a variable from Class A, A method in B class B method

    Thank you, I reposted it with [code] brackets.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Using a variable from Class A, A method in B class B method

    Welcome to the forum!

    You do not need to input an object's variable as the argument of that same object's method. If fnum belongs to your GettingInput object, then your GettingInput object can use fnum freely. Also, don't return a value if it isn't actually going to be used. For example, here:

    Quote Originally Posted by delirek View Post

    Java Code:
    public int getFirstNumber(int fnum){
            Scanner scr = new Scanner(System.in);
            System.out.println("Please type the first number: ");
            this.fnum=scr.nextInt();
            return fnum;
        }
    There is no purpose for the argument. You also never use the value returned in this method. The same for here:

    Quote Originally Posted by delirek View Post
    Java Code:
     public int getSecondNumber(int snum){
            Scanner scr = new Scanner(System.in);
            System.out.println("Please type the second number: ");
            this.snum=scr.nextInt();
            return snum;
        }
    And here:

    Quote Originally Posted by delirek View Post
    Java Code:
    public int calculating(int Sum) {
            GettingInput object = new GettingInput();
            int FirstNumber = object.fnum;
            int SecondNumber = object.snum;
            this.Sum = object.fnum + object.snum;
            return Sum;
           }
    The other problem with the method above is that you created a whole new GettingInput object when you are already using one in the main method. You want to use the GettingInput object that actually holds the values you received from user input. I suggest that you pass the existing GettingInput object as an argument into the calculating() method like so:

    Java Code:
    public void calculating(GettingInput gi) 
    {
          int FirstNumber = gi.fnum; 
          int SecondNumber = gi.snum; 
          this.Sum = gi.fnum + gi.snum;
    }
    
    public static void main(String[] args) 
    {
          Calculator calc = new Calculator(); 
          GettingInput input = new GettingInput();
          input.getFirstNumber();
          input.getSecondNumber();
          calc.calculating(input);
          System.out.println("The sum of the 2 number: " + calc.Sum);
    }
    I think you should check out this link in order to get a better understanding of how methods work: The Java™ Tutorials
    Last edited by awinston; 07-12-2012 at 07:15 PM. Reason: Clarity
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    delirek is offline Member
    Join Date
    Jul 2012
    Location
    Hungary, Budapest
    Posts
    3
    Rep Power
    0

    Default Re: Using a variable from Class A, A method in B class B method

    Thank you for the reply awinston:)

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Using a variable from Class A, A method in B class B method

    You're welcome!
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  3. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  4. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  5. Replies: 20
    Last Post: 06-03-2009, 05:33 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
  •