• Keine Ergebnisse gefunden

Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 6

N/A
N/A
Protected

Academic year: 2022

Aktie "Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 6"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Kathrin Geilmann

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 6

Date of Issue: 15.05.12

Deadline: 22.05.12 (before the lecture as PDF via E-Mail)

Exercise 1 Covariance & Contravariance

a) Discuss the concept of Java’sChecked Exceptionswith respect to covariance and contravariance.

b) Which kind of guarantee is given byChecked Exceptions?

Exercise 2 Generics

a) Write a generic static methodflipwhich takes an object of classPair(see the slides of the lecture) and flips the elements of thegivenPairobject.Hint: In order to flip the elements, both need to be of the same type.

b) Write a methodequalswhich only allows to compare two objects using thecompareToMethod of theComparable

interface.

c) What is the difference between aCollection<?>and aCollection<Object>? d) Explain the output of the following program:

public final class GenericClass<T> {

public void overloadedMethod(Collection<?> o) {

System.out.println("overloadedMethod (Collection<?>)");

}

public void overloadedMethod(List<Number> s) {

System.out.println("overloadedMethod (List<Number>)");

}

public void overloadedMethod(ArrayList<Integer> i) {

System.out.println("overloadedMethod (ArrayList<Integer>)");

}

private void method(List<T> t) {

overloadedMethod(t); // which method is called?

}

public static void main(String[] args) {

GenericClass <Integer> test = new GenericClass<Integer>();

test.method(new ArrayList<Integer>());

} }

e) The interfaceCollection<T>contains a generic method to convert a collection into an array. The method has the signature<T> T[] toArray(T[] a).

What is the purpose of the parametera? Is it possible to write a method with the same behavior with the signature

<T> T[] toArray()? Justify your answer.

(2)

Exercise 3 Extended Iterators

A lot of applications display lists of data to the user, and usually the internal data representation has to be transformed into a human readable format. In such an application you may find code like this:

Collection<E> c = ... // the data managed by the application Iterator<E> iter.iterator();

while (iter.hasNext()) { E entry = iter.next();

F transformed = transform(entry); // e.g. convert into human readable format display (transformed);

}

It would be nice have a more intelligent iterator that can be parameterized with a transform function and automatically already transformed objects.

The resulting code could the look like:

Collection<E> c = ... // the data managed by the application // a more intelligent iterator, generics are intentionally removed Transformer t = new FancyTransformer(); // the transform function Iterator i = new TransformingIterator(c.iterator(), t);

while (i.hasNext()) { F entry = i.next();

display (transformed);

}

a) A TransformingIterator transforms the values of the original iterator by a transformation function before returning them. Write a generic classTransformingIteratorand the accompanying interfaceTransformer, that decorates an existing Iterator with a transformation function. Because Java does not know higher order functions, we have to represent the transformer functions as objects of typeTransformer.

Use the following code skeleton and replace the ellipsis, such that your solution supports as much reasonable scenarios of reuse as possible.

public interface Transformer<...> { public ... transform(... o);

}

public class TransformingIterator <...> implements Iterator <...> {

public TransformingIterator(Iterator<...> inputIterator, Transformer<...> t) { ...

}

// see the documentation of Iterator for the specification of these methods public ... hasNext() ...

public ... next() ...

public ... remove() ...

}

b) Use your iterators to write a program that manages a list of Doubles and prints all numbers truncated/extended to two positions and prefixedEUR. For example the list 19.248, 7.0, 1.8882, -0.1992 will be presented asEUR 19.24, EUR 7.00,EUR 1.88,EUR -0.19.

Referenzen

ÄHNLICHE DOKUMENTE

Advanced Aspects of Object-Oriented Programming. Summer 2015

c) The output is overloadedMethod (Collection &lt;?&gt;), because overloading is resolved at compile time and at compile time the parameter t has type List &lt; T &gt; , such that

Refine the behavior of the getChar() method such that it returns the next character of the string if the position is not at the end and that it returns -1 if the reader is at the end

b) If the set of types / roles can be given at compile time and it will not change for an object during its lifecycle, it is usually a good idea to use subtyping and, if

b) Implement a StringReader. First add an interface equivalent to the BlankReader including the specification of the behavior. Second add an implementation of StringReader that

b) If the set of types / roles can be given at compile time and it will not change for an object during its lifecycle, it is usually a good idea to use subtyping and, if

a) Write a generic static method flip which takes an object of class Pair (see the slides of the lecture) and flips the elements of the given Pair object.. The method has the

• Guarantees of the type system cannot be used anymore.. b) If the set of types/roles can be given at compile time and it will not change for an object during its lifecycle, it