168 lines
2.5 KiB
Markdown
168 lines
2.5 KiB
Markdown
# Exercise 1
|
|
|
|
```java
|
|
class Param1
|
|
{
|
|
public int y;
|
|
// default constructor here
|
|
public void method1(int x)
|
|
{
|
|
y = 2*x;
|
|
}
|
|
|
|
public int getY()
|
|
{
|
|
return y;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param1 q1 = new Param1();
|
|
int y = 25; //Local Variable
|
|
q1.y = 100; //Field Variable
|
|
q1.method1(y); //method1(25) -> this.y = 50
|
|
System.out.println(y);
|
|
System.out.println(q1.getY());
|
|
|
|
` y = 25 `
|
|
` this.y = 50 `
|
|
|
|
```java
|
|
class Param2
|
|
{
|
|
public int y;
|
|
// default constructor here
|
|
public void method1(int x)
|
|
{
|
|
y = 2*x;
|
|
}
|
|
|
|
public int method2(int w)
|
|
{
|
|
return 3*w;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param2 q2 = new Param2();
|
|
int y = 25; //Local Variable
|
|
q2.y = 100; //Field Variable
|
|
q2.method1(y); //method1(25) -> this.y = 50
|
|
System.out.println(y);
|
|
System.out.println(q2.y);
|
|
System.out.println(q2.method2(y));
|
|
|
|
`y = 25`
|
|
`this.y = 50`
|
|
`method2(y) = 3*y = 3*25 = 75`
|
|
|
|
```java
|
|
class Param3
|
|
{
|
|
public int y;
|
|
// default constructor here
|
|
public void method1(int y)
|
|
{
|
|
y = y*3; // Local Variable
|
|
}
|
|
public int getY()
|
|
{
|
|
return y;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param3 q3 = new Param3();
|
|
q3.y = 100; //Field Variable
|
|
q3.method1(25); //Local y = 25x3 = 75
|
|
System.out.println(q3.getY());
|
|
|
|
`y = 100`
|
|
|
|
```java
|
|
class Param4
|
|
{
|
|
private int w;
|
|
public Param4()
|
|
{
|
|
w = 5;
|
|
}
|
|
public void method1(int w)
|
|
{
|
|
this.w = w*3;
|
|
}
|
|
public int getW()
|
|
{
|
|
return w;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param4 q4 = new Param4();
|
|
System.out.println(q4.getW());
|
|
q4.method1(25);
|
|
System.out.println(q4.getW())
|
|
|
|
` w = 5 `
|
|
` this.w = 15 `
|
|
` w = 15 `
|
|
|
|
# Exercise 2
|
|
|
|
```java
|
|
class Param5
|
|
{
|
|
private int a, b;
|
|
public Param5()
|
|
{
|
|
b = 2;
|
|
}
|
|
public void method1(int b)
|
|
{
|
|
a = this.b + 5;
|
|
System.out.println(a);
|
|
a = method2(b);
|
|
System.out.println(a);
|
|
System.out.println(this.b);
|
|
}
|
|
public int method2(int a)
|
|
{
|
|
b = 2 + a;
|
|
return this.a + b;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param5 q5 = new Param5();
|
|
q5.method1(3);
|
|
|
|
Field Variable b = 2
|
|
Local Variable b = 3
|
|
Field a = 2 + 5 = 7
|
|
output 7
|
|
a = method2(3)
|
|
Field b = 2 + 3 = 5
|
|
return 7 + 5 = 12
|
|
Field a = 12
|
|
output 12
|
|
output 5
|
|
|
|
```java
|
|
public void arrayMethod(int[] stats) //B
|
|
{
|
|
for (int k = 0; k < stats.length; k+=2)
|
|
stats[k] = k; //C
|
|
}
|
|
```
|
|
|
|
int [] numbers = {5, 10, 15, 20}; //A
|
|
arrayMethod(numbers);
|
|
|
|
Draw four state-of-memory diagrams to indicate the situation immediately after A has been executed, on starting execution of the method at B, immediately before returning from the method i.e. at C and after the method has been fully executed i.e. in the order in which the statements above are executed.
|
|
|
|
| numbers | -> | 5, 10, 15, 20 |
|
|
| numbers | -> | stats |
|
|
| stats | -> | 0, 10, 2, 20 |
|
|
| numbers | -> | 0, 10, 2, 20 |
|
|
|