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

hw3
Practice new Java vocabulary (Lens.java)
Improve TextFile class
Draw box-and-arrow pictures
Explore the Java API

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

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

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

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
 setContents(contents+text);
(don’t forget that it is a message: this is implicit)

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")

Shapes
A 20x10 Screen with 3 HLines:
++++++++++++++++++++++
+RRRRRRRRRR          +
+GGGGGGGGGGGGGGG     +
+BBBBBBBBBBBBBBB     +
+                    +
+                    +
+                    +
+                    +
+                    +
+                    +
+                    +
++++++++++++++++++++++
draw 3 Boxes (2 overlapping):
++++++++++++++++++++++
+                    +
+ RRRR               +
+ RRRR               +
+ RGGGGGGG           +
+  GGGGGGG           +
+  GGGGGGG  GGGGGGG  +
+  GGGGGGG  GGGGGGG  +
+           GGGGGGG  +
+           GGGGGGG  +
+                    +
++++++++++++++++++++++

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

Screen API (javadoc)

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

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 ...

Boxes and Arrows
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
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
  HLine h0 = new HLine(3,‘x’);
  HLine h1;

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

Reference values
h0 = new HLine(5,‘y’);

Reference values
h0 = h1;

Bank main - Boxes and Arrows

Shapes
A 20x10 Screen with 3 HLines:
++++++++++++++++++++++
+RRRRRRRRRR          +
+GGGGGGGGGGGGGGG     +
+BBBBBBBBBBBBBBB     +
+                    +
+                    +
+                    +
+                    +
+                    +
+                    +
+                    +
++++++++++++++++++++++
draw 3 Boxes (2 overlapping):
++++++++++++++++++++++
+                    +
+ RRRR               +
+ RRRR               +
+ RGGGGGGG           +
+  GGGGGGG           +
+  GGGGGGG  GGGGGGG  +
+  GGGGGGG  GGGGGGG  +
+           GGGGGGG  +
+           GGGGGGG  +
+                    +
++++++++++++++++++++++

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)

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

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)

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 );
}

for and while
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

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