Upload
sreenivas-reddy-g
View
213
Download
0
Embed Size (px)
Citation preview
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 1/8
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_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 2/8
Subclassing and Interfaces
Rob Miller
Fall 2008
© Robert Miller 2008
Getting Weather InformationLet’s build on Page to get weather conditions
¾Yahoo Weather feed: http://weather.yahooapis.com/forecastrss?p=02139
¾Returns an XML page that looks like this:
. . . <. . . t i t l e>Condi ti ons f or Cambri d e MA at 3: 54 m
, EDT</ t i t l e>. . . <yweat her : condi t i on t ext="Cl oudy"
code="26" t emp="82"date="Sat, 06 Sep 2008 3: 54 pmEDT" / >. . .
Some string manipulation can extract the current weather condition:
Page p = new Page( new URL( "ht t p:/ / weat her. . . =02139") ; St ri n weat her = Match. between( . etContent( ) ,
"<yweat her : condi t i on" , "/ >") ;
St ri ng condi t i on = Match. between(yweat her,"t ext =\ "" , " \ " " ) ;
© Robert Miller 2007
Today’s TopicsMore Java
¾subclassing¾ interfaces
¾collections
© Robert Miller 2007
Simple Pattern Matchingpubl i c cl ass Mat ch {
/ * Fi nds the f i rst match to pat tern i n st ri ng, and
* returns t he rest of t he str i ng. e. g. ,
* af t er( "Your Name: Ben", "Name: ") => "Ben“
* Returns nu i at te rn never occ urs i n st r i n . * /
publ i c stat i c Str i ng after( Str i ng str i ng, Str i ng pattern)
/ * Fi nds the f i rst match to pat tern i n st ri ng, and
* returns the part of t he str i ng before i t . e. g. ,* bef ore("hel l o/t here", "/ ") => "hel l o“
* Returns nul l i f patt ern never occurs i n str i ng. */
publ i c stat i c St ri ng bef ore(St ri ng content, St ri ng patt ern)
/ * F inds the f i rst match to l ef t i n str i ng, and
* the f i rst match to ri ght af t er t hat, and
* r etur ns t he subst r i ng between t he t wo mat ches.
* e. g. bet ween(" a <b>bol d</ b> word", "<b>", "</ b>") => "bol d“
* Returns nul l i f l ef t. . . r i ght never occurs i n str i ng. */
publ i c stat i c St ri ng between(St ri ng cont ent , Stri ng l ef t, Str i ng ri ght) © Robert Miller 2007
1
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 3/8
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 4/8
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 5/8
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 6/8
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 7/8
7/28/2019 MIT6_005f08_lec03
http://slidepdf.com/reader/full/mit6005f08lec03 8/8
c s t at c t ass o t
Switch Statement
switch tests a value against a set of cases¾equivalent to a sequence of if-else clauses¾ the value can be either an enum or integer type (int, char, etc.)
u l i c st at i c i nt e rees ComassPoi nt oi nt {
i nt resul t ;
swi t ch (poi nt) {
case NORTH: r esul t = 0; break;
case EAST: resul t = 90; break;
case SOUTH: resul t = 180; break;
case WEST: resul t = 270; break;
def aul t : t hrow new Runt i meExcepti on("i nval i d compass
The cases of a switch must be terminated by break, or they will fall through to the next case!
oi nt" ;}
return resul t ;
}default is like the else clause of a switch – it ’s good practice to always include one. Often it just throws an exception.
© Robert Miller 2007
Summary
Subclassing¾ Inheritance of fields and methods¾Declared type vs. actual type
¾DowncastingInterfaces
¾An interface captures the essence of a class: its method specifications
Packages
¾Packages define separate namespaces for classesamespaces are a use u organ z ng pr ncp e orl arge sys ems
Collections
¾List<Type> is a list of Type objects¾Map<Key,Value> is a set of <Key,Value> pairs ¾Generic types like List and Map take a type parameter <Type>
© Robert Miller 2007
7
The cases of a switch must be
terminated by break, or they
will fall through to the next
case!
default is like the else clause of a switch – it ’s good practice
to always include one. Often it just throws an exception.