WRAV201 Introduction to data structures and algorithms
WRAV201 Introduction to data structures and algorithms
WRAV201
Introduction, Java, Identifiers
1
Click to edit Master title style
Lecturer & Moodle
Yo u r g u i d e … a n d m a p …
2 2
Module Overview
Click to edit Master title style
3 3
Click to edit Master title style
Recommended Textbook
Optional – for those who like holding
something tangible…
4 4
Assessments
Click to edit Master title style
Module Assessment
1:
Friday 14 March: 14:00-
16:00, and 16:30-18:30
Module Assessment
2: Saturday 26 April:
9:00-11:00, and 11:30-
13:30
Module Assessment
3: concession letters ASAP
Email
No letter = no concession
Friday 30 May: 9:00-
5 5
6 6
Click to edit Master title style
7 7
Click to edit Master title style
Java
Capuchinos are sooo tasty…
8 8
Click to edit Master title style
9
Click to edit
Software Master title style
Installation
10
10
Click to edit Master title style
Java vs C#
Which is better? Does it matter?
11
11
Click to edit Master title style
12
12
Click toStructure
Files & edit Master title style
• Both have
• Multi-language support, code completion, generation and checking tools
• Debugging tools – use them!!!
13
13
Click
Hello to edit Master title style
World
Java C#
See HelloWorld
14
14
Click to edit Master title style
User Input
Java C#
See LargerNumber 15
15
Click
OOP to edit Master title style
Java C#
See SumNumbers
17
17
Click
UsefultoJava
editTutorial
Master Sites
title style
https://www.flaticon.com
• Jenkov - https://jenkov.com/tutorials/java/index.html
• Baeldung - https://www.baeldung.com/java-tutorial
• Official Java documentation - https://docs.oracle.com/javase/tutorial/
• Important!!
• Use Java Language Level 11 for WRAV201/2 (and WRPV301/2)
• due to Android currently supporting up to Language Level 11
• To check/set
• File > Project Structure > Language Level → 11
18
18
Click to edit Master title style
19
19
Click to edit Master title style
Identifiers
• Identifier
• name for a variable, method or class
• Variables
• must be declared in a class or method
• Methods
• may only be declared within a class
• may not be nested
• Class
• must have a globally unique full name (package included in full name)
• e.g. javafx.scene.control.Button & java.awt.Button
• typically one class per file, file has same name as class
• may be nested (within another class or method – but more on that in WRPV301)
20
20
Click to edit Operator
Assignment Master title
= style
Memory
Say what now?
22
22
Click to edit
Allocating Master-title
Memory style
Objects
Block number: #1 #2 #3 #4 #5
Object: A B “joe” C Empty
23
23
Click to edit
Allocating Master-title
Memory style
Primitives
#10
Identifier Value
Example 1
Basic Example
25
25
Click to edit
Example Master title
1 – initially style is empty
all memory
class Program
{
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Empty Empty Empty Empty Empty Empty
26
Click to edit
Example Master
1 – new titleof
object style
type Program
public class Program
{
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1
1 27
Click to 1edit
Example Master
– stack title
memory forstyle
object created
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1
1 28
Click to edit
Example 1 – Master title
identifier style to stack
x added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1 x 5
1 29
Click to edit
Example 1 – Master title style
String object created
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 30
Click to edit
Example 1 – Master title
identifier style to stack
s added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 31
Click to edit
Example 1 – Master title
identifier style to stack
z added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
z ‘a’
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 32
Click to edit
Example 1 – Master title
identifier pi style
added to stack
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 33
Example 1 – identifier mylist added to stack and
Click to edit Master
ArrayList title
object added style
to heap
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x; mylist #3
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 1 34
Example 1 – identifier j added to stack and contents
Click to editfrom
copied Master
x title style
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
j 5
int j = x; mylist #3
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 1 35
Example 1 – when method exits all identifiers
Click to editinMaster
declared method title style from stack
are removed
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 0 36
Example 1 – whenever no references exist to an object in
Click to edit
the heap, MasterCollector
the Garbage title style
(can) remove it
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";
public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 37
Click to edit Master title style
Method
Parameters
38
38
Click
Methodto edit Master title style
parameters
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
}
System.out.println(larger);
10
public int max(int x, int y) {
x = 0;
if(x > y) return x;
else return y;
}
}
larger 10
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 48
Object 1 1
Example 2 – exit constructor, delete larger
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}
52
Click to edit Master title style
Intermission
As far as we got in Lecture 1
53
Click to edit Master title style
Manipulating
Memory
From within a method
54
54
Click to edit
Changing Masterfrom
memory title within
style methods
• the previous example showed why one can not change the original
value of parameters from within a method
• however, because of the fact the objects are stored on the heap, it is
possible to change objects from within a method
• consider the following method:
public void updateList(ArrayList list) {
list.add("World");
}
55
Example 3 – create Program, stack, variables
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList Empty Empty Empty Empty 56
Object 1 1
[] 1
Example 3 – create “Hello” string
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 57
Object 1 1
[] 1 1
Example 3 – add string
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 58
Object 1 1
[#3] 1 1
Example 3 – display myList
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
[Hello]
System.out.println(myList);
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 59
Object 1 1
[#3] 1 1
Example 3 – call updateList, copy params & values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 63
Object 1 1
[#3, #4] 1 1 1
Example 3 – exit updateList, release values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}
public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
}
System.out.println(myList); [Hello, World]
public void updateList(ArrayList list) {
list.add("World");
}
}
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 64
Object 1 1
[#3, #4] 1 1 1
Click
Note to edit Master title style
65
65
Click to edit Master title style
Equality
Are two things really equal?
66
66
Click to edit
Equality Master==
Operator, title style
#1 (Program Object 1)
Identifier Value
• the equality operator compares values in the stack memory
int x = 10;
int y = 10;
System.out.println(x == y); // prints "true"
68
68
Click
== andto Equals
edit Master title style
DIY
Now you try…
70
70
package memory.example5;
Click to edit Master title style
public void doStuff(String s) {
public class Program { s = "Goodbye";
public static void main(String[] args) { }
new Program();
} public void doStuffWithWrapper(StringWrapper w) {
w.theString = "Cheers";
public Program() { }
String s = "Hello"; }
StringWrapper w = new StringWrapper();
doStuffWithWrapper(w);
System.out.println(w.theString);
} Hello
Hi
Cheers
71
71
Click to edit Master title style
Homework
Already??
72
72
Click
To doto edit Master title style
• Moodle
• Enroll for WRAV201 on funda.mandela.ac.za and yenza.mandela.ac.za
• Enrolment key is Latte2025
• Book lecture and practical sessions
• Software (at home)
• Install Java Development Kit & IntelliJ
• file://postgrad-new.mandela.ac.za/wrpv%20software
• Go through the IDE Tour after installation
• Practical Sessions
• Start this week
• Complete first quiz during practical session in lab
• Start working on practical assignment, which is due next week
• Do the homework exercise in the Lecture 2 (on Moodle) for next week 73
73