lecture 71 cs110 lecture 8 february 19, 2004 announcements –hw3 due tonight –hw4 available, due...

19
Lecture 7 1 CS110 Lecture 8 February 19, 2004 Announcements hw3 due tonight hw4 available, due Thursday February 26 exam Tuesday March 2 Agenda questions Shapes application counting signatures scope classes vs objects how parameters really work

Upload: magnus-austin

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 1

CS110 Lecture 8February 19, 2004

• Announcements– hw3 due tonight – hw4 available, due Thursday February 26– exam Tuesday March 2

• Agenda– questions– Shapes application– counting– signatures– scope– classes vs objects– how parameters really work

Page 2: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 2

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 +

+ +

++++++++++++++++++++++

• Character graphics on your terminal

Page 3: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 3

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 + & + + + + + + + +

Page 4: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 4

for loop start test stepfor (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

Page 5: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 5

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)

Page 6: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 6

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

}

Page 7: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 7

• while can replace for: int i = 0; while (i < 3) { for(int i=0;i<3;i++){ System.out.println(i); //ditto i++; } }

• for can replace while: //ask() returns boolean boolean more; for( ; ask(); ) { while ( ask() ) { // do something // do something }

}

for and while

note emptystart step

starttest

step

body

Page 8: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 8

for and while• for loop better than while loop

– fewer lines– control all on one line at top of loop– elegant, idiomatic– natural when loop count known in advance

• while loop better than for loop– reads more like pseudocode (English)– natural when loop count not known in advance (set

somehow in body)

Page 9: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 9

Signatures• HLine paintOn messages in HLine unit test (main)

– line 106: hline1.paintOn(screen)– line 108: hline1.paintOn(screen, 0, 1)

• Two declarations for paintOn in HLine.java:– line 39: paintOn(Screen, int, int)– line 52: paintOn(Screen)

line 54 delegates work to first paintOn (could use this)

• JVM uses shape of message to select method

• Signature: method name & types of parameters

Page 10: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 10

Scope• scope of a variable or method: where its unadorned name is

visible to the program

• Usually: the block { … } where it’s declared

• Examples from HLine.java– scope of all fields: lines 15-114– scope of screen (line 100) : lines 100-113– scope of length (line 25): lines 25-29 – scope of i (line 41): lines 41-43

• Scope of a method is the class it’s declared in• public declaration does not change scope

Page 11: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 11

Scope

• To see a method or field outside its scope, qualify the name of the method or field:– account.getBalance()– System.out– this.contents

• But– account.balance

will fail because balance is private

Page 12: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 12

static• Java keyword for belongs to whole class rather

than to an instance of the class• Static things are rare, objects are common: too

much static is bad design• public static void main( )

– main() is a static method - it can run before any objects are created with new

– TestShapes (like many testing programs) is all static: there is a TestShapes class, but never a TestShapes object (although main uses objects

Page 13: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 13

What can main() see?

• HLine is meant to be a client class– private fields– public getters, setters, other methods as appropriate

• HLine has a static main method, for unit testing• main in HLine

– can’t refer to length field or paintOn method, since those belong only to HLine objects

– can instantiate an HLine object, and then send it a paintOn message

Page 14: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 14

Static tools in the library• To invoke a static method, send a message to the

class (there is no object) - syntax ClassName.methodName( args )

• Math.sqrt( x )• Calendar.getInstance()

a factory method -Java designers chose this rather than new Calendar

• UnitTest.java line 21: HLine.main(args) sends message to HLine class to run main() there

Page 15: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 15

static fields• Syntax for accessing static field: ClassName.fieldName (e.g. System.out ) (no System constructor, no System object)

• Like global variables (in other languages)• In Integer class (part of Java API)

public static final int MAX_VALUE = 2147483647;• final: Java keyword for “can’t be changed”

int big = Integer.MAX_VALUE; // OKInteger.MAX_VALUE = 255; // error

• Naming convention for final fields: ALL_CAPS

Page 16: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 16

How parameters really work• Box.java line 143 sends a message: box2.paintOn( screen, 2, 2 );

• Execution shifts to method at Box.java line 52: public void paintOn( Screen s, int x, int y)

• Value of parameter– s in method is value of screen in message– x in method is (first) 2 in message– y in method is (second) 2 in message

Page 17: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 17

How parameters really work• Name of parameter (s) in method declaration need not

match the name of the value in the message (screen) • You can’t even think they should match:

– The value in the message might not even have a name (the 2 in the example)

– The method can be written before the client (in some other class) has even been imagined - and the client programmer does not have access to the source code with the method declaration

• The type of the value in the message must match the type in the method declaration

Page 18: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 18

In Box main

screen:

Screen Screen

box1:

Box Box

box2:

Box Box

width:int3

width: 4

pntCh: 'G'

int

char

Page 19: Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions

Lecture 7 19

In Box paintOnscreen:

Screen Screen

box1:

Box Box

box2:

Box Box

s:

Screen

x:

int2

this:

Box

y:

int2

width:int3

width: 4

pntCh: 'G'

int

char

out of scope