• Keine Ergebnisse gefunden

Exercise2RequiredandProvidedInterfaces Exercise1JavaScript–DynamicTyping PracticeSheet2(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2015)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise2RequiredandProvidedInterfaces Exercise1JavaScript–DynamicTyping PracticeSheet2(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2015)"

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 2015) Practice Sheet 2 (Hints and Comments)

Exercise 1 JavaScript – Dynamic Typing

a) The problem lies in the expressions of the forma > b > c. With parenthesis the expression has the form(a > b) > c.

a > breturns a value of type boolean. JavaScript than converts this value to a number, in this case0for false and1

for true.

A static type system could have helped detecting the problem by pointing out the different types of value on the left and on the right of the>operator.

b) The method Array.map takes a function and applies this function to all elements of the array. The results are collected in a new array. The problem in this case is, that the function taken bymaphas to take three parameters:

The current value, the index and the array itself. TheparseIntfunction takes a string value and parses it as an integer. The function can as well take two parameters: The string and the radix of the number (e.g. 10 for decimal numbers).

JavaScript does not check the arity of the function given tomap. Instead of complaining, the third parameter to

parseInt is simply ignored. The result of parsing the string-array is[1, NaN, NaN], sinceparseInt("2", 1) (the string "2" in the system with radix 1) it not a number. A static type system could have checked the arity of the functionparseInt.

c) The expression[1, 2, 3] + [4, 5, 6]should mean we concatenate the two arrays (read lists). The+operator in JavaScript only operates on numbers and strings. If not both arguments of this operator are numbers, the values are converted to strings.

Strings in JavaScript can be addresses as if they where arrays of characters. So the code to sum the values in the array does also work for strings. As explained before, the arguments to+(or in this case+=) are than converted to strings.

The-operation only works on numbers. If one of the arguments is not of type number, it is converted to type number. The fallback is the special constantNaN.

The result of computing[1, 2, 3] + [4, 5, 6]is the string ’1, 2, 34, 5, 6’. The functionsumconcatenates0to the front of this string, resulting in ’01, 2, 34, 5, 6’. This string is converted to a number and since it does not represent a valid number, the result isNaN. Subtracting 1 fromNaNyieldsNaNagain.

A static type system could have found out multiple problems: The types of the arguments to the+operator are not correct. Even if the conversion to string would have been made, the arguments to the-operator are not correct and should have resulted in feedback for the programmer.

Exercise 2 Required and Provided Interfaces

a) Provided interface for classes

• outside java.io: all public methods, constructors, fields, inner classes, etc ofObjectOutputStreamand its superclasses.

• inside java.io: like for classes outside java.io+protected fields, methods and constructors+fields, methods, constructors with the default modifier.

• subclasses of ObjectOutputStream inside java.iolike all other classes inside java.io

• subclasses of ObjectOutputStream outside java.io like all other classes outside java.io+protected field, methods, constructors, etc.

b) To find the required interface of a class, you have to look at the code (documentation, specification) to find out, which objects you can/ have to pass to the class in order to work with it. In this case, the OOS requires an OutputStream (parameter to the constructor).

(2)

Exercise 3 Prototype-based Inheritence

a) See enclosed source.

b) The prototypeBis changed after instantiating objectv2. Since all instances ofBhave this method afterwards, it can be called on instancev2. In Java this would mean to change the class of an already existing object, which is not possible.

Exercise 4 Introspection and Reflection

a) See enclosed source.

The class can be loaded usingClass.forName(. . .)since the plugin jar is in the classpath. The implemented interfaces are returned by the method Class.getInterfaces(). The methodClass.newInstance() can be used to create an new instance using the default constructor.

b) See enclosed source.

The method of a given name is given byClass.getMethod(String, Class<?>. . .). This method can be invoked usingMethod.invoke(Object, Object. . .).

c) No comments.

2

Referenzen

ÄHNLICHE DOKUMENTE

The local list of D may be accessed by more than one thread, the synchronization in run is needed. The generateObject method has to be synchronized, otherwise the field m may

An RMI server exports a remote object to a stub. This stub is than registered at the RMI registry, such that the client can retrieve a stub that is connected to the corresponding

Typically, the interface between the worker and the GUI is very broad: The process() method, which is part of the worker and updates the state of the GUI, will access most of

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

// @ public model instance JMLObjectSequence elements ; // @ initially elements != null &amp;&amp; elements.. c) The method implementation are straightforward, mainly delegate the

a) Implement a method IPlugin load(String clazz) that loads the plugin with the given name. Please also check that the loaded class really implements the needed interface.. b)

C was declared final, so there cannot be any subclasses of C and m was declared protected, therefore the only code that can call m is inside p, the change of the parameter type is

Since the example program neither uses synchronized blocks nor volatile fields, the value the main threads sets is never synchronized with the background thread. The example can