Anda di halaman 1dari 4

Programming Paradigms – Werner Wild

Describe the major concepts of the Object Oriented Paradigm and their benefit to Software
Development.

The object oriented paradigm is a way of programming that models the world into objects.
These objects contain inside themselves the data they need and the behaviour is defined by
the factory object, its class. Procedural languages used to separate the data from the functio-
nalities of the application, the procedures.
Object orientation allow programmers to think how human being think, that means how the
real world is built: everything we see is an object e.g. a car, a house, a window. A window is
an object and is part of the object ʻhouseʼ.
This is also reflected in OOP since an object can containseveral other object of different
types.
The main feature of the OOP are inheritance, polymorphism and encapsulation.
OOP emphases the possibility to reuse the code.
In detail:
Encapsulation: the data needed by an object is ʻencapsulatedʼ inside it, stored in instance
variables which are private to that object. This enforces also the concept of data hiding, since
this information cannot be accessed directly from the outside. Also the behavior that an object
can be applied to is defined in its factory, the class, so that all what belongs to a type of o-
bjects is together in one class.
Inheritance:can be single (Java, smalltalk) or multiple (c++). Inheritance allow to structure
classes by defining superclasses es and subclasses and so making an object 'part of' another
one. in the example we have the class Vehicle which has two subclasses: Car and Train.
When we instantiate an object Car, first an object Vehicle is instantiated, and after an object
Car. The Car is a Vehicle. Inheritance allows to subclasses to specialize adding or hiding data
and behavior of the superclass and so helps to modularize the code and to keep the classes
and the methods small.
Polymorphism:it's a very important part of the OOP, since it allows to define a function that
can take parameters of different type. there are three components: overloading, virtuality and
parametric polymorphism. Overloading means that we can define more functions with the
same name but different number and/or type of the arguments. Virtuality implements the con-
cept of late binding by, for example, overloading, where having a method 'start(){...}' in both
class and superclass, the method that is actually called is determined at the runtime.

What are instance Variables? What are Class Variables? What are they used for? How are
they declared in Java?

Instance variables are variables that are encapsulated in the object they belong to. Each o-
bject has its own copy to the instance variables whereas class variables are shared by all the
instances of the same class.
Static method cannot access directly instance variables; only through the object the instance
variables belong to ( e.g. anObject.x ).
Class variables are shared by all instances of the class. There is only one copy in memory.
This variables can be used for operation like counting the number of objects instantiated from
one class and similar operations. Instance variables contain the data that each object needs
to specify and to perform operations on it.
Instance variables in Java:
public class C {
private int x;
public String s
}
Programming Paradigms – Werner Wild

Class variables in Java:


public class C {
private static int x;
public static String s;
}

List some dis/ advantages of smalltalk vs. Java

Java and Smalltalk are both object oriented programming languages, but smalltalk is pure
OO while Java is not pure. Both run in a virtual machines. Java, compared to smalltalk has a
lot af keywords that cannot be used as names by the programmers (int, void, super...). Smal-
ltalk has only very few keywords. A great advantage of smalltalk is that is very easy to under-
stand since it's written more human-like; Java is more cryptic to read. In smalltalk everything
is an object, even for example "3", which is an instance of its class; Each class is an object.
A smalltalk application can be changed faster than a Java one. Smalltalk can also be used
with the children, for example by using the multimedia-oriented version: squeak.

What is an Object? What do object have? How do Objects communicate?

An object is an instance of a class, that means, it is a class "in action", holding its instance
variables and sharing with all objects of the same type the behavior and the Class Variables,
both defined in the factory of these objects, the class. objects have a defined behavior.
Objects communicate by sending messages each other. The object react to a message sent
executing the method the message asks for and give back a message. In Smalltalk a mes-
sage is always returned to the sender.

What is polymorphism?

(see Q1)

Give examples that shows how an iteration and a "if then else" is implemented in smalltalk.
What is the difference to languages like Java of C++?

