1 of 65

CSE 373 Su23 Section 1

Welcome, 12X/14X Review��TA: [insert TA name here]

2 of 65

Who Am I?

[insert TA intro here]

3 of 65

Ice-breaker

  • In your groups, introduce yourself to each other!
    • Pronouns
    • Major/Interests
    • The reason you take this class or what is the unexpected or surprising thing you had until now.

4 of 65

Administrivia

5 of 65

Course Logistics

  • Major Course Topics:
    • Data Structures and ADTs
    • Graphs and Graph Algorithms
    • Algorithm Analysis
    • Sorting

  • Components
    • Projects: There will be five programming projects (P0 through P4). These projects are completely autograded, you may work with a partner
    • Exercises: There will be seven individual exercises (EX1 to EX7)
    • Exams: There will be a midterm and a final held during lecture

6 of 65

Course Logistics (cont.)

  • Lateness
    • You have five late days to spend throughout the quarter that can be used on projects or exercises that allow you to submit up to 24 hours late without penalty

  • Grading Breakdown
    • Projects: 45%
    • Exercises 40%
    • Final: 15%
    • Surveys: Up to 0.05 GPA bump

  • See the Course Syllabus for GPA guarantees

7 of 65

Golden Rule of Section

  • Please don’t be afraid to ask questions :)
  • Use Ed Board if you have questions
    • If it is a private matter, use the Private Ed Post feature
    • If it is a REALLY really private question, then you can email TAs directly

  • Section videos will be posted to Canvas in case you are unable to make it to section.
    • However, you should still come to section. There are in-person learning opportunities that you will miss if you consistently skip.

8 of 65

MicroTeach:

Passing by Value and Reference

9 of 65

Note: The underlined code has just been executed.��Code for main is underlined in red. Code in functions called by main is underlined in blue.

10 of 65

main

x

y

1

0

1

Note 2: x is an int (primitive type). y and z are arrays (reference type). Hence the arrows.

z

0

1

2

2

Note 1: Each method in Java gets its own set of variables to operate on. This is called a stack frame (you don’t need to know the name).

11 of 65

main

x

y

1

0

1

z

0

1

2

2

foo

1

x

z

y

Note: The int is passed by value, the arrays by reference. This is because an int is a primitive type, while an array is not!

12 of 65

main

x

y

1

0

1

z

0

1

2

2

foo

2

x

z

y

Because it was passed by value, each method gets its own copy of the int!

13 of 65

main

x

y

1

0

1

z

0

1

2

2

foo

2

x

z

y

1

2

3

14 of 65

main

x

y

1

0

1

z

0

1

2

2

15 of 65

Go To Worksheet��Do Problem 1.1A

16 of 65

Problem 1.1a Reference Semantics

17 of 65

Note: The underlined code has just been executed.��Code for main is underlined in red. Code in functions called by main is underlined in blue.

18 of 65

main

x

a

1

0

0

Output:

Note: When an array is born, it holds the default value for its data type unless otherwise specified. (The default for int is 0!)

19 of 65

main

x

a

1

0

0

Output:

mystery1

x

list

1

Note: The int is passed by value, the array by reference.This is because an int is a primitive type, while an array is not!

20 of 65

main

x

a

1

0

1

Output:

mystery1

x

list

1

21 of 65

main

x

a

1

0

1

Output:

mystery1

x

list

2

Note: Each method has its own set of variables that it acts on!�(So the x in the mystery1 method changes).

22 of 65

main

x

a

1

0

1

Output:

2 [0, 1]

mystery1

x

list

2

23 of 65

main

x

a

1

0

1

Output:

1 [0, 1]

24 of 65

main

x

a

0

0

2

Output:

25 of 65

main

x

a

0

0

2

Output:

mystery1

x

list

0

26 of 65

main

x

a

0

1

2

Output:

mystery1

x

list

0

27 of 65

main

x

a

0

1

2

Output:

mystery1

x

list

1

28 of 65

main

x

a

0

1

2

Output:

1 [1, 2]

mystery1

x

list

1

29 of 65

main

x

a

0

1

2

Output:

0 [1, 2]

30 of 65

main

x

a

0

1

2

Output:

mystery2

x

list

0

31 of 65

main

x

a

0

1

2

Output:

Note: list is a reference (or pointer) to an array, so here we change the reference to point to a new array.

mystery2

x

list

0

0

0

32 of 65

main

x

a

0

1

2

Output:

0 [0, 0]

mystery2

x

list

0

0

0

33 of 65

main

x

a

0

1

2

Output:

0 [1, 2]

34 of 65

Generics

35 of 65

