• Keine Ergebnisse gefunden

Exercise 2 Tiny Web Server

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise 2 Tiny Web Server"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Mathias Weber, M.Sc.

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2013) Practice Sheet 4

Date of Issue: 29.04.13

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

Exercise 1 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.

package persons;

class Person { public String name;

public Role role;

public Person(String name) {}

public void assignRole(Role r) {role = r;}

public void print() { if (role == null)

System.out.println("Not much known about " + name);

else

role.print(this);

}

Role

Professor role

Assistant Student Person

public static void main(String... argv) { Person p = new Person("Max Mustermann");

p.assignRole(new Student()); // Max starts his career

p.assignRole(new Assistant()); // Max graduates and starts working at the university p.assignRole(new Professor()); // and finally he manages to become a professor }

}

interface Role {

public void print (Person p);

}

class Professor implements Role { String room;

String institute;

public void print(Person p) {

System.out.print("Professor " + p.name + "’s office is in room " + room);

} }

class Student implements Role { int reg_num;

public void print(Person p) {

System.out.print(p.name + " has the registration number " + reg_num);

} }

class Assistant implements Role { boolean phDStudent;

public void print(Person p) {

System.out.print(p.name + " is a PhD student: " + phDStudent);

} }

(2)

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 look like in a inheritance based system?

b) Formulate a general guideline, when to favor inheritance over delegation and vice versa.

Exercise 2 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 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 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) What is the difference between aCollection<?>and aCollection<Object>? c) 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>());

} }

d) 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.

Referenzen

ÄHNLICHE DOKUMENTE

In fact, the capacity and willingness to pay of consumers may surprise project developers – likely even more so once communities are educated about the benefits of

Even data from accelerometers, that usually form a separate solution in most automated data acquisition systems, is automatically uploaded and combined with conventional

 Contributed to position papers on H2020 by Italian Ministry of Education, University and Research. and other national and

This makes it very difficult for third parties to evaluate whether MEAs achieve their objectives.’ [12] Risk: burden of uncertainty regarding the affordability, real-world efficacy

The paradigm of Technical Inheritance allows monitoring the manufacturing and usage of a component, to analyse and employ the collected data into the development process of

b) Extend the syntax as well as the semantics (static &amp; dynamic) of StooJas to support static methods and give a detailed explanation for the necessary adjustments.. c) Extend

Until now the UAS used inheritance to model the di ff erent persons at a university, look at the listing below to see a di ff erent implementation, which uses delegation to

Upon startup, the demo application establishes a WebSocket connection to a BOPlish bootstrap server and instantiates a boplish client that uses the signaling-protocol to connect to