Iteration:
1 to: 10 do: [...]
If-Then-Else:
[x<y]
ifTrue: [...] ifFalse: [...]

Iterations and the condition expressions if-then-else can be written in a very compact way in
Smalltalk. both Java and c++ needs more lines of code to define the same functionality. The
syntax of Smalltalk is even easier to understand since it is very similar to the spoken langua-
ge.

What is the result of the following Smalltalk expression?


#(1,3,5,7,9)
inject: 2
into: [:element :acc | acc + element]

sum of the array's elements + 2 -> 27


Programming Paradigms – Werner Wild

What is the difference between = and == in Smalltalk? What are the corresponding methods
in Java? Which method must always be overwritten when the equals method is overwritten in
Java?

in Smalltalk:
= boolean operator that check whether the content of two objects are equals, bychecking the
hash computed on the data of the two objects.
== check whether the two references point to the same instance.
In Java:
Smalltalk = is == in Java
Smalltalk == is the method 'equals()'
When we overwrite the equals method in Java, we have also to overwrite the hash function
applied on the object since this function has to return 'True' on equals content of two different
objects.

What is pointcut? give an example too.

A pointcut is a set of join points. A join point is a well defined position in the program flow.
The pointcut define a set of methods for example, that are associated in some way e.g. all
setter methods or all methods with 'void' return type.
public Aspect X {
pointcut setXY(): call (void *.setX(*)) ||
call(void *.setY(*)); after setXY(){ System.out.println("Setter cal-
led");
}}

What is an aspect? Why is the Aspect Oriented Paradigm useful?

The Aspect Oriented Paradigm allows to perform operation that cannot be directly encapsula-
ted into objects, like logging, session tracking or security management. If these operations
should be written directly in the code, it would be very difficult to perform changes to the code
and ensure that every changed part is really affected by e.g. a logging-method.
AOP allows to define these concepts outside the methiod, so that changes and modification
can be done very quickly and only in one part of the code.
AOP is available as Eclipse Plug-in; it does not add new functionalities to Java, but it adds a
simple way to modularize such operations explained before.
Aspect is somehow similar to a Class and is defined by pointcuts. An aspect usually provides
an operation like security issues by defining the appropriate pointcuts and advice to run at
those pointcut.
The Aspect encapsulates this way the function needed to manage crosscuting calling.

What is not in AspectJ?

yes: aspect - pointcut


no: transaction - logging – encapsulation
Programming Paradigms – Werner Wild

What does the following haskell function do? define its type. Evaluate it for Lists of at least 5
elements step by step.

This function sorts an array recursively by unifying 3 array: the first is with the elements less
or equal than the first element, the second is only with the first element and the last is an arry
with the elements grater than the first. if the array is empty it returns an empty array.

What is currying? Why is it useful? Give an example.

Currying is a term used in Haskell programming languages and defines a way how functions
take the argument. Curried functions take the parameters one at a time instead of taking
them all together.
E.g.: add x y = x+ y takes first x and return the function add x which takes y and returns the
result x + y.
Curried functions are useful since they are more flexible then normal functions. Many useful
function can be specified by applying partially a curried function.
another example is the list [1, 2, 3] built up by 1: (2: ( 3: [])), a curried function that takes one
element at a time and adds them into the empty list.

Define a function to calculate the factorial of a number in 3 different ways in Haskell!

fact1 n = product (1..n)

fact2 n
| n == 0 = 1
| otherwise = n * fact2 (n-1)

fact3 n = foldr (*) 1 [1..n]

Evaluate the following Haskell expressiones.


map(+1)[1,3,5,7]
[2,4,6,8] filter p[] = [] filter p (x:xs)
|p x = x: filter p xs
|otherwise = filter p xs
[1..10] [2,4,6,8,10] filter2 p xs = [x|x <- xs, p x] [2,4,6,8,10]