{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

20 PointersObjects

20 PointersObjects - The Binky video The Binky video...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Binky video The Binky video Pointer Fun is a 3 minute video from the computer science dept. at Stanford 1 //The code from the Binky video //The class IntObj class { int value; int } IntObj IntObj x; y; // Allocate the pointers x and y // (but not the IntObj pointees) x = new IntObj(); new x.value = 42; x.value // Allocate an IntObj pointee // and set x to point to it // // Dereference x to store 42 in its //pointee //pointee y.value = 13;// CRASH -- y does not have a pointee yet y.value y = x; x; // Pointer assignment sets y to point to x's //pointee called an ALIAS //pointee y.value = 13; y.value // Dereference y to store 13 in its //(shared) pointee //(shared) 2 IntObj IntObj IntObj x; y; x y 3 x = new IntObj(); x y 4 x.value = 42; x 42 y 5 y.value = 13; y.value x 42 y 6 y.value = 13; y.value //Null Pointer exception x 42 y 7 y = x; x; x 42 y 8 y.value = 13; x 13 y 9 //What would //What println(x.value); //Display? x 13 y 10 //Answer 13 //Answer x 13 y 11 Change the code from the Binky video Change the code from the Binky video so that it produces this structure x 1 y 2 class IntObj class { int value; int } IntObj x; IntObj IntObj y; x = new IntObj(); x.value = 42; y = x; y.value = 13; y.value println(x.value); println(y.value); 12 Practice Quiz Question: Find the Practice Quiz Question: Find the Output if Buttons b1 and b2 are b1 and clicked in that order import interfascia.*; GUIController c; IFButton b1, b2; int nTimes = 0; SomeClass bob; void setup() {}//not shown class SomeClass class void draw(){}//not used { void actionPerformed(GUIEvent e) IFButton theButton; IFButton { SomeClass() SomeClass() nTimes++; nTimes++; { println( println( theButton = b2; theButton "clicked " + nTimes + " times"); "clicked } IFButton clicked = boolean mystery( boolean (IFButton)e.getSource(); (IFButton)e.getSource(); IFButton clicked) IFButton if(bob.mystery(clicked)==false) if(bob.mystery(clicked)==false) { { if(theButton==clicked) if(theButton==clicked) println("first"); println("first"); { } return true; return else else } { else else println("second"); println("second"); { } return false; return } } } } 13 “dot equals” vs. “double equals” = assigns two handles assigns two handles to the same object if(x == y)asks: “Are x and y referring to the same object?” if(x.equals(y))asks: “Do x and y have the same value?” 14 X & Y refer to the same object X & Y refer to the same x 2 y 15 X & Y have the same value X & Y have the same x 2 y 2 16 Practice Quiz Question: Practice Quiz Question: What is the output of this applet? SomeType one = new SomeType("one"); SomeType two = new SomeType("two"); void setup() { SomeType three = two; SomeType three.sMessage = new String("three"); three.sMessage println(one.sMessage + ", " + two.sMessage); println(one.sMessage three = one; three three.sMessage = new String("four"); three.sMessage println(one.sMessage + ", " + two.sMessage); println(one.sMessage } void draw(){}//not used class SomeType { String sMessage; String SomeType(String sText) SomeType(String { sMessage = sText; sMessage } } 17 SomeType one = new SomeType("one"); SomeType SomeType two = new SomeType("two"); one one two two 18 SomeType three = two; SomeType three.sMessage = new String("three“); System.out.println(one.sMessage + System.out.println(one.sMessage ", " + two.sMessage); two.sMessage); one one two three three 19 three = one; three three.sMessage = new String("four“); System.out.println(one.sMessage + System.out.println(one.sMessage ", " + two.sMessage); two.sMessage); one four two three three 20 primitives (likes ints) primitives are different than objects int n1 = 3; int n2 = n1; n2 = 7; println(n1); 21 There are two int There are "mailboxes" and each has a 3 int n1 = 3; int n2 = n1; n2 = 7; println(n1); 22 There are two int There are "mailboxes" and each has a 3 int n1 = 3; int n2 = n1; n2 = 7; println(n1); //displays 3 23 objects are created with class objects are created with class IntObj { int value; int } IntObj n1 = new IntObj(); n1.value = 3; IntObj n2 = n1; n2.value = 7; println(n1.value); 24 We've only created one We've only created new IntObj class IntObj { int value; int } IntObj n1 = new IntObj(); n1.value = 3; IntObj n2 = n1; n2.value = 7; println(n1.value); 3 25 We've only created one We've only created new IntObj class IntObj { int value; int } IntObj n1 = new IntObj(); n1.value = 3; IntObj n2 = n1; n2.value = 7; println(n1.value); //displays 7 7 26 find the output find the int n1 = 4; int n2 = n1; n2 = 2; println(n1); class IntObj { int value; int } IntObj n3 = new IntObj(); n3.value = 4; IntObj n4 = n3; n4.value = 2; println(n3.value); 27 null pointer exception null pointer exception Remember what happened to Binky? Don't let it happen to you! Make sure your pointers are all assigned to point to something If you try to use a pointer that isn't pointing to something, your program crashes 28 a nasty null pointer exception a Ship bob; void setup() { Ship bob = new Ship(); Ship //lots more java //lots } void actionPerformed() { IFButton shot=(IFButton)e.getSource(); IFButton if(bob.isHit(shot) == true) if(bob.isHit(shot) //lots more java //lots 29 How many bob pointers? How many Ship bob; void setup() { Ship bob = new Ship(); Ship //lots more java //lots } void actionPerformed() { IFButton shot=(IFButton)e.getSource(); IFButton if(bob.isHit(shot) == true) if(bob.isHit(shot) //lots more java //lots 30 Now it's fixed Now it's fixed Ship bob; void setup() { bob = new Ship(); bob //lots more java //lots } void actionPerformed() { IFButton shot=(IFButton)e.getSource(); IFButton if(bob.isHit(shot) == true) if(bob.isHit(shot) //lots more java //lots 31 Arrays of Objects Arrays of Objects A very powerful technique Let's say you have a planet class and want to model the solar system First, you'd declare an Array of Planets: Planet planets; 32 Arrays of Objects Arrays of Objects Then you would initialize the array Planet planets; void setup() { //other code not shown //other planets = new Planet[8]; planets 33 Arrays of Objects Arrays of Objects How many new Planets have been created? Planet planets; void setup() { //other code not shown //other planets = new Planet[8]; planets 34 Arrays of Objects Arrays of Objects None! All we have are 8 pointers. Planet planets; void setup() { //other code not shown //other planets = new Planet[8]; planets 35 Arrays of Objects Arrays of Objects We nee to loop through the array to "hook those pointers up" to new Planets Planet planets; void setup() { //other code not shown //other planets = new Planet[8]; planets for(int i=0; i<8; i++) for(int { planets[i] = new Planet(); planets[i] 36 Arrays of Objects Arrays of Objects Once the array is set up, you can loop through it to draw and move the planets void draw() { for(int i=0; i<8; i++) for(int { planets[i].move(); planets[i].move(); planets[i].show(); planets[i].show(); 37 An outline for the Snake game An outline for the Snake game Snake theSnake; //other variables not shown void setup() { size(600,410); size(600,410); theSnake = new Snake( ??? ); theSnake //lots more java //lots } void draw(){}//not shown void keyPressed() { theSnake.direction = keyCode; theSnake.direction } class Segment { //variables and functions not shown //variables } class Snake { Segment segments; Segment //variables and functions not shown //variables } 38 The Segment class The Segment class Represents one of the squares that form the body of the snake Should have everything that Segments have and do class Segment { ?? ?? } 39 The Segment class The Segment class What Segments have: x and y coordinates, a length for the side of the segment, and a color. What Segments do: get constructed and shown to the screen class Segment { ?? ?? } 40 The Segment class The Segment class What Segments have: x and y coordinates, a length for the side of the segment, and a color. class Segment { int x, y, length; int color snakeColor; color } 41 The Segment class The Segment class What Segments do: get constructed and shown to the screen class Segment { int x, y, length; int color snakeColor; color Segment() Segment() { x = ??? ; ??? y = ??? ; ??? length = ??? ; ??? snakeColor = ??? ; ??? } void show() {??? ;} void } 42 The Snake class The Snake class Represents the entire Snake Should have everything that Snakes have and do class Snake { ?? ?? } 43 The Snake class The Snake class Snakes have: An array of segments, a direction, the current length of the snake, and the maximum length of the snake class Snake { ?? ?? } 44 The Snake class The Snake class Snakes have: An array of segments, a direction, a color, the current length of the snake, and the maximum length of the snake class Snake { Segment segments; Segment int direction; int int currentLength; int int maxLength; int } 45 The Snake class The Snake class What Snakes do: They get constructred, moved and drawn to the screen class Snake { Segment segments; Segment int direction; int int currentLength; int int maxLength; int Snake() {??? } Snake() void move() {??? } void void show () {??? } void } 46 The Snake constructor The Snake constructor class Snake { Segment segments; Segment int direction; int int currentLength; int int maxLength; int Snake() { direction = ??? ; direction direction = ??? ; direction currentLength = ??? currentLength maxLength = ??? ; maxLength ; } } 47 Initializing the array of segments is probably Initializing the array of segments is probably the trickiest part of the constructor Snake() Snake() { //other code not shown //other segments = new Segment[maxLength]; segments for(int i = 0; i < maxLength; i++) for(int { segments[i] = new Segment()); segments[i] } //change the color of the head //change segments[0].snakeColor = color(255,255,0); color(255,255,0); } 48 How snakes move How snakes move Starting from the back of the snake: copy the x and y coordinates of the next segment. Stop when you get to the segment behind the head The head coordinates change depending on the direction of travel (5,2) (6,2) (5,2) (6,2) (7,2) (5,3) (5,3) (5,4) 49 Find the output Find the output char letters1 = {'b','c','d','e'}; char letters2 = {'b','c','d','e'}; letters1[0] = 'a'; for(int i = 1; i < letters1.length;i++) { letters1[i] = letters1[i - 1]; letters1[i] } println(letters1); for(int i = letters2.length-1; i > 0;i--) { letters2[i] = letters2[i - 1]; letters2[i] } letters2[0] = 'a'; 50 println(letters2); ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online