Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Markus Reitz
Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik
Practice Sheet 5: Fortgeschrittene Aspekte objektorientierter Programmierung (SS 2008)
Date of Issue: 06. Mai 2008 Deadline: 19. Mai 2008 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Covariance & Contravariance
a) Discuss the concept of Java’s Checked Exceptions with respect to covariance and contravariance.
b) Which kind of guarantee is given by Checked Exceptions?
Exercise 2 Type Erasure
p u b l i c c l a s s C o m p a r a b l e T e s t<A , B> i m p l e m e n t s C o m p a r a b l e<A>, C o m p a r a b l e<B> { p u b l i c i n t compareTo (A a ) { r e t u r n −1; }
p u b l i c i n t compareTo ( B b ) { r e t u r n 1 ; } p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g s ) {
C o m p a r a b l e T e s t<S t r i n g , I n t e g e r>C=new C o m p a r a b l e T e s t<S t r i n g , I n t e g e r>( ) ; }
}
a) Transform the given source code using the type erasure algorithm utilised by Java 5.
b) Explain why proper compilation of the source code is impossible.
Exercise 3 Permutations
a) Implement a class Permutator that obeys to the following requirements
1. For a given collection of elements of an arbitrary type T, the Permutator class provides an iterator over all permutations of the elements contained in the collection.
2. The implementation has to provide an usage interface as exemplified by the code fragment
L i s t<I n t e g e r> l i s t=a s L i s t ( 1 , 2 , 3 ) ;
f o r( C o l l e c t i o n<I n t e g e r> c u r r e n t : new P e r m u t a t o r<I n t e g e r>( l i s t ) ) S y s t e m . o u t . p r i n t l n ( c u r r e n t ) ;
3. The implementation has to use class Permutations (also available online).
b) Analyse and explain the algorithm that is used by class Permutations to provide an iterator over all permutations
of length n.
i m p o r t j a v a . u t i l .∗;
i m p o r t s t a t i c j a v a . u t i l . A r r a y s .∗;
c l a s s P e r m u t a t i o n s i m p l e m e n t s I t e r a b l e<I t e r a b l e<I n t e g e r>> { p r i v a t e i n t s i z e ;
p u b l i c P e r m u t a t i o n s (i n t s i z e ) { t h i s. s i z e=s i z e ;
}
p u b l i c I t e r a t o r<I t e r a b l e<I n t e g e r>> i t e r a t o r ( ) { r e t u r n(new I t e r a t o r<I t e r a b l e<I n t e g e r> >() {
p r i v a t e i n t[ ] c u r r e n t=new i n t[ s i z e ] ; p r i v a t e b o o l e a n h a s N e x t=( s i z e>0 ) ; p r o t e c t e d b o o l e a n i n c r e m e n t ( ) {
c u r r e n t [ 0 ]++;
f o r(i n t i=0 ; i<c u r r e n t . l e n g t h ; i++) i f ( c u r r e n t [ i ]>i ) {
i f ( i==( c u r r e n t . l e n g t h−1 ) ) r e t u r n f a l s e;
c u r r e n t [ i ]=0 ; c u r r e n t [ i+1]++; }
r e t u r n t r u e; }
p r o t e c t e d I t e r a b l e<I n t e g e r> c r e a t e P e r m u t a t i o n ( ) { t r y {
I n t e g e r [ ] r e s u l t=new I n t e g e r [ s i z e ] ; b o o l e a n[ ] u s a g e =new b o o l e a n[ s i z e ] ; f o r(i n t i=c u r r e n t . l e n g t h−1 ; i>=0; i−−) {
i n t c o u n t e r=c u r r e n t [ i ]+1 ;
f o r(i n t j=0 ; j<u s a g e . l e n g t h ; j++) { i f ( ! u s a g e [ j ] )
c o u n t e r− −;
i f ( c o u n t e r==0) { u s a g e [ j ]=t r u e; r e s u l t [ j ]=i ; break; } } }
r e t u r n a s L i s t ( r e s u l t ) ; }
c a t c h( E x c e p t i o n ex ) {
throw new U n s u p p o r t e d O p e r a t i o n E x c e p t i o n ( ex ) ; }
}
p u b l i c b o o l e a n h a s N e x t ( ) { r e t u r n h a s N e x t ;
}
p u b l i c I t e r a b l e<I n t e g e r> n e x t ( ) { i f ( ! h a s N e x t ( ) )
throw new U n s u p p o r t e d O p e r a t i o n E x c e p t i o n ( ) ; t r y {
r e t u r n c r e a t e P e r m u t a t i o n ( ) ; }
f i n a l l y {
h a s N e x t=i n c r e m e n t ( ) ; }
}
p u b l i c v o i d remove ( ) {
throw new U n s u p p o r t e d O p e r a t i o n E x c e p t i o n ( ) ; }
} ) ; } }