Java Object Programming Exam Corrections
Java Object Programming Exam Corrections
1A June 2017
class Greeting {
static public void main(String args[]) {
Hello
}
};
1. javac [Link]
[Link] [Link]
Javac [Link]
[Link] [Link]
[Link]
Exercise 2)
Let there be a Java interface I, and two classes C1 and C2 that implement it. Which of the...
Are the following statements true or false? Why?
1
The interest is twofold:
Exercise 3: The following Java source file declares two classes, a Vehicle class which
wear a method me() that prints a message, and a subclass 4x4Vehicle that bears the
same method. A classGMen then that contains that contains two methods
start(Vehicle) and start(Vehicle4x4).
class Vehicle {
void moi() {
I have 4 wheels
}
}
class GM {
public static void start(Vehicle v) {
A vehicle starts
[Link]();
}
public static void start(Vehicule4x4 v) {
A 4x4 starts
I see you.
}
}
class VehicleTest {
static public void main(String args[]) {
[Link](new Vehicle());
[Link](new SUV());
Vehicle x = new 4x4Vehicle();
[Link](x);
}
2
give as results:
A vehicle starts
I have 4 wheels (2)
A 4x4 starts 3
I have four-wheel drive. (4)
Comment on these results, especially why the results (lines (3) and (5)) are different, and the
results (lines 4 and 6) are the same.
3
Exercise 4)
We would like to define a vector object of given dimension. The operations that we
would like to carry out, among other things, are
interface Vector {
We will neglect here cases of errors: index i out of bounds, negative n, etc.
They will have to be subject to exceptional lifting.
4
public double scalarProduct (Vector r) {
double ps = 0;
for(int i = 0; i < [Link]; i++) {
ps += t[i]*[Link](i);
}
return ps;
Test if same dimension
}
};
class TestVector {
public static void main(String [] args){
[Link](3);
[Link](0, 1);
[Link](1, 3);
[Link](2, 2);
[Link](3);
[Link](4);
[Link]([Link](v2));
}
}
Subsidiary question:
What needs to be changed in the vector interface to have a vector of integers? of characters?
Propose an idea to generalize the definition of a vector, to accept elements of
any type.
5
6