Problem with chaining *confusing results* | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem with chaining *confusing results*

I'll post entire code: "public class Ball { private double x,y,xStep,yStep; public Ball(double x, double y, double xStep, double yStep) { this.x = x; this.y = y; this.xStep = xStep; this.yStep = yStep; } public double getX() { return this.x; } public double getY() { return this.y; } public double getXStep() { return this.xStep; } public double getYStep() { return this.yStep; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public void setXStep(double xStep) { this.xStep = xStep; } public void setYStep(double yStep) { this.yStep = yStep; } @Override public String toString() { return "Ball{" + "x=" + x + ", y=" + y + ", xStep=" + xStep + ", yStep=" + yStep + '}'; } public double []getXY(){ double[]results=new double[2]; results[0]=this.x; results[1]=this.y; return results; } public void setXY(double x,double y){ this.x=x; this.y=y; } public double[]getXYStep(){ double []results=new double[2]; results[0]=this.xStep; results[1]=this.yStep; return results; } public void setXYStep(double xStep,double yStep){ this.xStep=xStep; this.yStep=yStep; } public Ball move(){ x+=xStep; y+=yStep; return this; } } " public class TestBall { public static void main(String[] args) { Ball b1=new Ball(1,2,11,12); System.out.println(b1); b1.setXY(5,6); b1.setXYStep(15,16); System.out.println(b1); System.out.println("x is: " + b1.getXY()[0]); System.out.println("y is: " + b1.getXY()[1]); System.out.println("xStep is: " + b1.getXYStep()[0]); System.out.println("yStep is: " + b1.getXYStep()[1]); System.out.println(b1.move()); System.out.println(b1.move().move().move()); System.out.println(b1.move().move()); } }" *results for .move* Ball{x=20.0, y=22.0, xStep=15.0, yStep=16.0} Ball{x=65.0, y=70.0, xStep=15.0, yStep=16.0} Ball{x=95.0, y=102.0, xStep=15.0, yStep

10th Aug 2017, 12:01 AM
Yurodivi
3 Answers
+ 3
The reason each move is being passed to the next call is because of how the move method works. "return this" So, it's always returning the current object, which is always b1. So, b1 is being modified everytime. b1.move(); // modifies b1, returns b1. b1 gets printed. b1.move().move() // modifies b1, returns b1, then modifies what was returned. Which was b1. So, b1 is modified again. b1 gets printed
10th Aug 2017, 2:30 AM
Rrestoring faith
Rrestoring faith - avatar
0
I don't understand how does chaining works results for 3 x .move are less then when i call it twice!! ok so i was observing "System.out.println(b1.move());//x=20 System.out.println(b1.move().move().move());//here is x=20+15+15+15=65 System.out.println(b1.move().move());"//and here is x=65+15+15=95 So at first when i set xStep=15; that value goes to constructor move() use it and we get x=20; In next print that value of is 20 and it adds 3xStep on it! So what is new here for me or i'm just very tired and confused is that move() method passes old value of x to every next print. Ok ok soo method move() is keeping old value and passes it to next caller.
10th Aug 2017, 1:45 AM
Yurodivi
0
Thank you.
10th Aug 2017, 3:53 AM
Yurodivi