Informatik II Tutorial 5 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation

informatik ii
SMART_READER_LITE
LIVE PREVIEW

Informatik II Tutorial 5 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation

Informatik II Tutorial 5 Mihai Bce mihai.bace@inf.ethz.ch Mihai Bce | | 31-Oct-19 1 Overview Debriefing Exercise 4 Briefing Exercise 5 Mihai Bce | | 31-Oct-19 2 U4.A1 Stack Noteworthy Two attributes: buffer length


slide-1
SLIDE 1

| |

Mihai Bâce

mihai.bace@inf.ethz.ch

31-Oct-19 1

Informatik II

Tutorial 5

Mihai Bâce

slide-2
SLIDE 2

| | 31-Oct-19 Mihai Bâce 2

Overview

§ Debriefing Exercise 4 § Briefing Exercise 5

slide-3
SLIDE 3

| | 31-Oct-19 Mihai Bâce 3

U4.A1 Stack

§ Noteworthy

§ Two attributes: buffer length and size § capacity : buffer.length (Array indices from 0 to length-1) § empty : size == 0 § size : index of first free space at the top

§ void push(int value) { … buffer[size++] = value; } § grow

§ Conditions of Grow in push: size() == capacity() § Java-library functions (search and copy) § int[] Arrays.copyOf(int[] original, int newLength)

§ JavaDoc

§ How it is documented!

slide-4
SLIDE 4

| | 31-Oct-19 Mihai Bâce 4

U4.A2 Ackermann function

§ Recursive definition

slide-5
SLIDE 5

| | 31-Oct-19 Mihai Bâce 5

U4.A2 Pseudocode sample

push n on stack push m on stack As long as the stack's size is greater than 1 pop the uppermost element from stack to m [m] pop the uppermost element from stack to n [n] if n = 0 then push m+1 on stack [A(0,m)=m+1] elseif m = 0 then push n-1 on stack; push 1 on stack [A(n,0)=A(n-1,1)] else push n-1 on stack push n on stack push m-1 on stack [A(n,m)=A(n-1,A(n,m-1))] the uppermost element from the stack is the result

n m

while(stack.size() > 1){ .... if n == 0 à result = m+1 else if m == 0 à push(n-1), push(1) else push(n-1), push(n), push(m-1) “Function call”

slide-6
SLIDE 6

| | 31-Oct-19 Mihai Bâce 6

U4.A3

§ SourceCode-Bytecode, assignment clear? § Order of parameters / return, clear?

return A(n-1, A(n, m-1)) 21: aload 0 22: iload 1 23: iconst 1 24: isub 25: aload 0 26: iload 1 27: iload 2 28: iconst 1 29: isub 30: invokevirtual 33: invokevirtual 36: ireturn

slide-7
SLIDE 7

| | 31-Oct-19 Mihai Bâce 7

JAVA Tips & Tricks

slide-8
SLIDE 8

| | 31-Oct-19 Mihai Bâce 8

Data types

§ Primitive Types

§ E.g. byte, int, float, char

§ Reference-Type

§ E.g. Arrays, Strings, Classes

slide-9
SLIDE 9

| | 31-Oct-19 Mihai Bâce 9

Call by

§ Call by value

§ The method receives a copy of the variables § No connection between the data in the caller and the data in the function

§ Call by reference

§ Instead of copying the data, you assign a reference to it § Method calls of a referenced object work on the same object which is visible from outside.

slide-10
SLIDE 10

| | 31-Oct-19 Mihai Bâce 10

Call by value vs. call by reference

§ In C++ both are possible

§ Call by value § Call by reference

§ Java is always call by value

§ This means, that when passing reference types, the address value is copied a local variable! § In case of transferring from a primitive types, the value would be copied in local copy.

slide-11
SLIDE 11

| | 31-Oct-19 Mihai Bâce 11

JAVA: Call by reference vs. call by value

§ Modification is possible, interchanging not

int x1; int y1;

myPoint1

int x2; int y2;

myPoint2 main(...)

int x1; int y1;

myPoint1 p1

int x2; int y2;

myPoint2 p2

Main programm is called neverthelsess

swap(myPoint1, myPoint2)

int x1; int y1;

myPoint1 p1

int x2; int y2;

myPoint2 p2 After swap(...)

Good reference: www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

slide-12
SLIDE 12

| | 31-Oct-19 Mihai Bâce 12

Overview

§ Debriefing Exercise 4 § Briefing Exercise 5

slide-13
SLIDE 13

| | 31-Oct-19 Mihai Bâce 13

U5 Lists

§ Features: § Dynamic size -> no initialization like in arrays

§ How do we determine the size? § How de we iterate over it? § When are we at the end of the list?

value 76 next value 15 next value 22 next value 3 next value 32 next

myList null

slide-14
SLIDE 14

| | 31-Oct-19 Mihai Bâce 14

U5 Lists

§ toString(List list)

public static String toString(List list) { if (list == null) return "null"; return list.value + "," + toString(list.next); } value 76 next value 15 next value 22 next value 3 next value 32 next

myList null

76,15,22,3,32,null

u5a1.Lists.toString(myList)

slide-15
SLIDE 15

| | 31-Oct-19 Mihai Bâce 15

U5A1 Lists – Implementation (1)

§ add

§ Add a value to the front of the list

§ size

§ Calculate the length of the list

§ sum

§ Sum the values in the list

§ last

§ End of list (last node before the zero, otherwise the zero)

slide-16
SLIDE 16

| | 31-Oct-19 Mihai Bâce 16

U5A1 Lists – Implementation (2)

§ sublist

§ ”Sublist" from a given index

§ valueAt

§ Return the value of a given index in the list

§ index

§ Index of the first node with a given value

§ Tip: Consider Helper functions (code reusability!)

§ E.g. nodeAt

§ Similar usability in sublist and valueAt § You use when manipulating the list as well…

§ Must also be recursively implemented!

slide-17
SLIDE 17

| | 31-Oct-19 Mihai Bâce 17

U5.A2 More Lists

§ append

§ Attach a value at the end of list

§ concat

§ Attach a list to the back of another list

§ insertAt

§ Insert an element to list after certain index

§ remove

§ Delete a value in the list at certain position

slide-18
SLIDE 18

| | 31-Oct-19 Mihai Bâce 18

U5A3 Sorting lists

§ insertSorted

§ Insert a value in a sorted list

§ sort

§ Sort a given list

slide-19
SLIDE 19

| | 31-Oct-19 Mihai Bâce 19

U5.A4 Back to stacks

§ Implement a stack using a list

§ push – first element of the list is at the top of the stack § pop – don’t forget to update the references § peek § empty § size

slide-20
SLIDE 20

| | 31-Oct-19 Mihai Bâce 20

Have Fun!

Image