73 lines
1.3 KiB
Markdown
73 lines
1.3 KiB
Markdown
# Exercise 1
|
|
|
|
```java
|
|
class Param6
|
|
{
|
|
private int x, y;
|
|
public Param6(int a, int b)
|
|
{
|
|
x = b;
|
|
y = a;
|
|
}
|
|
|
|
public Param6(int a)
|
|
{
|
|
this(a, 5);
|
|
}
|
|
|
|
public void method1(int x)
|
|
{
|
|
y = y + x;
|
|
System.out.println(y);
|
|
this.x = this.x + x;
|
|
x = method2(y);
|
|
System.out.println(x);
|
|
System.out.println(this.x);
|
|
}
|
|
|
|
public int method2(int x)
|
|
{
|
|
return x + y;
|
|
}
|
|
}
|
|
```
|
|
|
|
Param6 q6 = new Param6(3);
|
|
q6.method1(4);
|
|
|
|
Param6(3) = Param6(3, 5)
|
|
x = 5, y = 3 //Field Variables
|
|
|
|
method1(4), x = 4 //Local
|
|
y = 3 + 4 = 7
|
|
output 7
|
|
this.x = 5 + 4 = 9
|
|
local x = method2(7) = 7 + 7 = 14
|
|
output 14
|
|
output 9
|
|
|
|
# Exercise 2
|
|
|
|
```java
|
|
public int[] arrayMethod1(int size)
|
|
{
|
|
int[] numbers = new int[size]; //B
|
|
for (int k = 0; k < size; k++)
|
|
numbers[k] = k*k;
|
|
//C
|
|
return numbers;
|
|
}
|
|
```
|
|
int[] arrayOfNumbers = arrayMethod1(5); //A
|
|
|
|
Draw three state-of-memory diagrams to indicate the complete situation immediately after B has been executed, at C i.e. immediately before returning from the method,and finally after statement A has been fully executed. This is the order in which the statements above are executed.
|
|
|
|
B) | numbers | -> int[]
|
|
k=0, while k < 5, k increment
|
|
numbers[0] = 0
|
|
numbers[1] = 1
|
|
numbers[2] = 4
|
|
numbers[3] = 9
|
|
numbers[4] = 16
|
|
C) | numbers | -> 0,1,4,9,16
|
|
A) | arrayOfNumbers | -> 0,1,4,9,16 |