You are on page 1of 2

Object Oriented Programming 1

Engineering track / 2nd year


2023-2024

Lab # 6: Messages and communication between objects (2)

Statement

Exercise 1

What will the following java program display when executed? Note the obligation for the static main
method to only be able to integrate into its code attributes or methods also declared static.

Exemple.java
public class Exemple {
static int i;
static public void test(int i) {
i++;
System.out.println ("i = " + i);
}
public static void main(String[] args) {
i = 5;
test(i);
System.out.println("i = " + i);
}
}

Exercise 2

Will the following Java code display at runtime ?

What would this same code look like if we replaced it in the static method void test( int j) the name
of the argument by k, as well as if the method instruction , i+=j became i+=k?

class TestI {
int i;
public TestI(int i) {
this.i = i;
}
public void getI(){
System.out.println ("i = " + i);
}
public void incrementeI(int i) {
this.i += i; // this est le référent de l'objet lui-même
}
}
public class Exemple {
static TestI unTest = new TestI(5);
static int i = 5;
static int j = 5;
static void test(int j) {
i+=j;
j+=5;
}
static void Test(TestI unTest) {
unTest.incrementeI(i);
unTest.incrementeI(j);
}
public static void main(String[] args) {
test(i);
Test(unTest);
unTest.getI();
}}

Exercise 3

What will the Java code presented below display when executed?

class TestI {
int i;
public TestI(int i) {this.i = i;}
public void getI() {System.out.println ("i = " + i);}
public void incrementeI(int i) {this.i += i; // this est le référent de l'objet lui-même}
}
public class Exemple {
static TestI unTest = new TestI(5);
static void Test(TestI unTest) {
unTest = new TestI(6);
unTest.incrementeI(5);
}
public static void main(String[] args) {
Test(unTest);
unTest.getI();
}}

You might also like