Reputation: 21
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!
Upvotes: 0
Views: 1806
Reputation:
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);
}
}
Upvotes: 0
Reputation: 123
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.
Upvotes: 0
Reputation: 126
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.
Upvotes: 0
Reputation: 17605
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);
}
}
Upvotes: 1