lec04-VariablesAndMethods-after.pdf

Int x 3 x x 1 cs concept references what does this

Info icon This preview shows pages 13–22. Sign up to view the full content.

int x = 3; x = x + 1;
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CS Concept: References What does this code draw? World world1 = new World(200,100); Turtle maria = new Turtle(25, 25, world1); Turtle jose = new Turtle(100, 50, world1); maria = jose; maria.forward(50); jose.turn(90); jose.forward(50); 1) SOLO VOTE (30 secs) 2) Discuss in team (1 min) 3) GROUP VOTE (20 sec) A B C D >> int x = 42; >> int y = x; >> x = 101; >> S.o.p(x); 101 >> S.o.p(y); 42 Y doesn't change? So why does maria's turtle move when you move jose??
Image of page 14
Memory Model misconception x y int x = 42; int y = 75; y = x; x = 101; Turtle maria = new Turtle(25, 25, world1); Turtle jose = new Turtle(100, 50, world1); maria = jose; jose.foward(100); maria jose
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Turtle maria = new Turtle(25, 25, world1); Turtle jose = new Turtle(100, 50, world1); maria jose maria is a variable with type Turtle. maria stores a reference to a Turtle object jose is a variable with type Turtle. jose stores a reference to a different Turtle object Object types store references!
Image of page 16