05-07feb06static - C S100J 6 F eb ru ary 2 007 /* * E a c h...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C S100J 6 F eb ru ary 2 007 /* * E a c h in s ta n c e d e s c rib e s a c h a p te r in a b o o k * * / Download class p u b lic c la s s C h a p te r { from course web p r iv a te S trin g title ; // T h e title o f th e c h a p te r page. p r iv a te i n t n u m b e r; // T h e n u m b e r o f c h a p te r p r iv a te C h a p te r p re v io u s ; // p re v io u s c h a p te r (n u ll if n o n e ) /* * C o n s tru c to r: a n in s ta n c e w ith title t, c h a p n , p re v io u s c h a p c * / p u b lic C h a p te r(S trin g t, i n t n , C h a p te r c ) Today, we use a { title = t; n u m b e r= n ; p re v io u s = c ; } /* * = title o f th is c h a p te r * / p u b lic S trin g g e tT itle ( ) { r e tu r n title ; } /* * = n u m b e r o f th is c h a p te r * / public i nt getNumber() { r eturn number; } /* * = (n a m e o f) th e p re v io u s c h a p te r (n u ll if n o n e ) * / p u b lic C h a p te r g e tP re v io u s ( ) { r e tu r n p re v io u s ; } } 2 In 1 9 6 8 , th e D e fe n s e D e p a rtm e n t h ire d B o lt B e ra n e k a n d N e w m a n (B B N ) o f B o s to n to h e lp d e v e lo p th e A R P A N E T , w h ic h la te r tu rn e d in to th e in te rn e t. In 1 9 7 1 , R a y T o m lin s o n o f B B N w a s g iv e n th e ta s k o f fi g u rin g o u t h o w to s e n d fi le s fro m o n e p e rs o n to a n o th e r. H e c re a te d e m a il w ith fi le a tta c h m e n ts . H e s e le c te d @ a s th e s e p a ra to r b e tw e e n a n e m a il n a m e a n d lo c a tio n . N a m e s fo r @ in o th e r la n g u a g e s : TODAY: Ita lia n : c h io c c io lin a = little s n a il • O b je c t: th e s u p e re s t c la s s F re n c h : p e tit e s c a rg o t = little s n a il o f th e m a ll. p p 1 5 3 -1 5 4 . G e rm a n : k la m m e ra ffe = s p id e r m o n k e y D u tc h : a p i = s h o rt fo r a p e s ta a rt • F u n c tio n to S trin g . ( m o n k e y 's t a i l ) • S ta tic v a ria b le s a n d F in n is h : m ia u = c a t ta il methods. Sec. 1.5 (p. 47). Is ra e li: s tru d e l = a p a s try D a n is h : s n a b e l = a n " A " w ith a tru n k • T e s tin g u s in g J U n it. S p a n is h : u n a rro b a = a u n it o f a b o u t 2 5 p o u n d s N o rw e g ia n : k a n e l-b o lle = s p ira l-s h a p e d c in n a m o n c a k e F o r m o r e i n f o : h t t p : / / w w w . m a i l m s g. c o m / h i s t o r y . h t m 1 class C hapter: an instance of which describes a book. Here, we have a constructor and three getter methods C la s s O b je c t: T h e s u p e r e s t c la s s o f th e m a ll B ill E v e ry c la s s th a t d o e s n o t e x te n d a n o th e r o n e a u to m a tic a lly e x te n d s c la s s O b je c t. p u b lic c la s s C { … } is e q u iv a le n t to p u b lic c la s s C e x te n d s O b je c t { … } S e e 1 /2 - p a g e s e c t io n 4 .3 .1 o n p a g e 1 5 4 . T h e r e a s o n fo r th is w ill b e c o m e c le a r la te r . Y o u n e e d th is in fo r m a tio n to d o a s s ig n m e n t A 2 . 3 C la s s O b je c t: T h e s u p e r e s t c la s s o f th e m a ll B ill P a tie n t e q u a ls (O b je c t) to S trin g ( ) n a m e B . C lin to n ad d ress N ew Y o rk ow es $ 2 5 0 .0 0 P a tie n t O b je c t n a m e B . C lin to n ad d ress N ew Y o rk ow es $ 2 5 0 .0 0 th is is re a lly th is B e c a u s e it is a lw a y s th e re , to a v o id c lu tte r, w e d o n ’ t g e n e ra lly d ra w th e p a rtitio n fo r s u p e rc la s s O b je c t S e e 1 /2 - p a g e s e c t io n 4 .3 .1 o n page 154. 4 M e th o d to S tr in g ( ) Convention: c.toString() returns a re p re s e n ta tio n o f fo ld e r c , g iv in g in fo rm a tio n o n th e v a lu e s in its fi e ld s . P u t fo llo w in g m e th o d in P a tie n t. B ill O b je c t e q u a ls (O b je c t) to S trin g ( ) E x a m p le o f to S tr in g in a n o th e r c la s s /* * A n in s ta n c e re p re s e n ts a p o in t (x , y ) in th e p la n e * / p u b lic c la s s P o in t { p r iv a te i n t x ; // th e x -c o o rd in a te p r iv a te i n t y ; // th e y -c o o rd in a te / * * C o n s tru c to r: A n in s ta n c e fo r p o in t(x x , y y ) * / public Point(int xx, int yy) { x = xx; y= yy; Getter and setter } methods are not / * * = a r e p r e s e n ta tio n o f th is p o in t * / public S tring t oString() { r etu rn “( ” + x + “, ” + y + “ ) ”; } } 5 p u b l i c S t r i n g t o S t r i n g( ) { r etu rn n am e + “ ” + ad d ress + n a m e B . C lin to n “ ” + ow es; } ad d ress N ew Y o rk In a p p ro p ria te p la c e s , th e e x p re s s io n c a u to m a tic a lly does c.toString( ) o w e s $ 2 5 0 .0 0 to S trin g ( ) P a tie n t given on this slide F u n c tio n to S trin g s h o u ld g iv e th e v a lu e s in th e fi e ld s in a fo rm a t th a t m a k e s s e n s e fo r th e c la s s . 6 1 A s ta tic fi e ld d o e s n o t a p p e a r in e a c h fo ld e r . I t a p p e a r s in th e fi le d r a w e r , b y its e lf, o n a p ie c e o f p a p e r . T h e r e is o n ly O N E c o p y o f it. Reference static variable using p u b lic c la s s C h a p te r { Chapter.numberChaps p r iv a te S trin g title ; // title o f c h a p te r p r iv a te s ta tic i n t n u m b e rC h a p s = 0 ; // n o . o f fo ld e rs c re a te d } M a k e a m e th o d s ta tic w h e n it d o e s n o t re fe r to a n y o f th e fi e ld s o r m e th o d s o f th e fo ld e r. p u b lic c la s s C h a p te r { p r iv a te i n t n u m b e r; // N u m b e r o f c h a p te r p r iv a te s ta tic i n t n u m b e rO fC h a p te rs = 0 ; / ** = “T his chapter has a lower chapter number than Chapter c”. P re c o n d itio n : c is n o t n u ll. * / p u b lic b o o le a n i s L o w e rT h a n ( C h a p te r c ) { r e t u r n n u m b e r < c .n u m b e r ; } /** = “b’s chapter number is lower than c’s chapter number”. P re c o n d itio n : b a n d c a re n o t n u ll. * / p u b lic s ta tic b o o le a n is L o w e r( C h a p te r b , C h a p te r c ) { r e t u r n b .n u m b e r < c .n u m b e r ; } } 8 U s e a s ta tic v a ria b le w h e n y o u w a n t to m a in ta in in fo rm a tio n a b o u t a ll (o r s o m e ) f o ld e rs . a1 title “truth” C h a p te r a0 title “p eace ” C h a p te r n u m b erC h ap s 2 F ile d ra w e r fo r c la s s C h a p te r 7 T e s t i n g - - u s i n g J u n i t. P a g e s 3 8 5 - 3 8 8 ( t h r o u g h S e c . 1 4 . 1 . 1 ) . B u g : E rro r in a p ro g ra m . T e s tin g : P ro c e s s o f a n a ly z in g , ru n n in g p ro g ra m , lo o k in g fo r b u g s . Test case: A set of input values, together with the expected output. Debugging: Process of finding a bug and removing it. G e t in th e h a b it o f w ritin g te s t c a s e s fo r a m e th o d fro m th e s p e c ifi c a tio n o f th e m e th o d e v e n b e fo re y o u w rite th e m e th o d . T o c re a te a fra m e w o rk fo r te s tin g in D rJ a v a , s e le c t m e n u F ile item new Junit t est case… . At the prompt, put in the class n a m e C h a p te r T e s te r . T h is c re a te s a n e w c la s s w ith th a t n a m e . Im m e d ia te ly s a v e it — i n th e s a m e d ire c to ry a s c la s s C h a p te r. The class imports junit.framework.TestCase, which provides s o m e m e th o d s fo r te s tin g . 9 1. c1= new Chapter(“one”, 1, null); T i t l e s h o u l d b e : “ o n e ” ; c h a p . n o . : 1 ; p r e v i o u s : n u l l. H ere a re tw o test ca ses 2 . c 2 = n e w C h a p te r(“ t w o ” , 2 , c 1 ); T itle should be: “two”; chap. no.: 2; previous: c1. / * * = a S t r i n g t h a t c o n s i s t s o f t h e fi r s t l e t t e r o f e a c h w o r d i n s. E .g . f o r s = “ J u r is H a r tm a n is ” , t h e a n s w e r is “ J H ” . P re c o n d itio n : s c o n s is ts o f a n a m e in th e fo rm “ fi rs t la s t ” o r “ fi rs t m id d le la s t ” , w ith o n e o r m o re b la n k s b e tw e e n e a c h p a ir o f n a m e s . T h e re m a y b e b la n k s a t th e b e g in n in g a n d e n d . public String initialsOf(String s) { … } 10 /* * A J U n it t e s t c a s e c la s s . * E v e ry m e th o d s ta rtin g w ith th e w o rd " te s t" w ill b e c a lle d w h e n ru n n in g * th e te s t w ith J U n it. * / p u b lic c la s s C h a p te rT e s te r e x te n d s T e s tC a s e { /* * A te s t m e th o d . * (R e p la c e " X " w ith a n a m e d e s c rib in g th e te s t. Y o u m a y w rite a s * m a n y " te s tS o m e th in g " m e th o d s in th is c la s s a s y o u w is h , a n d e a c h * o n e w ill b e c a lle d w h e n te s tin g .) * / p u b lic v o id t e s tX ( ) { } } / * * T e s t fi rs t c o n s tru c to r a n d g e tte r m e th o d s g e tT itle , g e tN u m b e r , a n d g e tP re v io u s * / public void testFirstConstructor() { o n e C h a p te r c 1 = n e w C h a p te r(" o n e " , 1 , n u ll); assertEquals("one” , c1.g etTitle( ), ); test assertEquals(1, c1.getNumber()); case assertEquals(null, c1.g etPrevious()); } testM eth o d s to test g etters a n d setters a s s e r t E q u a ls ( x ,y ) : te s t w h e th e r x e q u a ls y ; p rin t a n e rro r m e s s a g e a n d s to p th e m e th o d if th e y a re n o t e q u a l. x : e x p e c te d v a lu e , y : a c tu a l v a lu e . O th e r m e th o d s l is te d o n p a g e 4 8 8 . 11 /* * Test Setter methods s etTitle, setNumber, and setPrevious * / p u b lic v o id t e s tS e tte rs ( ) { C h a p te r c 1 = n e w C h a p te r(" o n e " , 1 , n u ll ) ; c 1.setTitle("new title"); Every time you c1.setNumber(18); click button Test Chapter c2= new Chapter("two", 2, null); in DrJava, all c 1 .s e t P r e v i o u s ( c 2 ) ; methods with a assertEquals("new title", c1.getTitle()); name testXXX a s s e r t E q u a l s ( 1 8 , c 1 .g e t N u m b e r ( ) ) ; will be called. a s s e r t E q u a l s ( c 2 , c 1 . g e t P r e v i o u s( ) ) ; } 12 2 ...
View Full Document

This note was uploaded on 03/03/2012 for the course CS 100J taught by Professor Davidgries during the Spring '07 term at Cornell University (Engineering School).

Ask a homework question - tutors are online