0

I'd like to run multiple methods using the same loop, but I'm running into a problem.

Let's say I have the following method:

void xy1() {
int y1 = 0;
    for (int x = 0; x < 10; x++){
    y1 += x;
    }
}

Works fine, all I have to do is call on it in the constructor.

Now, let's say I also want to use another method, but with a different initial value of y:

void xy2() {
int y2 = 1;
    for (int x = 0; x < 10; x++){
    y2 += x;
    }
}

Also works fine.

What I want however, is to have both methods run at the same time, in the same loop. An obvious solution would be to merge them into one method:

void xy (){
int y1 = 0;
int y2 = 1;

for (int x = 0; x < 10; x++){
 y1 += x;
 y2 += x;
    }
}

This gets messy fast though, when more and more variables are introduced. So what I tried doing was putting the loop in the constructor, and have it call on the methods every cycle:

Constructor (){
int y1 = 0;
int y2 = 1;

for (int x = 0; x < 10; x++){
xy1(y1, x);
xy2(y2, x);
   }
}

With the methods looking like this:

void xy1(int y1, int x) {
 y1 += x;
}

void xy2(int y2, int x) {
 y2 += x;   
}

The problem with this is of course that every time the constructor calls on the methods, it simply passes down the initial values of y1 and y2, not the values that they should currently have. Defining the initial values inside the methods would just cause them to be reset in the same way each cycle.

So what I need, is for the methods to somehow remember the last value of y for the new calculation. I feel like there's an obvious solution I'm missing... I've tried using google but I don't really know what search terms to use.

And while we're at it, since the calculations performed by the methods are the same, it would be great if I could simply define a single method xy, and have the constructor use it with different initial y values (in the same loop). The problem with this would be that there would have to be separate instances of y, so prevent one y being manipulated by two instances of the method (Did that make any sense? This is a problem that I expect to run into, so I'm not entirely sure what shape it'll take. I can always create a new question.)

Thanks for any help!

5
  • 4
    What you're missing to make this work is y1 = xy1(y1, x); y2 = xy2(y2, x); in the Constructor so that you record the updated values of the variables. Commented Dec 22, 2017 at 15:39
  • 1
    Maybe return y1 + x;? What are you actually trying to solve? Those methods don't seem to do anything useful. Why do you even want those to operate "in the same loop" instead of calling both of the original methods one after the other? Commented Dec 22, 2017 at 15:43
  • 2
    I feel there's a design problem in your question, at least phrases like "every time the constructor calls on the methods", "Defining the initial values inside the methods would just cause them to be reset in the same way each cycle", "methods to somehow remember the last value of y for the new calculation" etc. make my spidey-sense tingle. Is this for learning purposes only or can you describe what you're actually trying to achieve (also see xy-problem). Commented Dec 22, 2017 at 15:46
  • To elaborate a bit: "The problem with this is of course that every time the constructor calls on the methods, it simply passes down the initial values of y1 and y2, not the values that they should currently have." - a constructor is meant to create an object and set the object's initial value. If you want to have a different initial value for the object you'd best pass it as a parameter to the constructor. Commented Dec 22, 2017 at 15:49
  • Sorry, I should've provided some context. I created a physics simulation of a pendulum, and wanted to simulate multiple pendulums simultaneously. I simplified the problem as much as I could to make it more understandable to myself and others. Commented Dec 22, 2017 at 17:01

4 Answers 4

1

You would need to change the signature of the method to return its calculated value as follows.

int Calc(int InitialValue, int Increment){
    int Result = InitialValue + Increment;
    return Result;
}

After doing so, the constructor would have to be changed to assign the calculated values to its local variables as follows.

Constructor(){
    int y1 = 0;
    int y2 = 1;

    for (int x = 0; x < 10; x++){
        y1 = Calc(y1, x);
        y2 = Calc(y2, x);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, delegating a single += to another method is unnecessary to say the least, but I'm just going to act like it's a more meaning operation done within the method.

Code:

Constructor (){
    int y1 = 0;
    int y2 = 1;

    for (int x = 0; x < 10; x++){
        y1 = xy(y1, x);
        y2 = xy(y2, x);
    }
}

int xy(int y, int x) {
    return y + x;
}

Passing a value like xy(y1,x) will create a copy of both for the xy method, thus in your second method you will be performing the += operation on local copies, not the actual value you want to increase.

Comments

0

I am a newbie in programming but I feel that it's better to use an object and describe every event for the object, by doing this your code becomes shorter and clearer.

1 Comment

Being a Senior after n years I am reading my old comment and cannot hold a laugh :D
0

You declare and instantiate the y1 and y2 variables in the beginning of the method, before entering the for-loop. Therefore, when you enter the for-loop the value of y1 and y2 will be 0, but afterward it will take the updated value of y1 and y2 because you are not declaring it again.

If you call the methods xy1 and xy2 inside the for-loop because you are declaring the variables inside the methods xy1 and xy2, the values of y1 and y2 will be the initial ones because the variables are being declared over and over again every time the loop gets executed.

A possible solution will be this one:

public void calculate() {

    int y1 = 0;
    int y2 = 1;

    for(int i = 0; i < 10; i++) {
        y1 += i;
        y2 += i;
        System.out.printf("y1: %d, y2: %d \n", y1, y2);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.