If you call this first method, ‘CreateLotsOfAlphas’, what is it supposed to print? I’m just having trouble following the flow of the program. I thought it would print aabbc, but for some reason it actually prints bacbc.
My reasoning is that newA1.y is just the input, a, at first due to the null. a is saved into this.y, so newA2.y is (a + b) with b saved into this.y, then newA3.y is (b + c) to give aabbc.
Am I looking at this wrong or something?
public void CreateLotsOfAlphas() {
Alpha newA1 = new Alpha(1.0, "a", null);
Alpha newA2 = new Alpha(2.0, "b", newA1);
Alpha newA3 = new Alpha(3.0, "c", newA2);
System.out.println(newA1.y + newA2.y + newA3.y);
}
These two methods are in two different classes by the way.
public Alpha(double x, String y, Alpha oldAlpha) {
this.x = x;
this.y = y;
w = (int) x;
if (oldAlpha != null) {
oldAlpha.y = y + oldAlpha.y;
}
}
… clear enough?