CS110 Lecture 7 February 17, 2004 • Announcements – hw3 due Thursday • Agenda – – – – – – questions hw3 tips getters and setters – information hiding delegation Shapes application boxes and arrows Lecture 7 1 hw3 • • • • Practice new Java vocabulary (Lens.java) Improve TextFile class Draw box-and-arrow pictures Explore the Java API Lecture 7 2 getters and setters • Good private String contents; public String getContents() public void setContents (String contents) aTextFile.setContents(“foo”) in client class • Watch naming conventions • Bad (public access to field itself) public String contents; aTextFile.contents = “foo” in client class Lecture 7 3 getters and setters • Hide implementation details from TextFile clients • setContents(String contents) (line 51) – sets value of field and … – changes modification date – practice using this • int getSize() (line 97) – looks like a getter but … – there is no size field - code delegates the job Lecture 7 4 Delegation • Pass along the message, asking some other object to do the work for you • Important OO design pattern The King asked The Queen, and The Queen asked The Dairymaid: "Could we have some butter for The Royal slice of bread?" A. A. Milne, “The King’s Breakfast”, http://ingeb.org/songs/thekingb.html Lecture 7 5 this • • • Keyword for the object we are looking at Tricky - takes getting used to Settles ambiguity in variable names: 40 this.contents = contents; declared on line 25 on line 37 • Send a message to yourself 76 this.setContents(contents+text); is the same as 76 setContents(contents+text); (don’t forget that it is a message: this is implicit) Lecture 7 6 String tricks • ("hello, " + "world"). equals("hello, world") • + concatenates Strings • remember to send equals message , don’t test with == • Java can sometimes guess what you mean, converting a number to a String: ("balance: $" + 100). equals("balance: $100") Lecture 7 7 Shapes • Character graphics on your terminal A 20x10 Screen with 3 HLines: ++++++++++++++++++++++ +RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB + + + + + + + + + + + + + + + ++++++++++++++++++++++ draw 3 Boxes (2 overlapping): ++++++++++++++++++++++ + + + RRRR + + RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG + + GGGGGGG + + + ++++++++++++++++++++++ Lecture 7 8 Shapes classes • Particular shapes: – horizontal line: class HLine – box: class Box – VLine, Frame, Triangle (hw4) • Shapes are clients for Screen – Use Screen API (javadoc) – Don’t look at source code • TestShapes is a test driver (client) for HLine and Box Lecture 7 9 Screen API (javadoc) Lecture 7 10 TestShapes • Client for Screen, HLine, Box, self documenting • interesting code fragments 28-31: create a Screen, declare and create two HLines 32,33 : paintOn message to HLine wants Screen and position as arguments: “ask the HLine to paint itself on a Screen” - Screen is invisible still 34: creates an anonymous new HLine which is then asked to paint itself on the Screen 35: draw message to Screen gets Terminal as an argument “ask the Screen to draw itself on a Terminal” – finally, everything is visible Lecture 7 11 Variables and Values (review) • Variable: named place to hold a value of a particular type • Kinds of variables: fields (instance variables), local variables in methods, parameters • Variables must be declared before use • Type is either: – primitive (int, char, boolean,...) – reference to an instance (object) of some class • Why “reference to” ? Draw pictures ... Lecture 7 12 Boxes and Arrows • Draw a picture of a variable - box with narrow border, showing name and type type name: • If type is primitive, show value inside box int accountNumber: 2 • If type is a class then value is a reference to an object ... Lecture 7 13 Boxes and Arrows • Draw a picture of an object - box with thick border, showing type, containing fields (which are just variables) HLine int length: 3 char paintChar: ‘x’ • The object’s methods are not part of this picture! Lecture 7 14 Boxes and Arrows HLine h0 = new HLine(3,‘x’); HLine h1; HLine HLine h0: HLine h1: null int length: 3 char paintChar: ‘x’ • Value of h0 is a reference to (arrow to) an HLine object (which wouldn’t fit into the h0 box in any case) • Value of h1 is null (reserved word but not a keyword) Lecture 7 15 How References Work h1 = h0; h0.setLength(9); • Variables h0 and h1 refer to the same HLine instance • The HLine referred to by h1 sees the change since it’s the same HLine HLine HLine h0: HLine h1: int length: 9 char paintChar: Lecture 7 ‘x’ 16 Reference values h0 = new HLine(5,‘y’); HLine HLine h0: int length: 5 char paintChar: ‘y’ HLine HLine h1: int length: 9 char paintChar: ‘x’ Lecture 7 17 Reference values h0 = h1; HLine h0: HLine Now no variable refers to this HLine - it’s ready for garbage collection int length: 5 char paintChar: ‘y’ HLine HLine h1: int length: 9 char paintChar: ‘x’ Lecture 7 18 Bank main - Boxes and Arrows Bank javaBank: Bank “Engulf and Devour” BankAccount String bankName: int balance: 999 BankAccount account1: BankAccount account2: Terminal BankAccount int 200 balance: 200 Terminal atm: Lecture 7 19 Shapes • Character graphics on your terminal A 20x10 Screen with 3 HLines: ++++++++++++++++++++++ +RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB + + + + + + + + + + + + + + + ++++++++++++++++++++++ draw 3 Boxes (2 overlapping): ++++++++++++++++++++++ + + + RRRR + + RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG + + GGGGGGG + + + ++++++++++++++++++++++ Lecture 7 20 Counting • 1,2,3,... (everyday, mathematics) • 0,1,2,... (computer science) • Screen models (x,y) coordinates – – – – y value increases as you read down (0,0) is upper left hand corner Each location holds one pixel – a character Frame of +’s is not part of Screen • 5 3 Screen with G at position (3,1), & at position (0,2) Lecture 7 0 1 2 + + + + 0 + 1 + 2 + & + + + + 3 4 + + + + G + + + + +21 for loop start test step for (int i = 0; i < 5; i=i+1) { System.out.println(2*i + 1); body } Prints 1, 3, 5, 7, 9 on successive lines – do start – if test is true do body do step go back and test again – else loop is done, so do first line after body • Use a for loop when you know how many repetitions you want (else use while loop) • See ForDemo.java in JOI Lecture 7 22 for loop • HLine paintOn() method (lines 47,48) for ( int i = 0; i < length; i++ ){ s.paintAt( x+i , y, paintChar ); } • Counts from i = 0 to i = length-1, executing what’s in the body each time – – – – i=0: ask Screen s to put paintChar at (x,y) i=1: ask Screen s to put paintChar at (x+1,y) i=2: ask Screen s to put paintChar at (x+2,y) and so on … at (x+length-1,y) Lecture 7 23 for loop for ( int i = 0; i < length; i++ ){ s.paintAt( x+i , y, paintChar ); } • Variable i is declared inside for statement • Surround body with braces {...}for safety • i++ is short for i = i+1 (or i += 1) • Can do the same job other ways: for (int col=x+len-1; col >=x; col-- ){ s.paintAt( col , y, paintChar ); } Lecture 7 24 for and while • while can replace for: start int i = 0; while (i < 3) { System.out.println(i); i = i + 1; } • for can replace while: boolean more = true; while ( more ) { // do something more = ask(); test for(int i=0;i<3;i++){ //ditto } step note empty start step for( ; ask(); ) { // do something } } • For loop advantages: – fewer lines, control all on one line, elegant, idiomatic Lecture 7 25 Signatures • HLine paintOn messages in HLine unit test (main) – line 116: hline1.paintOn(screen) – line 118: hline1.paintOn(screen, 0, 1) • Two declarations for paintOn in HLine.java: – line 45: paintOn(Screen, int, int) – line 58: paintOn(Screen) delegates work to first paintOn • JVM uses shape of message to select method • Signature: method name & types of parameters Lecture 7 26