• Keine Ergebnisse gefunden

Exercise3Permutations Exercise2TypeErasure Exercise1Covariance & Contravariance PracticeSheet5:FortgeschritteneAspekteobjektorientierterProgrammierung(SS2008)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise3Permutations Exercise2TypeErasure Exercise1Covariance & Contravariance PracticeSheet5:FortgeschritteneAspekteobjektorientierterProgrammierung(SS2008)"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

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.

(2)

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 ( ) ; }

} ) ; } }

Listing 1: Source code of class Permutations.

Referenzen

ÄHNLICHE DOKUMENTE

As pumpkin seed oils from different geographic origin show variable trace element and rare earth distribution patterns, is should possible to trace the origin of these oils.. In

The results we will prove in Section 2 are as follows: Let S&#34; denote the Stirling numbers of the second kind, i.e., the number of ways to partition an w-set into r

The number, which groups the given headword to a specific grammatical class and references the user to the appropriate inflection table, is symbolized with Roman numerals

( 1993) concluded that the recov- ery of core from Site 806 was incomplete and that there were missing segments. In particular, by comparing the data with data from other sites,

instance of Dictionary3. As usual Extend requires special treatment. We must declare the Extend in IntegerSheli to take only frames instantiated from

On 24 November 1938, the art and objets d’art left behind at 64 Tiergartenstrasse in Dresden – includ- ing not only Gustav’s porcelain collection but also Victor von

In contrast, genome ‐ wide analysis of element diversity showed that the coelacanth contains more different families of transposable elements than the genome of chicken and human..

Two other research projects supported by the SFOE are also exploring wide-bandgap technologies: Scientists from the University of Applied Scien- ces Northwestern Switzerland