• Keine Ergebnisse gefunden

Exercise2FeatherweightJava Exercise1Generics PracticeSheet5 AdvancedAspectsofObject-OrientedProgramming(SS2014)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise2FeatherweightJava Exercise1Generics PracticeSheet5 AdvancedAspectsofObject-OrientedProgramming(SS2014)"

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 2014) Practice Sheet 5

Date of Issue: 20.05.14

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

Exercise 1 Generics

a) Implement a generic static methodflattenhaving the following signature

public static <... > List <... > flatten ( List <... > l)

The method takes a list of lists of anything and flattens it by one level. Replace each ellipsis such that your implementation provides maximum re-usability without violating type correctness.

b) (optional)Java does not support the creation of generic arrays, i.e. new List<E>[], new List<String>[], new E[], where E is a declared type variable, are illegal expressions. In order to find out the reasons for it, assume that the statementList<String>[] stringLists = new List<String>[1];is legal.

Write a code snippet that leads to aClassCastExceptionat a cast, that has been inserted by the type erasure. Your code should be legal Java except for the given statement.Hint: Take advantage of the covariance of arrays and try to assign a list of a different type tostringLists[0].

Exercise 2 Featherweight Java

a) Use the rules given in Fig. 2 in the paper "Featherweight Java: A Minimal Core Calculus for Java and GJ" to check for correct typing of the following program.

class Person extends Object { String name ;

int age ;

Person ( String name , int age ) { super();

this. name = name ; this. age = age ; }

int getAge () { return this. age ; }

}

class Student extends Person { int studentId ;

Student ( String name , int age , int studentId ) { super( name , age );

this. studentId = studentId ; }

Person me () {

return ( Person )this; }

}

class Main extends Object { Main () {super(); } Person main () {

return new Person (" Berta " , new Student (" Bob " , 22 , 12345). getAge ());

} }

(2)

Check if the program is type correct according to the type rules of Featherweight Java. Do a detailed proof of the type correctness of theMainobject using the procedure shown in the lecture.

You may assume that the typesintandStringare built in and valid types in Featherweight Java with the same meaning and syntax as the corresponding types in Java.

b) Give a step-by-step reduction of the expression

new Person (" Berta " , new Student (" Bob " , 22 , 12345). getAge ());

as shown in the lecture.

c) Extend Featherweight Java with aletconstruct. The syntax should look like in the following example:

let x = e1 in e2

wheree1ande2are expressions and the variablexcan be used ine2. Give the needed typing rule(s) as well as the needed reduction rule(s).

Exercise 3 Aliasing

a) Give two examples fordynamic aliasing, one where aliasing is desired and one where aliasing has undesired effects.

b) Describe the relationship between capturing, leaking and aliasing.

c) Give a solution to the signers issue on slide 8 of chapter 4.

Referenzen

ÄHNLICHE DOKUMENTE

An exten- sive sample of 121 early–type galaxies in vari- ous densities ranging from galaxy clusters with highest richness class, rich clusters, over poor richness class, poor Low–L

With GaCl 3 and fluorobenzene analogous tetrachlorogallates were observed, however, some by-products were recognized: para-fluoro- phenylchlorofluorophosphonium

433, the am- plitude is larger (so the larger speed) than that for the exact resonant case, and the lumps travel in a smaller angle with respect to the positive x-axis (it is about

// - add is not available , because the compiler cannot ensure , that // the dynamic type of the added object is compatible to generic // element type of the List. This would need

This code snippet would break the guarantee of the type system, that the casts that are inserted by the compiler during the type erasure will never fail. Furthermore, one could not

Long-term exposure to fine particulate matter and incidence of type 2 diabetes mellitus in a cohort study: effects of total and traffic-specific air pollution.. Rajagopalan S,

To enable dedicated modeling tools to work with metamodel snippets, the metamodel snip- pets need to be materialized. The Package operator takes the result of the Reduce operator

The research part of this dissertation focuses on the following areas: (i) studies of BPV1 and EBV segregation elements with heterologous replicons; (ii) discus- sion