vault backup: 2024-02-27 16:57:07
95
.obsidian/workspace.json
vendored
@@ -4,20 +4,55 @@
|
||||
"type": "split",
|
||||
"children": [
|
||||
{
|
||||
"id": "7b5e68cfd661dd0d",
|
||||
"id": "74175e978d06be55",
|
||||
"type": "tabs",
|
||||
"children": [
|
||||
{
|
||||
"id": "505b4bbc8a7e15f6",
|
||||
"id": "6ff62909f822ba9b",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "pdf",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Exercise Booklet.pdf"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "90e6fc52439b47f9",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "markdown",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Week 6/Week 6 Database Systems.md",
|
||||
"file": "Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
|
||||
"mode": "source",
|
||||
"source": false
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "1dda09aee43b18f4",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "full-calendar-view",
|
||||
"state": {}
|
||||
}
|
||||
}
|
||||
],
|
||||
"currentTab": 1
|
||||
},
|
||||
{
|
||||
"id": "1177e1c9853f407d",
|
||||
"type": "tabs",
|
||||
"children": [
|
||||
{
|
||||
"id": "53317c245e573f8c",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "pdf",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Exercise Booklet.pdf"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -86,7 +121,7 @@
|
||||
"state": {
|
||||
"type": "backlink",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Week 6/Week 6 Database Systems.md",
|
||||
"file": "Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
|
||||
"collapseAll": false,
|
||||
"extraContext": false,
|
||||
"sortOrder": "alphabetical",
|
||||
@@ -103,7 +138,7 @@
|
||||
"state": {
|
||||
"type": "outgoing-link",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Week 6/Week 6 Database Systems.md",
|
||||
"file": "Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
|
||||
"linksCollapsed": false,
|
||||
"unlinkedCollapsed": true
|
||||
}
|
||||
@@ -126,7 +161,7 @@
|
||||
"state": {
|
||||
"type": "outline",
|
||||
"state": {
|
||||
"file": "Semester 2/Database Systems/Week 6/Week 6 Database Systems.md"
|
||||
"file": "Semester 2/Database Systems/Week 7/Week 7 Database Systems.md"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -166,31 +201,37 @@
|
||||
"switcher:Open quick switcher": false
|
||||
}
|
||||
},
|
||||
"active": "505b4bbc8a7e15f6",
|
||||
"active": "90e6fc52439b47f9",
|
||||
"lastOpenFiles": [
|
||||
"images/Pasted image 20240220132550.png",
|
||||
"images/Pasted image 20240220132521.png",
|
||||
"images/Pasted image 20240220132154.png",
|
||||
"images/Pasted image 20240220132113.png",
|
||||
"images/Pasted image 20240220131641.png",
|
||||
"images/Pasted image 20240220131547.png",
|
||||
"Semester 2/Database Systems/Week 5/Week 5 Database Systems.md",
|
||||
"images/Pasted image 20240227162324.png",
|
||||
"images/Pasted image 20240227161946.png",
|
||||
"images/Pasted image 20240227161426.png",
|
||||
"Semester 2/Database Systems/Exercise Booklet.pdf",
|
||||
"Semester 2/Database Systems/Booklet of Lecture Slides for Semester Two (2).pdf",
|
||||
"Semester 2/Database Systems/Week 7/Week 7 Database Systems.md",
|
||||
"images/Pasted image 20240227133544.png",
|
||||
"images/Pasted image 20240227133521.png",
|
||||
"images/Pasted image 20240227132816.png",
|
||||
"images/Pasted image 20240227132741.png",
|
||||
"images/Pasted image 20240227132007.png",
|
||||
"images/Pasted image 20240227131954.png",
|
||||
"images/Pasted image 20240227131936.png",
|
||||
"images/Pasted image 20240227131321.png",
|
||||
"Semester 2/Database Systems/Week 7",
|
||||
"Semester 2/Database Systems/Week 6/Week 6 Database Systems.md",
|
||||
"Semester 2/Computer Systems Internals & Linux/Week 6/Week 6 Computer Systems Internals.md",
|
||||
"Semester 2/Programming 2/Assessment 3 Revision/Parameter Passing WS3.md",
|
||||
"Semester 2/Computer Systems Internals & Linux/Week 6",
|
||||
"Semester 2/Computer Systems Internals & Linux/Untitled.md",
|
||||
"Semester 2/Programming 2/Assessment 3 Revision/Parameter Passing WS2.md",
|
||||
"Semester 2/Programming 2/Assessment 3 Revision/Parameter Passing WS1.md",
|
||||
"Semester 2/Programming 2/Assessment 3 Revision",
|
||||
"Semester 2/Database Systems/Week 5/Week 5 Database Systems.md",
|
||||
"Semester 2/Database Systems/Week 6",
|
||||
"Semester 2/Programming 2/Project/Part 2/Library.java#",
|
||||
"Semester 2/Programming 2/Project/Part 2/Periodical.java#",
|
||||
"Semester 2/Programming 2/Project/Part 2/data/item_data_2.txt",
|
||||
"Semester 2/Programming 2/Project/Part 2/data/item_data_1_v2.txt",
|
||||
"Semester 2/Programming 2/Project/Part 2/data/item_data_1.txt",
|
||||
"Semester 2/Programming 2/Project/Part 2/data",
|
||||
"Semester 2/Programming 2/Project/Part 2/LibraryItem.ctxt",
|
||||
"Semester 2/Programming 2/Project/Part 2/LibraryItem.class",
|
||||
"Semester 2/Programming 2/Project/Part 2/LibraryItem.java#",
|
||||
"Semester 2/Programming 2/Project/Part 2/Part 1/doc/resources/inherit.gif",
|
||||
"Semester 2/Programming 2/Project/Part 2/doc/resources/inherit.gif",
|
||||
"Semester 2/Programming 2/Project/Part 1 (copy)/doc/resources/inherit.gif",
|
||||
"images/Pasted image 20240213172702.png",
|
||||
"images/Pasted image 20240213172509.png",
|
||||
"Semester 2/Database Systems/Week 4/Week 4 Database Systems.md",
|
||||
"Semester 2/HCI/Week 2/Week 2 Human Computer Interfaces.md",
|
||||
"Semester 2/HCI/Week 3/Week 3 Human Computer Interfaces.md",
|
||||
@@ -208,12 +249,6 @@
|
||||
"Semester 2/Database Systems/Week 2/Week 2 Database Systems.md",
|
||||
"CCNA/1 - Networking Today/1.2 - Network Components.md",
|
||||
"CCNA/1 - Networking Today/1.1 - Networks Affect our Lives.md",
|
||||
"CCNA/Module Information.md",
|
||||
"Semester 2/Database Systems/Week 1/Week 1 Database Systems.md",
|
||||
"Semester 2/Computer Systems Internals & Linux/Week 1/Week 2 Computer Systems Internals.md",
|
||||
"Semester 2/Programming 2/Week 2/Week 2 Programming 2.md",
|
||||
"Semester 2/Programming 2/Week 1/Week 1 Programming 2.md",
|
||||
"Semester 2/Programming 2/README.md",
|
||||
"Database Systems/Untitled.canvas",
|
||||
"Untitled 1.canvas",
|
||||
"Untitled.canvas"
|
||||
|
@@ -64,3 +64,38 @@ Similarly to the previous issue, we can solve this by locking T3 until the data
|
||||
- 
|
||||
We can solve this issue by locking the data items appropriately. By write locking balx and balz in T5, T6 must wait to modify or read any data from either of these data items to preserve consistency.
|
||||
- 
|
||||
|
||||
# Tutorial
|
||||
|
||||
1. .
|
||||
1. Locks are used to preserve database consistency by disallowing reads and / or writes.
|
||||
2. Locks are implemented by requiring transactions to request locks before reading or updating an item.
|
||||
3. An exclusive lock is a write lock, restricting read and write access of the database item to the transaction that requested it.
|
||||
4. An exclusive lock is granted if no other lock is currently in effect on a data item.
|
||||
2. .
|
||||
1. False, a durable transaction is where all changes to data are persistent, even through power loss or system failure. The statement is an example of atomicity.
|
||||
2. True
|
||||
3. True
|
||||
4. False, mutual blocking of data access in 2PL causes a deadlock
|
||||
3. .
|
||||
1. .
|
||||
|
||||
| Time | Transaction 1 | Transaction 2 | balance |
|
||||
| ---- | ---- | ---- | ---- |
|
||||
| t1 | begin | | 100 |
|
||||
| t2 | read_lock( balance ) | | 100 |
|
||||
| t3 | read( balance ) | | 100 |
|
||||
| t4 | balance:=balance-60 | begin transaction | 40 |
|
||||
| t5 | … | wait | 40 |
|
||||
| t6 | abort, rollback. <br>Release lock ( balance ) | wait | 100 |
|
||||
| t7 | | read_lock( balance ) | 100 |
|
||||
| t8 | | read( balance ) | 100 |
|
||||
| t9 | | balance:=balance-10 | 90 |
|
||||
| t10 | | write( balance ) | 90 |
|
||||
| t11 | | commit.<br>Release lock ( balance ) | 90 |
|
||||
|
||||
# Laboratory
|
||||
1. 
|
||||
2. 
|
||||
3. 
|
||||
4. 
|
113
Semester 2/Database Systems/Week 7/Week 7 Database Systems.md
Normal file
@@ -0,0 +1,113 @@
|
||||
# Deadlock
|
||||
|
||||
An impasse that may result when two or more transactions are waiting for locks held by the other to be released.
|
||||
ex. 
|
||||
|
||||
## Breaking a Deadlock
|
||||
|
||||
Abort one or more transactions.
|
||||
If deadlock occurs, DBMS will
|
||||
|
||||
- Automatically abort one transaction
|
||||
- Release locks, allow other transactions to continue
|
||||
- Restart aborted transaction
|
||||
|
||||
## Handling Deadlock
|
||||
|
||||
- Timeouts
|
||||
- Prevention
|
||||
- Detection and recovery
|
||||
|
||||
### Timeouts
|
||||
|
||||
Transactions that request a lock will only wait a system-defined amount of time.
|
||||
If lock not granted within the period,
|
||||
|
||||
- Lock request times out
|
||||
- DBMS assumes deadlock
|
||||
- Transaction aborted and restarted
|
||||
|
||||
### Prevention
|
||||
|
||||
DBMS looks ahead to see if a transaction causes deadlock.
|
||||
Order transactions using transaction timestamps with the following methods:
|
||||
|
||||
#### Wait-Die
|
||||
|
||||
Only older transaction can wait for a younger one.
|
||||
If a younger transaction requests a lock held by an older one, the younger one is aborted then restarted with the same timestamp, so will eventually become the oldest active transaction and will not die.
|
||||

|
||||
|
||||
#### Wound-Wait
|
||||
|
||||
If an older transaction requests a lock held by a younger one, the younger one is aborted.
|
||||

|
||||
|
||||
#### Summary of Prevention Techniques
|
||||
|
||||
Deadlock free
|
||||
Wait-Die, transactions only wait for younger transactions, no cycle
|
||||
Wound-Wait, transactions only wait for older transactions, no cycle
|
||||
Both may cause transactions to be aborted and restarted needlessly, even if they would not cause a deadlock.
|
||||
|
||||
### Deadlock Prevention and Recovery
|
||||
|
||||
DBMS allows deadlock to occur, recognises and breaks it.
|
||||
DBMS uses a wait-for-graph (WFG) that shows transaction dependencies
|
||||
A WFG is generated by creating a node for each transaction, and an edge ( Ti -> Tj ). If Ti is waiting to lock an item locked by Tj.
|
||||
Deadlock exists if the WFG contains a cycle.
|
||||
A WFG is created at regular intervals
|
||||
|
||||
#### Wait-For-Graph Example 1
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
#### Example 2
|
||||
|
||||

|
||||
Edge Ti -> Tj, if Ti waiting to lock item locked by Tj
|
||||

|
||||
Since the WFG contains a cycle, deadlock exists.
|
||||
|
||||
#### Example 3
|
||||
|
||||

|
||||
|
||||
- 
|
||||
Graph contains several cycles, deadlock exists
|
||||
|
||||
#### Recovery
|
||||
|
||||
Several issues that DBMS needs to address
|
||||
|
||||
- Selecting deadlock victim that will minimise cost of breaking deadlock
|
||||
- May be better to abort transaction that has just started, rather than one that has been running longer.
|
||||
- May be better to abort transaction that makes little change to database, rather than one that makes a significant change
|
||||
- Avoiding starvation
|
||||
- Occurs when same transaction is chosen as victim, preventing completion
|
||||
- DBMS may try to avoid this by storing a count of abortions of a transaction.
|
||||
|
||||
# Tutorial
|
||||
|
||||
1. .
|
||||
1. Deadlock occurs when one or more transactions are held by each other, and neither can continue
|
||||
2. The DBMS detects deadlock by utilising a WFG, and checking for cycles
|
||||
3. The only way to break a deadlock is by aborting one or more transaction.
|
||||
2. .
|
||||
|
||||
| Transaction | Locked | Waiting For |
|
||||
| ----------- | ------ | ----------- |
|
||||
| T1 | X | |
|
||||
| T2 | Z | |
|
||||
| T3 | | Z |
|
||||

|
||||
|
||||
3. 
|
||||
There is no deadlock since there are no cycles.
|
||||
|
||||
4. 
|
||||
There may be a deadlock, since there are multiple cycles in the WFG
|
||||
|
||||
# Database Recovery Part 1
|
@@ -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
|
BIN
images/Pasted image 20240223101441.png
Normal file
After Width: | Height: | Size: 49 KiB |
BIN
images/Pasted image 20240223101502.png
Normal file
After Width: | Height: | Size: 47 KiB |
BIN
images/Pasted image 20240223102417.png
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
images/Pasted image 20240223102717.png
Normal file
After Width: | Height: | Size: 55 KiB |
BIN
images/Pasted image 20240227130252.png
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
images/Pasted image 20240227131306.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
images/Pasted image 20240227131321.png
Normal file
After Width: | Height: | Size: 29 KiB |
BIN
images/Pasted image 20240227131936.png
Normal file
After Width: | Height: | Size: 69 KiB |
BIN
images/Pasted image 20240227131954.png
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
images/Pasted image 20240227132007.png
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
images/Pasted image 20240227132741.png
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
images/Pasted image 20240227132816.png
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
images/Pasted image 20240227133521.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
images/Pasted image 20240227133544.png
Normal file
After Width: | Height: | Size: 68 KiB |
BIN
images/Pasted image 20240227161426.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
images/Pasted image 20240227161946.png
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
images/Pasted image 20240227162324.png
Normal file
After Width: | Height: | Size: 12 KiB |