Passing variables with void methods

Discussion in 'Programmer's Corner' started by krow, Mar 21, 2012.

  1. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    Hello,

    Not sure if this is either too easy or just impossible. Here's the story: I'm processing data from a sensor and I have several methods that do a specific task with those input values.

    The actual code is a bit long so there's no need to have you read it all. I wrote something simpler with the basic idea of what I'm trying to do. I'll try to explain it first:

    Let's say I have 5 void methods: A, B, C, D and main ("main" is not really the problem here though).

    1. Method "A" passes a variable (int data) to method "B"
    2. Method "B" does something with that variable.
    3. Method "A" also passes the variable "data" to method "C".
    4. Now, method "B" and method "C" want to pass the processed data (two variables = one variable from each method) to one more method called "D", which will give me a final result.

    So basically, method "D" is taking a variable from both method "B" and "C". I've been trying to do that but I get an error in method "B" and "C" saying that method "D" requires two variables, not one. It's obvious, I'm giving it only one variable per method but method "D" needs two arguments. I hope you're still reading this and sorry if I made it complicated, please check the code below, very simple:

    Code ( (Unknown Language)):
    1.  
    2. package passing;
    3.  
    4. public class Passing {
    5.  
    6.     Passing p;
    7.  
    8.     public static void main(String[] args) {
    9.         new Passing().sum();
    10.     }
    11.  
    12.     public void sum() {
    13.  
    14.         int x = 3;
    15.         int y = 7;
    16.         int mySum1 = x + y;
    17.         int mySum2 = 2 * x + y;
    18.  
    19.         mult(mySum1, mySum2);
    20.         add5(mySum1, mySum2);
    21.     }
    22.  
    23.     public void mult(int pass1, int pass2) {
    24.  
    25.         int myMult1 = pass1 * pass2;
    26.         int myMult2 = myMult1 * myMult1;
    27.         total(myMult1, myMult2);
    28.  
    29.     }
    30.  
    31.     public void add5(int a, int b) {
    32.        
    33.         int add = a + b + 5;
    34.         total(add);
    35.        
    36.     }
    37.  
    38.     public void total(int r1, int r2, int r3) {
    39.         System.out.println(r1+ "\t"+ r2);
    40.     }
    41. }
    42.  
    43.  
    Any help on this will be much appreciated, thanks.
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,883
    999
    You are making this way more complicated than it needs to be. Refer to your functions just as they are written and post the exact error with the offending line marked in some way.
     
    Last edited: Mar 21, 2012
  3. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    OK, sorry about that.

    Look at the mult() and add5() methods:

    The question is, how do I pass variables "myMult1", "myMult2" and "add" to the "total()" method?

    I made a mistake in the total() method, I actually wanted to print r1, r2 and r3 but I printed only two :/


    System.out.println(r1+ "\t"+ r2 + "\t3);

    I hope it's clear now!
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,883
    999
    Remove , int r3 from the total method declaration. It is not being used.

    Is this supposed to be c++? If so there is no reason no reason to precede each function with public.

    You can simply do as follows

    public:

    Then list all of your public functions.
     
  5. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    No no :( I can't remove r3 because I'm using that argument in the total() method, or at least that's what I want to do, I made it way too complicated. I'll change the two methods that are giving me trouble:

    public void mult(int pass1, int pass2) {

    int myMult1 = pass1 * pass2;
    int myMult2 = myMult1 * myMult1;

    //=============================================
    total(myMult1, myMult2); //pass variables to 'total()'
    //=============================================

    }

    public void add5(int a, int b) {

    int add = a + b + 5;
    //=============================================
    total(add); // Pass variable to 'total()'
    //=============================================

    }

    public void total(int r1, int r2, int r3) {

    int result = r1 + r2 + r3;

    System.out.println(result);
    }

    So is there any way one method can receive arguments from TWO different methods?

    I'm using Java by the way. Thanks for your patience.
     
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    The way your total() method is written, you must pass it exactly three arguments. If you want a version that handles two arguments, you need to declare it with a method signature that takes two arguments (with same name, this is allowed because of method overloading).

    The other option is to pass a 0 as the third argument. That will work in this case since adding 0 to a number doesn't change it, but be careful because a 0 might change the result in other contexts (like a multiply method).

    Bigger picture, I'm confused by what you're trying to do. In the method add5(int, int), you're adding some numbers, getting a single result, and saving it to the variable 'add'. Why are you then passing that result to total()? There's nothing for total() to total since you're only passing it one variable. I'm guessing what you're trying to do is display the result, but there's no reason to co-mingle the summation that total() is doing with the printing. It's good programming practice to separate functions that calculate a value from those that handle UI - total() should be declared with an int return type and should return its result. A separate method can then print that result.
     
  7. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    Thanks vpoko,

    Sorry for the mess, I myself got confused by the way I explained everything, I obviously understand what I'm trying to do because I know my project but simplifying the whole story made it too hard.

    Well, I have two sensors. I get raw data from the sensors and I must derive some important quantities from them such as voltage, angles in degrees, etc.

    I want to use several methods that will do each task separately. e.g 'A()' will calculate voltage, 'B()' will calculate angles, and so on.

    I'm using a third method 'C()' that stores all the data coming from A() and B() and that information is being stored in an arraylist, in 'C()'.

    For simplicity I'm using only three methods to explain what the problem is about, A(), B() and C(). But in my program, I have many more methods for "derivation" purposes, not only A() and B() like in the example above. So in my program there's one method whose only function is to get all the data, store it in an array and save the information in a file. Having said that, my original question was, is that possible? is it possible for a method to receive arguments from many different methods?

    I hope it's a bit clearer now, thank you for your help, you guys gave me a few ideas.
     
  8. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    Because I could normally use return methods for this purpose but the problem is that some of the methods return more than one variable, unless there's a way for a return method to return multiple variables?
     
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    Yes, you have to put the multiple return values into some kind of collection.

    For example, if you want to return two ints, you can create a class with the two ints as members:
    Code ( (Unknown Language)):
    1.  
    2. class myVars
    3. {
    4. public int a;
    5. public int b;
    6. }
    7.  
    Then, in your total() method, create an instance of myVars and save your local variables to its members:
    Code ( (Unknown Language)):
    1.  
    2. myVars rvals = new myVars();
    3. rvals.a = a;
    4. rvals.b = b;
    5.  
    You can then return rvals (your total() method should be defined with myVars as the return type).
     
Loading...