CS110 Lecture 7 February 17, 2004 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CS110 Lecture 7 February 17, 2004

Description:

getters and setters information hiding. delegation. Shapes application. boxes and arrows ... Hide implementation details from TextFile clients. setContents ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 27
Provided by: ethanb8
Category:

less

Transcript and Presenter's Notes

Title: CS110 Lecture 7 February 17, 2004


1
CS110 Lecture 7February 17, 2004
  • Announcements
  • hw3 due Thursday
  • Agenda
  • questions
  • hw3 tips
  • getters and setters information hiding
  • delegation
  • Shapes application
  • boxes and arrows

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

3
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
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
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 Kings Breakfast,
    http//ingeb.org/songs/thekingb.html

6
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(contentstext)
  • is the same as
  • setContents(contentstext)
  • (dont forget that it is a message this is
    implicit)

7
String tricks
  • ("hello, " "world"). equals("hello,
    world")
  • concatenates Strings
  • remember to send equals message ,
    dont test with
  • Java can sometimes guess what you mean,
    converting a number to a String
  • ("balance " 100).
    equals("balance 100")

8
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

9
Shapes classes
  • Particular shapes
  • horizontal line class HLine
  • box class Box
  • VLine, Frame, Triangle (hw4)
  • Shapes are clients for Screen
  • Use Screen API (javadoc)
  • Dont look at source code
  • TestShapes is a test driver (client)
    for HLine and Box

10
Screen API (javadoc)
11
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
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
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 ...

type
name
int
accountNumber
2
14
Boxes and Arrows
  • Draw a picture of an object - box with thick
    border, showing type, containing fields
    (which are just variables)
  • The objects methods are not part of this
    picture!

15
Boxes and Arrows
  • HLine h0 new HLine(3,x)
  • HLine h1

HLine
h0
HLine
h1
null
  • Value of h0 is a reference to (arrow to) an HLine
    object (which wouldnt fit into the h0 box in any
    case)
  • Value of h1 is null (reserved word but not a
    keyword)

16
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
    its the same HLine

HLine
h0
HLine
h1
17
Reference values
  • h0 new HLine(5,y)

HLine
h0
HLine
h1
18
Reference values
Now no variable refers to this HLine - its ready
for garbage collection
  • h0 h1

HLine
h0
HLine
h1
19
Bank main - Boxes and Arrows
Bank
javaBank
Engulf and Devour
Bank
BankAccount
String
int
bankName
balance
999
BankAccount
account1
BankAccount
200
BankAccount
int
balance
account2
200
Terminal
Terminal
atm
20
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

21
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
for loop
  • start test
    step
  • for (int i 0 i lt 5 ii1)
  • System.out.println(2i 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
for loop
  • HLine paintOn() method (lines 47,48)
  • for ( int i 0 i lt length i )
    s.paintAt( xi , y, paintChar )
  • Counts from i 0 to i length-1, executing
    whats in the body each time
  • i0 ask Screen s to put paintChar at (x,y)
  • i1 ask Screen s to put paintChar at (x1,y)
  • i2 ask Screen s to put paintChar at (x2,y)
  • and so on at
    (xlength-1,y)

24
for loop
  • for ( int i 0 i lt length i )
    s.paintAt( xi , y, paintChar )
  • Variable i is declared inside for statement
  • Surround body with braces ...for safety
  • i is short for i i1 (or i 1)
  • Can do the same job other ways
  • for (int colxlen-1 col gtx col-- )
    s.paintAt( col , y, paintChar )

25
for and while
  • while can replace for
  • int i 0
  • while (i lt 3) for(int i0ilt3i)
  • 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

test
start
step
note empty start step
26
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
Write a Comment
User Comments (0)
About PowerShow.com