cs61b l02 using objects (1)garcia / yelick fall 2003 © ucb kathy yelick handout for today: these...

18
CS61B L02 Using Objects (1) Garcia / Yelick Fall 2003 © U Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects 2003-08-27

Upload: bryan-norman

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (1) Garcia / Yelick Fall 2003 © UCB

Kathy Yelick

Handout for today: These lecture notes

Computer Science 61BLecture 2 – Using Objects

2003-08-27

Page 2: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (2) Garcia / Yelick Fall 2003 © UCB

Design Problem Revisited• Want to build a web search engine like Google• How can we break this into smaller problems?

– Design a data structure to store keyword/urls– Design interface to respond to keyword queries– Build a web crawler to collect words on each page

» Open a connection» Process strings on that page

•Find title•Eliminate html junk•Eliminate useless words (the, and, is, …)

» Save results in some files

– And much more

Page 3: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (3) Garcia / Yelick Fall 2003 © UCB

How Long Does It Take?•Factoid: 23% of web pages change daily•How long does it take to crawl the full web?a) < 2 minutesb) < 2 hoursc) < 2 daysd) < 2 weeks

•How long did it take for the 61B web page changes to show up in Google’s own copy?a) < 2 minutesb) < 2 hoursc) < 2 daysd) < 2 weeks

Page 4: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (4) Garcia / Yelick Fall 2003 © UCB

Design by Wishful Thinking• Approach a problem using top-down

design– Break it into pieces– Assume you can build most of them– Concentrate on one at a time– When you’re sure you can build one, go onto the next

• Rule: don’t write any code until you know how to build the pieces– We won’t follow this rule in lecture

• Three parts of Java we’ll need in this design:– Strings, Web connections, Files (later)

Page 5: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (5) Garcia / Yelick Fall 2003 © UCB

Strings are Objects• Strings are objects: What does that

mean?– We can give them a name that will refer to the object

– We can send them messages to have them do things» Tell me your length» What is your first character?» In Java these are called methods: ~40 on strings

– It belongs to a class (the “factory” that created it)» We also call this the type of the object» All string objects are of type String

s“Hello World”

Page 6: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (6) Garcia / Yelick Fall 2003 © UCB

Box and Pointer Diagrams• To understand complicated data structures

draw pictures!• A variable is shown as a box with a name

– A variable may contain a primitive value (3, 2.5, etc.) – Or it may contain a reference to an object

» Drawn as a pointer» The variable “refers to” or “points to” the object

• An object is also a box– So far we have seen only string objects– We will see that there may be variables inside objects

s

Page 7: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (7) Garcia / Yelick Fall 2003 © UCB

Variables and Objects• Declare a variable

String s1;

s1

• Create an object by calling a constructor

s1 = new String();

“”

• Do both togethers2

String s2 = new String(“white”);

“white”

• Assign one to another (swing pointer)

s1 = s2;

X

Page 8: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (8) Garcia / Yelick Fall 2003 © UCB

Invoking Methods on Strings• Can invoke a string method using “.”• Strings are immutable: cannot modify

string objects• Setting one does not affect the other

s1“white”

s2

s2 = s2.concat(“house");

x “whitehouse”

s2 = s2.substring(0,5);

“white”x

•Wait: “house” is a String object, but it was not created with

“new String…”•Why? Because Strings are special

Page 9: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (9) Garcia / Yelick Fall 2003 © UCB

Equality on Strings

• Two notions of equality: == and .equals

Same object Different object, same value

s1“white”

s2

s1“white”

s2

“white”

(s1 == s2) is true

s1.equals(s2) is true

(s1 == s2) is false

s1.equals(s2) is true

s2 = s1 s2 = new String(s1)

Page 10: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (10) Garcia / Yelick Fall 2003 © UCB

Printing Strings• To output strings, use “println”

– Can concatenate them using “+”– Can also use + on string with another type

System.out.println("s1 is: " + s1); System.out.println("the length of s1 is "

+ s1.length());System.out.println(s1 + s2 + s1);

• Do you feel you know enough about strings in Java to write a program that finds keywords in a string representing a line on a web page?

Page 11: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (11) Garcia / Yelick Fall 2003 © UCB

Scheme vs. Java: DifferencesScheme Java

PunctuationComments ; // or /*

*/Separators

( ) ,;.{ } ( )

Arithmetic prefix infixAssignment

set! =

TypesImplicit:list, atom, function

Explicitint, char, boolean, …

Page 12: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (12) Garcia / Yelick Fall 2003 © UCB

Scheme to JavaScheme Java

Kinds of ValuesPrimitive atom int, char,

boolean …Non-primitive built-in

Lists String,URL,…

Non-primitive user defined

Trees, etc. NewTree,…

Invoking FunctionsWith objects (ask o ‘m arg …) o.m( arg …)

Without objects

(f arg …) f( arg …)

Page 13: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (13) Garcia / Yelick Fall 2003 © UCB

Announcements• Lab 1 and homework 1 are online

– Homework 1 is due at 11:59 Tuesday night– Homeworks will be auto-graded

• Textbooks were at Ned’s yesterday– Reading assignments online (do not need to know

html)• The CS61B Course Reader

– Available at Copy Central on Euclid/Hearst– Contains “Scheme to Java”, tool documentation, etc.

• Labs and discussions– You must attend your assigned lab (change through

Telebear if you must)– You may attend a different discussion, although the

one that matches your lab has the same TA

Page 14: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (14) Garcia / Yelick Fall 2003 © UCB

Input/Output (I/O) in Java• To build a web crawler we need: Read

from files across the web.• We’ve seen one simple form of output

–System.out.println(“some string”);–System.out is a PrintStream that prints to the

screen

• What about input? Does System have anything?–http://java.sun.com/j2se/1.4.1/docs/api/–System.in is an InputStream from the keyboard.– And there’s a URL class with an openStream method,

which creates an InputStream to the web– But you can’t read a line of text from an InputStream

Page 15: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (15) Garcia / Yelick Fall 2003 © UCB

Input/Output (I/O) in Java

We know how to construct an InputStream

An InputStreamReader is one kind of Reader

We want to construct a BufferedReader

Need to have a Reader to call the constructor

To construct an InputStreamReader we can use an InputStream

Page 16: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (16) Garcia / Yelick Fall 2003 © UCB

Why So Complicated?• InputStream objects (like System.in)

– read raw data, but don't format the data.

• InputStreamReader objects – compose the raw data into characters.

• BufferedReader objects – compose the characters into lines of text.

• Why are these tasks divided among three different classes? – So one can reimplement part (for efficiency)– Can reuse some parts of code for different kinds

of I/O (file, network, user, …)

Page 17: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (17) Garcia / Yelick Fall 2003 © UCB

Code for Accessing the Webclass WHWWW { public static void main(String[] arg) throws Exception { URL u = new URL("http://www.whitehouse.gov/"); InputStream ins = u.openStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader whiteHouse = new BufferedReader(isr); System.out.println(whiteHouse.readLine()); }}

Page 18: CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (18) Garcia / Yelick Fall 2003 © UCB

Summary• Break large designs into pieces

– Top-down design– Concentrate on one piece at a time

• Java and Scheme– Many syntactic differences– Many semantic similarities

• Strings and I/O Streams– Java Strings and Streams are objects– Use the online reference to the Java library

• Understanding objects– Draw pictures!