0% found this document useful (0 votes)
5 views6 pages

Java Object Programming Exam Corrections

The document contains a corrected exam on Object Programming in Java from June 2017, detailing various exercises related to Java programming concepts such as command line usage, interfaces, polymorphism, and vector operations. It includes explanations of incorrect command lines, the purpose of interfaces, and the implementation of a vector interface with methods for vector operations. Additionally, it discusses the concept of generics in Java for creating vectors of different types.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Object Programming Exam Corrections

The document contains a corrected exam on Object Programming in Java from June 2017, detailing various exercises related to Java programming concepts such as command line usage, interfaces, polymorphism, and vector operations. It includes explanations of incorrect command lines, the purpose of interfaces, and the implementation of a vector interface with methods for vector operations. Additionally, it discusses the concept of generics in Java for creating vectors of different types.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Corrected Exam on Object Programming in Java

1A June 2017

Note: Some corrections are detailed more than requested.

Exercise 1) A [Link] the following text

class Greeting {
static public void main(String args[]) {
Hello
}
};

Which of the following command lines are incorrect and why?

1. javac [Link]
[Link] [Link]
Javac [Link]
[Link] [Link]
[Link]

Line 2. Java is a command to execute, and .java is a file.


source

Line 3. Conversely, javac is a compilation command and .class


is a bytecode

line 4. No need to indicate the .class extension

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. I x = new I(); false, you do not instantiate an interface.


2. C1 y = new C1(); just a declaration followed by instantiation by
a class that implements the interface
3. I[] z = {new C1(), new C2()} ;same, even if two classes
different. They implement the same interface
4. C1 w = new C2(); false, we declare a class and instantiate.
with another (incompatible types)

What is the point of declaring an interface to then implement it with classes?


Can we declare classes directly without going through an interface?

1
The interest is twofold:

1- To be able to choose between several possible implementations for a


MEME interface.

2- The same variable of type Interface can receive instances of


different classes.

Of course, we can always create classes without using any


interfaces.

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 4x4Vehicle extends Vehicle {


void main() {
I have 4-wheel drive
}
}

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)

A vehicle starts (5)


I have all-wheel drive (6)

Comment on these results, especially why the results (lines (3) and (5)) are different, and the
results (lines 4 and 6) are the same.

[Link](new Vehicle()); This is the first method


of GM which is called (call resolved at compilation according to the profile
The vehicle is therefore instantiated, it starts (1) and it has 4
wheels (2). Normal.

[Link](new 4x4Vehicle()); This is the second


GM method that is called (resolved call at compilation according to the
the parameter profile). A 4x4 is therefore instantiated, it starts (3) and it has 4
driving wheels (4). Normal.

In the instruction [Link](x); x is a vehicle (Vehicle x). It is


So the first method of GM that is called. It’s a vehicle.
which starts (line 5). Even if the instance is a 4x4 (x=new
Vehicle4x4() This is the difference with line 3. But, like the instance
present is a 4x4, it has 4 wheel drive (line 6 like line
4). Because it's polymorphism here. [Link]() of GM.

cf. TD Java, "Surcharge and Redefinition" (in Various)

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

• Create an origin vector of dimension data


• Assign a value to the component, and the data
• Access the tenth component, data, results, this fifth component
• Fill the vector with the same given value
• Scalar product of two vectors.

1. Define an Interface for such a vector object.

interface Vector {

public void origin(int n);


Create an origin vector of dimension n
public void set(int i, double x);
put the i-th component to x (0 ≤ i)
public double get (int i);
returns the i-th component
public void setConst(int d);
puts all the components at d
public double scalarProduct (Vector r);
dot product of two vectors (this and r)
}

2. Implement this vector interface

We will neglect here cases of errors: index i out of bounds, negative n, etc.
They will have to be subject to exceptional lifting.

class Table implements Vector {


double [] t;

public void origine(int n) {


t = new double[n];
setConst(0);
}

public void set(int i, double x) {


t[i] = x;
}

public double get (int i) {


return t[i];
}

public void setConst(int d){


for(int i = 0; i < [Link]; i++)
t[i] = d;
}

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
}
};

3. Write a programmainwho creates the two three-dimensional vectors v1 (1, 3, 2)


Calculate the dot product of (4, 4, 4).

class TestVector {
public static void main(String [] args){

Vector v1 = new Array(),


v2 = new Tableau();

[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.

To have a vector of something else, intouchar, we can replace


in the source file type double by the desired type. This can
walk for known primitive types of the compiler.

Because we need to consider the operations = or == or + etc. performed on these


types, and which may not be defined for any type
(user class). Furthermore, here, the scalar product does not make sense for
the typechar.

This aspect of object programming is called generics. It will be said


generic class. It is a class parameterized by a formal type. By
example, here the type of the elements of the vector. One must then ensure that
The operations performed by the class on this formal type are all
offers in the corresponding effective type

5
6

You might also like