LP24: Object Oriented Programing – Final Exam 2h
Documents, Computer, Calculator unauthorized
1. Theoretical questions (6 pts)
Question 1: Is it possible to instanciate an interface? An abstract class?
Question 2: Is it possible to put an empty Constructor inside an interface? Inside an abstract class? Can this constructor have a body in both cases?
Question 3: Is the following statement allowed : A a = new B();
If A is an abstract class, derived by the class B? If A is an interface, implemented by the class B?
Question 4: Can an interface/an abstract class contain attributes? With which modifiers?
Question 5: Can an interface inherits from another interface? From an abstract class?
Question 6: Can an abstract class inherit from another abstract class? From an interface?
2. Geometric shapes and code factorization (10 pts)
In this exercise, we aim to manipulate geometric shapes according to the following interface:
interface Geometricable { double perimeter();
double surface();
}
Question 1: Write the classes Circle, Triangle, Rectangle and Square using this interface.
Question 2: A polygon is defined as a set of connected segments (made each of two points) which are not cutting each other (in this exercise it is not asked to check if the lines are cutting each other) and which is starting and finishing at the same point of the space. What is the type of Polygon (abstract class or interface)? What is its relationship with the other classes?
Question 3: modify the previous classes so as to integrate Polygon into this new scheme. You may need to introduce new “tool” classes.
Question 4: A polygon is convex if all segments that link any couple of points of the periphery are included into the shape of the polygon. If a polygon is convex, its surface is given by the following equation:
With (xi,yi) the ordered set of points of the polygon. (see next figure)
What can be the type of ConvexPolygon and what are the relationships with the other classes/interfaces? Write the corresponding code.
3. What is the output of the following program (4 pts)
Here is a program:
class Less {
public int n ;
public Less(int x) { n = x ; }
public String toString() { return "Less ! " ; } public void test1() {
System.out.println("Less.test1 : " + this.toString());
}
public void test2() {
System.out.println("Less.test2 : " + this.n);
} }
class More extends Less{
public int n ;
public More(int x, int y) { super(x) ; this.n = y ; } public String toString() { return "More ! " ; } public void test1() {
super.test1() ; System.out.println(
"More.test1 : " + this.toString() + super.toString());
}
public void test2() { super.test2() ;
System.out.println( "More.test2 : " + this.n + super.n);
}
public static void main(String argv[]) { More m = new More(1, 2) ; m.test1() ;
m.test2() ;
Less n = new Less(0) ; n.test1() ;
n.test2() ; }
}
What is the output on the screen?