Upload
sreenivas-reddy-g
View
217
Download
0
Embed Size (px)
Citation preview
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 1/6
MIT OpenCourseWare
http://ocw.mit.edu
6.005 Elements of Software Construction
Fall 2008
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 2/6
(name, retur
whi ch
Classes
Rob Miller
Fall 2008
© Robert Miller 2008
Review: How To Write a Method/ *
* Ret urns t he content s of t he web page i denti f i ed
1. Write the method signature n type, arguments
*/publ i c stat i c Str i ng f etch(Str i ng url Str i ng) {
* by url Str i ng, must be a val i d URL.* e. g. f etch( "htt p: / / www. mi t . edu")
* r eturns t he MI T home page as a st r i ng of HTML.
. . .
}
2. Write a specification (a comment that defines what it returns, any side‐effects, and assumptions about the arguments.
3. Write the method body so that it conforms to your specification. (Revise the signature or specification if you discover you can’t implement it!)
© Robert Miller 2007
Today’s Topicsobject-oriented programming in Java
¾exceptions¾classes
© Robert Miller 2007
Getting Data from the Webi mport j ava. net . URL;
/ *
imports the class URL from the java.net package
* Ret urns t he cont ent s of t he web page i dent i f i ed
* by url St ri ng. e.g. f etch( "htt p: / / www. mi t . edu")
* r et urns t he MI T home page as a st r i ng of HTML. */
publ i c stat i c Str i ng f etch(Str i ng url Str i ng) {URL url = new URL(url St ri ng) ; . . .
}
constructs a new URL object
© Robert Miller 2007
1
1. Write the method signature
(name, return type, arguments
2. Write a specification (a comment
that defines what it returns, any side‐
effects, and assumptions about the
arguments.
3. Write the method body so that it conforms to your
specification. (Revise the signature or specification if
you discover you can’t implement it!)
imports the class URL from
the java.net package
constructs a new URL object
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 3/6
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 4/6
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 5/6
7/28/2019 MIT6_005f08_lec02
http://slidepdf.com/reader/full/mit6005f08lec02 6/6
Static Fields and Methods¾Fields and methods declared static are associated with the class itself,
rather than an individual object• A static field has only one value for the whole program (rather than
one value per object)
– All objects of the class share that single copy of the static field
• A static method has no this object
• Static methods and fields are referenced using the class name (e.g.
Web.fetch()) rather than an object variable
• Some classes are purely containers for static code (e.g. Hailstone,Web, java.lang.Math), and no objects of the class are ever constructed
methods¾static final is commonly used for constants, e.g.:
publ i c st at i c f i nal PI = 3. 14159;
© Robert Miller 2007
Using the Cache
publ i c Page( URL ur l ) t hrows I OExcept i on {
t hi s. ur l = ur l ;
Page p = getPageFromCache( url ) ;
i f (p != nul l ) {
t hi s. content = p. content ;
} el se {t hi s. content = Web. f etch(ur l ) ;
putPageI nCache( t hi s) ;
}}
Implementing the Cachepr i vate stat i c f i nal Page[] cache = new Page[ 100]; pr i vate stat i c i nt cachePoi nter = 0;
/ / i ndex of next page to r epl ace i n the cache
pr i vate stat i c Page getPageFromCache(URL ur l ) {
f or ( Page p : cache) {
i f (p != nul l && p. get URL( ) . equal s( url ) ) return p;
}
ret urn nul l ;
why might p be null? what happens if we don’t check?
/ / page not f ound
}pri vate stati c voi d put PageI nCache( Page page) {
cache[ cachePoi nter ] = page; ++cachePoi nter ; i f ( cachePoi nter >= cache. l engt h) cachePoi nter = 0;
}
© Robert Miller 2007
SummaryExceptions
¾Exceptions are abnormal returns from a method
¾Exceptions can be caught or declared
¾Members (fields, constructors, methods)¾Access control (public, protected, private) ¾Static members¾Overloading
© Robert Miller 2007 © Robert Miller 2007
5
why might p be null?
what happens if we don’t check?