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 2011) Practice Sheet 5
Date of Issue: 10.05.11Deadline: 17.05.11 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Questions
a) Add one or more questions to the document athttp://bit.ly/lGbs2v.
Exercise 2 StoJas Extension (postponed from Sheet 4)
In this exercise, we are going to build extensions to the Java subsetStoJasthat was presented in the lecture.
a) Extend the syntax as well as the semantics (static & dynamic) ofStoJasto support a "for(...) { ... }" statement and give a detailed explanation for the necessary adjustments.
b) Extend the syntax as well as the semantics (static & dynamic) of StooJasto support static methods and give a detailed explanation for the necessary adjustments.
c) Extend the syntax as well as the semantics (static & dynamic) ofStooJasto support static variables and give a detailed explanation for the necessary adjustments.
Exercise 3 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 4 Generics
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 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 theComparableinterface.
c) What is the difference between a Collection<?>and aCollection<Object>? d) Explain the output of the following program:
p u b l i c f i n a l c l a s s G e n e r i c C l a s s<T> {
p u b l i c v o i d o v e r l o a d e d M e t h o d ( C o l l e c t i o n<?> o ) {
S y s t e m . o u t . p r i n t l n ( " o v e r l o a d e d M e t h o d( C o l l e c t i o n<?>) " ) ; }
p u b l i c v o i d o v e r l o a d e d M e t h o d ( L i s t<Number> s ) {
S y s t e m . o u t . p r i n t l n ( " o v e r l o a d e d M e t h o d( L i s t<Number>) " ) ; }
p u b l i c v o i d o v e r l o a d e d M e t h o d ( A r r a y L i s t<I n t e g e r> i ) {
S y s t e m . o u t . p r i n t l n ( " o v e r l o a d e d M e t h o d( A r r a y L i s t<I n t e g e r>) " ) ; }
p r i v a t e v o i d method ( L i s t<T> t ) {
o v e r l o a d e d M e t h o d ( t ) ; / / w h i c h m e t h o d i s c a l l e d ? }
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 ) {
G e n e r i c C l a s s <I n t e g e r> t e s t = new G e n e r i c C l a s s<I n t e g e r>( ) ; t e s t . method (new A r r a y L i s t<I n t e g e r>( ) ) ;
} }
e) The interface Collection<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.