Object Oriented Software Design (NTU, Class Even, Spring 2009) instructor: Hsuan-Tien Lin
Final Examination Answer Sheet
TIME: 06/16/2009, 14:20–17:20
Feel free to as the TAs for additional answer sheets when necessary. But please mark your names clearly and staple your sheets together.
Your NTU ID Number:
Your Chinese Name:
Anything You Want to Say about the Class (optional):
1 POO Zoo
(1) basic answer: assume that the first part can be compiled, just new three classes without the inheritance hierarchy, and use several lines for action.
ideal answer (with bonus 2 points): say that the first part cannot be compiled. Use whateverway you can do (say, println) to print the messages
(2)
• Let the three classes extends Unicorn
• Move do unicorn walk to class Unicorn, and merge it with the original walk.
1 of 4
Object Oriented Software Design (NTU, Class Even, Spring 2009) instructor: Hsuan-Tien Lin
2 POO Calculus
(1)
c l a s s Exp extends F u n c t i o n implements D i f f e r e n t i a b l e , I n t e g r a b l e { public double e v a l ( double x ) { return Math . exp ( x ) ; }
public F u n c t i o n d i f f ( ) { return t h i s ; } public F u n c t i o n i n t e ( ) { return t h i s ; } }
(2)
c l a s s Poly extends F u n c t i o n implements D i f f e r e n t i a b l e , I n t e g r a b l e { double deg ;
double c o e f ;
Poly ( double c o e f , double d e g r e e ) { t h i s . c o e f = c o e f ; t h i s . deg = deg ; } public double e v a l ( double x ) { return c o e f ∗ Math . pow ( x , deg ) ; } public F u n c t i o n d i f f ( ) { return new Poly ( c o e f ∗ deg , deg − 1 ) ; } public F u n c t i o n i n t e ( ) { return new Poly ( c o e f / ( deg +1) , deg + 1 ) ; } }
Bonus up to 5 points if doing some special case handling (for instance, deg = -1 in inte.)
(3)
c l a s s Sum extends F u n c t i o n implements D i f f e r e n t i a b l e , I n t e g r a b l e { F u n c t i o n f , g ;
Sum( F u n c t i o n f , F u n c t i o n g ) { t h i s . f = f ; t h i s . g = g ; }
public double e v a l ( double x ) { return f . e v a l ( x ) + g . e v a l ( x ) ; } public F u n c t i o n d i f f ( ) { return new Sum(
( ( D i f f e r e n t i a b l e ) f ) . d i f f ( ) , ( ( D i f f e r e n t i a b l e ) g ) . d i f f ( ) ) ; }
public F u n c t i o n i n t e ( ) { return new Sum(
( ( I n t e g r a b l e ) f ) . i n t e ( ) , ( ( I n t e g r a b l e ) g ) . i n t e ( ) ) ; } }
A class casting exception would be thrown when calling the corre- sponding function if f or g is not both differentiable and integrable.
(4)
System . o u t . p r i n t l n (
new Sum(
new Sum(new Poly ( 3 , 2 ) , new Poly ( 2 , 1 ) ) , new Sum(new Poly ( 5 , 0 ) ,
new Exp ( ) . i n t e ( ) ) ) . d i f f ( ) . e v a l ( 2 )
) ;
2 of 4
Object Oriented Software Design (NTU, Class Even, Spring 2009) instructor: Hsuan-Tien Lin
Feel free to ask the TAs for additional answer sheets when necessary. But please mark your names clearly and staple your sheets together.
Your NTU ID Number:
Your Chinese Name:
3 POO Search
(1)
c l a s s S e a r c h e r <E e x t e n d s S e a r c h a b l e > e x t e n d s A r r a y L i s t <E>(2)
S e a r c h e r <E> s e a r c h ( S t r i n g q u e r y )(3)
S e a r c h e r <E> a d d S e a r c h R e s u l t ( S e a r c h e r <? e x t e n d s E> o r i g l i s t , S t r i n g q u e r y )4 POO Exception
(1)
b o o l e a n a c t i o n 0 f l a g = t r u e ; t r y {
a c t i o n 1 ( ) ;
i f ( a c t i o n 2 ( ) ) { a c t i o n 3 ( ) ; a c t i o n 0 f l a g = f a l s e ; } e l s e r e t u r n ;
}
c a t c h ( N u l l P o i n t e r E x c e p t i o n e1 ) { a c t i o n 4 ( ) ; a c t i o n 5 ( ) ; a c t i o n 6 ( ) ; }
c a t c h ( N e t w o r k d i s c o n n e c t E x c e p t i o n e2 ) { a c t i o n 4 ( ) ; a c t i o n 6 ( ) ; a c t i o n 7 ( ) ; }
c a t c h ( U s e r N o t E x i s t E x c e p t i o n e3 ) { a c t i o n 4 ( ) ; a c t i o n 6 ( ) ; a c t i o n 8 ( ) ; }
c a t c h ( E x c e p t i o n e4 ) { a c t i o n 4 ( ) ; a c t i o n 6 ( ) ; }
c a t c h ( Throwable e5 ) { a c t i o n 4 ( ) ;
}
f i n a l l y {
i f ( a c t i o n 0 f l a g ) a c t i o n 0 ( ) ; }
3 of 4
Object Oriented Software Design (NTU, Class Even, Spring 2009) instructor: Hsuan-Tien Lin
5 POO Love
(1) Yes.
l o v e c o u n t = 0 ; // i n i t i a l l y
r = l o v e c o u n t + 1 ; // from J1 , r = 1 r = l o v e c o u n t + 1 ; // from J2 , r = 1 r = l o v e c o u n t + 1 ; // from J3 , r = 1
l o v e c o u n t = r ; // from J1 , now l o v e c o u n t = 1 l o v e c o u n t = r ; // from J2 , now l o v e c o u n t = 1 l o v e c o u n t = r ; // from J3 , now l o v e c o u n t = 1
(2) Yes. Because each thread holds a different lock (the thread itself), and hence still works asynchronously.
(3) No. Because each thread shares the same lock (the user M object), and hence should work synchronously.
(4)
s y n c h r o n i z e d ( r e c e i v e r ) {
i n c r e a s e l o v e c o u n t ( r e c e i v e r ) ; }
4 of 4