Overview of Generics

  • Generics allow a single class to work with many data types.
    • We define one (or more) generic types in our class declaration, and we can refer to the same type throughout our class implementation

  • The generic type is a placeholder for the type a client passes into the “<>” brackets when creating a new instance of our class
    • For example, a user creates a new Integer List as follows: List<Integer> myList = new ArrayList<Integer>();

36 of 65

Making a Class Generic

  • To make a class generic:
    • Use the “<>” brackets next to the class name to specify a generic type. This will allow clients to decide what type of objects they want to pass into our generic class.

  • Example class declaration with generic type “T”:
    • public class MyGenericClass<T> {...}

37 of 65

Generic MyArrayList Example

  • We use “<>” brackets to specify parameter types in generic class creation. This allows the user to decide what type of objects they want to pass into the MyArrayList, and we can now refer to that passed-in type throughout MyArrayList!

  • For example, T[] is an array of type T, the type given to use by the user.

  • Now you can declare an instance of MyArrayList of any data types (i.e Integer, String, ….).

Don’t worry if generics are still confusing. You’ll have an opportunity to practice in Friday’s lecture! :)

38 of 65

Go To Worksheet��Do Problems 1.2A and 1.3A��(Both removeFront() problems)

39 of 65

Problem 1.2a MyArrayList removeFront()

40 of 65

Example: MyArrayList removeFront(4)

data = [8, 17, 9, 24, 42, 3, 8]

size = 7

list.removeFront(4)

data = [42, 3, 8]

size = 3

Note: Remember that we’re implementing the MyArrayList data structure, so when we write the code for removeFront(k), we need to make sure to modify our data and size fields accordingly!

41 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8

17

k = 4

size = 7

42 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8

17

Intuition: We want our array to contain valid data in the first size - k indices!

k = 4

size = 7

43 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8

17

k = 4

size = 7

44 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8 42

17

k = 4

size = 7

45 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8 42

17

k = 4

size = 7

46 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8 42

17 3

k = 4

size = 7

47 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9

24

42

3

8

8 42

17 3

k = 4

size = 7

48 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

9 8

24

42

3

8

8 42

17 3

k = 4

size = 7

49 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

k = 4

size = 7

The first 3 indices of data now have what we want!

0

1

2

3

4

5

6

9 8

24

42

3

8

8 42

17 3

50 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

k = 4

size = 7

What about the remaining elements in data?

0

1

2

3

4

5

6

8

24

42

3

8

42

3

51 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

8

24

42

3

8

42

3

k = 4

size = 7 - 4

We can just get rid of them by reducing size!

52 of 65

Walkthrough: MyArrayList removeFront(4)

Expected behavior:

[8, 17, 9, 24, 42, 3, 8] → [42, 3, 8]

public void removeFront(int k) {...}

0

1

2

3

4

5

6

8

24

42

3

8

42

3

Note: The last 4 indices still exist in data. Remember that data.length != size. Since it’s an expectation that all our MyArrayList methods will only access indices of data between 0 and size - 1, we don’t have to worry about what values live beyond that! As such, the client of our class can only interact with indices 0 to size - 1.

k = 4

size = 3

53 of 65

Solution: MyArrayList removeFront(k)

public class MyArrayList<T> {

private T[] data;

private int size;

public void removeFront(int k) {

for (int i = 0; i < this.size - k; i++) {

this.data[i] = this.data[i + k];

}

this.size -= k;

}

// other methods commented out

}

54 of 65

Problem 1.3a MyLinkedList removeFront()

55 of 65

Example: MyLinkedList removeFront(2)

8

front

null

data

11

data

91

data

2

data

next

next

next

next

list.removeFront(2)

91

front

null

data

2

data

next

next

Note: Remember that we’re implementing the MyLinkedList data structure, so when we write the code for removeFront(k), we need to make sure to modify our front field accordingly!

56 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

57 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

Intuition: Advance the front pointer k times.

58 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

curr

59 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

curr

60 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

curr

61 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

curr

curr now points to what we want!

62 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

f

null

11

91

2

curr

63 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

8

null

11

91

2

f

Note: This part gets automatically deleted when Java runs its garbage collector because there’s nothing pointing to it!

64 of 65

Walkthrough: MyLinkedList removeFront(2)

public void removeFront(int k) {...}

8

f

null

11

91

2

Expected behavior:

91

f

null

2

Before:

After:

f

null

91

2

65 of 65

Solution: MyLinkedList removeFront(k)

public class MyLinkedList<T> {

private Node<T> front;

private static class Node<T> {

public final T data;

public Node<T> next;

}

public void removeFront(int k) {

Node<T> curr = this.front;

for (int i = 0; i < k; i++) {

curr = curr.next;

}

this.front = curr;

}

// other methods commented out

}