vault backup: 2024-02-27 16:57:07
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
# Exercise 1
|
||||
|
||||
1. What is the relationship between the two local variables called temp?
|
||||
2. What is the scope of each of these two variables?
|
||||
3. Suppose now that we introduce a field called temp.
|
||||
1. What is the scope of each of the local variables now ?
|
||||
2. What is the scope of the field called temp ?
|
||||
3. How would we access this field's value from within report()?
|
||||
|
||||
```java
|
||||
public class ArrayHwk1
|
||||
{
|
||||
// constructor here
|
||||
|
||||
public void report()
|
||||
{
|
||||
double temp = rainiest();
|
||||
System.out.println(′′Most rain: ′′ + temp + ′′mm′′);
|
||||
// other code here
|
||||
}
|
||||
|
||||
private double rainiest()
|
||||
{
|
||||
double temp;
|
||||
// code to find most rainfall here and to store
|
||||
// it in the local variable temp
|
||||
return temp;
|
||||
}
|
||||
|
||||
// other code here
|
||||
}
|
||||
```
|
||||
|
||||
1. There is no relationship between 2 local variables.
|
||||
2. The lifetime of both local variables, temp, is the method in which they are created.
|
||||
3. .
|
||||
1. The scope is unchanged, since the local variables are still created in the method.
|
||||
2. The field variable's scope is the entire class, aside from where the local variables are created.
|
||||
3. By using this.temp, we can access the class's field variable
|
||||
|
||||
# Exercise 2
|
||||
|
||||
```java
|
||||
class Param1
|
||||
{
|
||||
public final float fixed = 50;
|
||||
public double a;
|
||||
private int b;
|
||||
public float c;
|
||||
|
||||
// constructor here
|
||||
|
||||
public float method1(int z)
|
||||
{
|
||||
return (float) z; //H
|
||||
}
|
||||
|
||||
private void method2(double y)
|
||||
{
|
||||
c = y; //I double to float error
|
||||
}
|
||||
|
||||
public int method3()
|
||||
{
|
||||
return b; //J
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
tester.fixed = 25; // Statement A fixes is final, immutable.
|
||||
tester.b = 50; // Statement B b is private, cannot directly modify.
|
||||
tester.c = 5.0; // Statement C c is a float, cannot assign double.
|
||||
int y = tester.method1(); // Statement D logic error: not given parameter, cannot assign float to int.
|
||||
tester.method2(10.0f); // Statement E method2 is private.
|
||||
float y = tester.method3(); // Statement F no error, implicit typecast int to float.
|
||||
double y = tester.method1(30); // Statement G no error, implicit typecast int to float to double.
|
||||
|
||||
# Exercise 3
|
||||
|
||||
```java
|
||||
class Param1
|
||||
{
|
||||
private int score;
|
||||
|
||||
// default constructor here
|
||||
|
||||
public void method1(int y)
|
||||
{
|
||||
score = y;
|
||||
}
|
||||
|
||||
public int getScore()
|
||||
{
|
||||
return score;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Param1 q1 = new Param1();
|
||||
q1.method1(100);
|
||||
System.out.println(q1.getScore());```
|
||||
|
||||
score = 100
|
||||
|
||||
```java
|
||||
class Param2
|
||||
{
|
||||
public int x;
|
||||
// default constructor here
|
||||
public void method1(int y)
|
||||
{
|
||||
x = 4*y;
|
||||
}
|
||||
|
||||
public int getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Param2 q2 = new Param2();
|
||||
int y = 25; // Local Variable.
|
||||
q2.method1(y);
|
||||
System.out.println(y);
|
||||
System.out.println(q2.getX());
|
||||
|
||||
```
|
||||
y = 25
|
||||
x = 100
|
||||
```
|
||||
|
||||
```java
|
||||
class Param3
|
||||
{
|
||||
public int x;
|
||||
// default constructor here
|
||||
private void increase(int p)
|
||||
{
|
||||
x = x*p;
|
||||
}
|
||||
|
||||
public void calculateX(int y)
|
||||
{
|
||||
increase(y);
|
||||
}
|
||||
|
||||
public int getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Param3 q3 = new Param3();
|
||||
q3.x = 5;
|
||||
q3.calculateX(7); increase(7) -> x(5) = x(5) * 7 -> 5 * 7 = 35
|
||||
System.out.println(q3.getX());
|
||||
|
||||
`x = 35`
|
@@ -0,0 +1,167 @@
|
||||
# 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 |
|
||||
|
@@ -0,0 +1,73 @@
|
||||
# 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
|
Reference in New Issue
Block a user