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 4
Date of Issue: 03.05.11Deadline: 10.05.11 (until 10 a.m. as PDF via E-Mail)
Remark
We set up an online documenthttp://bit.ly/lGbs2v, in which you can enter questions, which you think could appear in the exam. You can formulate the questions in German or English as you like. You could also try to give an answer to the question, but please do not start large discussions in this document. If there are open questions, please discuss them in the exercise courses. It is OK to reformulate questions, but try to ensure that the meaning of the question is not changed. If in doubt add a new question.
Exercise 1 Questions
a) Add one or more questions to the document athttp://bit.ly/lGbs2v.
Exercise 2 Super-Calls
Write a program, that would behave differently under the assumption of dynamically boundsuper-calls than it does with statically boundsuper-calls.
Exercise 3 Tiny Web Server
You can find the sources of a simple Java-based web server on the lecture’s web page.Important notice: The sources are meant to illustrate/practice concepts of the lecture. Students will refactor/redesign/extend parts of the software system, so currently existing deficiencies are intentional.
a) Download the ZIP file, unzip it, and start the server viaant clean compile run. Check if everything works by requesting the URLhttp://localhost:8080/public_html/HelloWorld.htmlusing a web browser.
b) Analyse the classesSimpleWebServerandLoggableClass. Introduce an appropriate interface and refactor the existing code of these two classes so forwarding/delegation is used instead of inheritance.
Exercise 4 University Administration System - Reloaded
The delegation pattern can be used to simulate inheritance, but it is a more general design pattern (seehttp://en.
wikipedia.org/wiki/Delegation_pattern). Note, the meaning of the terms delegation and forwarding varies in the literature, each author has a slightly different notion of them.
Until now the UAS used inheritance to model the different persons at a university, look at the listing below to see a different implementation, which uses delegation to establish the link between a person and the role, it currently has at the university. In this implementationPerson-objects delegate some calls to theirRole-object. The figure shows the architecture of the implementation.
Role
Professor role
Assistant Student Person
package p e r s o n s ; c l a s s P e r s o n {
p u b l i c S t r i n g name ; p u b l i c R o l e r o l e ;
p u b l i c P e r s o n ( S t r i n g name ) {}
p u b l i c v o i d a s s i g n R o l e ( R o l e r ) { r o l e = r ; } p u b l i c v o i d p r i n t ( ) {
i f ( r o l e == n u l l)
S y s t e m . o u t . p r i n t l n ( " Notmuchknowna b o u t" + name ) ; e l s e
r o l e . p r i n t (t h i s) ; }
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 v ) { P e r s o n p = new P e r s o n ( "MaxMustermann " ) ;
p . a s s i g n R o l e (new S t u d e n t ( ) ) ; / / Max s t a r t s h i s c a r e e r
p . a s s i g n R o l e (new A s s i s t a n t ( ) ) ; / / Max g r a d u a t e s and s t a r t s w o r k i n g a t t h e u n i v e r s i t y p . a s s i g n R o l e (new P r o f e s s o r ( ) ) ; / / and f i n a l l y he manages t o become a p r o f e s s o r }
}
i n t e r f a c e R o l e {
p u b l i c v o i d p r i n t ( P e r s o n p ) ; }
c l a s s P r o f e s s o r i m p l e m e n t s R o l e { S t r i n g room ;
S t r i n g i n s t i t u t e ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( " P r o f e s s o r" + p . name + " ’ so f f i c ei si nroom" + room ) ; }
}
c l a s s S t u d e n t i m p l e m e n t s R o l e { i n t reg_num ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( p . name + "h a st h er e g i s t r a t i o nnumber" + reg_num ) ; }
}
c l a s s A s s i s t a n t i m p l e m e n t s R o l e { b o o l e a n p h D S t u d e n t ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( p . name + "i saPhDs t u d e n t :" + p h D S t u d e n t ) ; }
}
a) Can you think of advantages and disadvantages of the delegation based implementation compared to an inheritance based implementation? How does the scenario of the main method looks like in a inheritance based system?
b) Formulate a general guideline, when to favor inheritance over delegation and vice versa.
Exercise 5 StoJas Extension
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.