0% found this document useful (0 votes)
15 views2 pages

Lab 3

The document contains instructions for 6 exercises on object-oriented programming concepts in Java: 1. Create a Car class with methods to increment and decrement the speed, overloading the increment method to allow specifying the speed change amount. 2. The test method can be overloaded as public void test(), public void test(double a), and public void test(int a, int b) but not public int test(int a). 3. The A, B, and C files will not compile due to errors like trying to call a non-static method without an object and missing a constructor. 4. Create a constructor for the Car class that initializes the speed to 0, overloading it to
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)
15 views2 pages

Lab 3

The document contains instructions for 6 exercises on object-oriented programming concepts in Java: 1. Create a Car class with methods to increment and decrement the speed, overloading the increment method to allow specifying the speed change amount. 2. The test method can be overloaded as public void test(), public void test(double a), and public void test(int a, int b) but not public int test(int a). 3. The A, B, and C files will not compile due to errors like trying to call a non-static method without an object and missing a constructor. 4. Create a constructor for the Car class that initializes the speed to 0, overloading it to
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
You are on page 1/ 2

Object Oriented Programming 1

Engineering track / 2nd year


2023-2024

Lab # 3 : Classes et Objects (2)

Statement

Exercise 1

Create the car class with a speed change, by installing, first of all, two methods returning nothing, but
allowing one to increment the speed and the other to decrement it. Then overload the speed
increment method, passing it as an argument the quantity of speed to add.

Exercise 2

Consider the declaration of the following method:

public void test(int a) {}

What overloads are allowed among these different possibilities?

• public void test() {}


• public void test(double a) {}
• public void test(int a, int b) {}
• public int test(int a) {}

Exercise 3

A.java, B.java and C.java files will not compile. Explain why.

A.java
public class A {
void A(int i) {
System.out.println("Hello");
}
public static void main(String[] args) {A unA = new A(5);}
}

B.java
class Test {
int a;
Test (int b) {a = b;}
}
public class B {
public static void main(String[] args) {
Test unTest = new Test();
}
}
C.java
class Test {
int a;
int c;
Test (int b) {a = b;}
static int donneC() {
return c;
}
}
public class C {
public static void main(String[] args)
{ Test unTest = new Test(5);}
}

Exercise 4

Create the constructor of the car class, initializing the speed to 0. Overload this constructor for cases
where we know the initial speed.

Exercise 5

Among the following attributes of the Renault Kangoo class, the version with all possible options,
separate those that you would declare as static: speed, number of passengers, maximum speed,
number of gears, tank capacity, age, power, price, color, number of doors.

Exercise 6

Create, in Java, a program containing a Point class, with its three coordinates in space (x,y,z), and which
can be initialized in three different ways (we know either x, or x and y, or x and y and z). Next, include
a translate() method in the class that is overloaded three times, also depending on which of the three
translation values are known.

Create two objects of the Point class and test whether the program works correctly when you translate
these points.

You might also like