Download presentation
Presentation is loading. Please wait.
Published byJames George Modified over 8 years ago
1
Lecture 71 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
2
Lecture 72 hw3 Practice new Java vocabulary (Lens.java) Improve TextFile class Draw box-and-arrow pictures Explore the Java API
3
Lecture 73 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
4
Lecture 74 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
5
Lecture 75 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
6
Lecture 76 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)
7
Lecture 77 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")
8
Lecture 78 Shapes A 20x10 Screen with 3 HLines: ++++++++++++++++++++++ +RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB + + ++++++++++++++++++++++ draw 3 Boxes (2 overlapping): ++++++++++++++++++++++ + + RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG + + ++++++++++++++++++++++ Character graphics on your terminal
9
Lecture 79 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
10
Lecture 710 Screen API (javadoc)
11
Lecture 711 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
12
Lecture 712 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...
13
Lecture 713 Draw a picture of a variable - box with narrow border, showing name and type If type is primitive, show value inside box If type is a class then value is a reference to an object... Boxes and Arrows name: type accountNumber: int 2
14
Lecture 714 Draw a picture of an object - box with thick border, showing type, containing fields (which are just variables) The object’s methods are not part of this picture! Boxes and Arrows paintChar: 3 ‘x’ length: int char HLine
15
Lecture 715 Boxes and Arrows HLine h0 = new HLine(3,‘x’); HLine h1; h0: h1:null HLine paintChar: 3 ‘x’ length: int char HLine 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)
16
Lecture 716 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 h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine
17
Lecture 717 Reference values h0 = new HLine(5,‘y’); h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine paintChar: 5 ‘y’ length: int char HLine
18
Lecture 718 Reference values h0 = h1; Now no variable refers to this HLine - it’s ready for garbage collection h0: h1: HLine paintChar: 9 ‘x’ length: int char HLine paintChar: 5 ‘y’ length: int char HLine
19
Lecture 719 Bank main - Boxes and Arrows “Engulf and Devour” int 200 BankAccount balance: BankAccount balance: Terminal String bankName: Bank BankAccount account1: BankAccount account2: Terminal atm: 999 200 int Bank javaBank:
20
Lecture 720 Shapes A 20x10 Screen with 3 HLines: ++++++++++++++++++++++ +RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB + + ++++++++++++++++++++++ draw 3 Boxes (2 overlapping): ++++++++++++++++++++++ + + RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG + + ++++++++++++++++++++++ Character graphics on your terminal
21
Lecture 721 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) 0 1 2 3 4 + + + + + + + 0 + + 1 + G + 2 + & + + + + + + + +
22
Lecture 722 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
23
Lecture 723 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)
24
Lecture 724 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 ); }
25
Lecture 725 while can replace for : int i = 0; while (i < 3) { for(int i=0;i<3;i++){ System.out.println(i); //ditto i = i + 1; } } for can replace while : boolean more = true; for( ; ask(); ) { while ( more ) { // do something // do something } more = ask(); } For loop advantages: –fewer lines, control all on one line, elegant, idiomatic for and while note empty start step start test step
26
Lecture 726 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.