informatik ii
play

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


  1. Informatik II Tutorial 5 Mihai Bâce mihai.bace@inf.ethz.ch Mihai Bâce | | 31-Oct-19 1

  2. Overview § Debriefing Exercise 4 § Briefing Exercise 5 Mihai Bâce | | 31-Oct-19 2

  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! Mihai Bâce | | 31-Oct-19 3

  4. U4.A2 Ackermann function § Recursive definition Mihai Bâce | | 31-Oct-19 4

  5. U4.A2 Pseudocode sample m while(stack.size() > 1){ push n on stack n 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] “Function call” if n = 0 then push m+1 on stack [A(0,m)=m+1] elseif m = 0 if n == 0 à result = m+1 then push n-1 on stack; push 1 on stack [A(n,0)=A(n-1,1)] else if m == 0 à push(n-1), push(1) else push n-1 on stack push n on stack else push(n-1), push(n), push(m-1) push m-1 on stack [A(n,m)=A(n-1,A(n,m-1))] the uppermost element from the stack is the result Mihai Bâce | | 31-Oct-19 5

  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 Mihai Bâce | | 31-Oct-19 6

  7. JAVA Tips & Tricks Mihai Bâce | | 31-Oct-19 7

  8. Data types § Primitive Types § E.g. byte, int, float, char § Reference-Type § E.g. Arrays, Strings, Classes Mihai Bâce | | 31-Oct-19 8

  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. Mihai Bâce | | 31-Oct-19 9

  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. Mihai Bâce | | 31-Oct-19 10

  11. JAVA: Call by reference vs. call by value § Modification is possible, interchanging not Main programm is called neverthelsess After swap(...) swap(myPoint1, myPoint2) main(...) myPoint1 myPoint1 int x1; int x1; int y1; int y1; myPoint1 int x1; p1 p1 int y1; myPoint2 myPoint2 myPoint2 int x2; int x2; int x2; int y2; int y2; int y2; p2 p2 Good reference : www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html Mihai Bâce | | 31-Oct-19 11

  12. Overview § Debriefing Exercise 4 § Briefing Exercise 5 Mihai Bâce | | 31-Oct-19 12

  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? myList value value value value value 22 3 76 32 15 null next next next next next Mihai Bâce | | 31-Oct-19 13

  14. U5 Lists public static String toString(List list) { if (list == null) § toString(List list) return "null"; return list.value + "," + toString(list.next); } myList value value value value value 22 3 76 32 15 null next next next next next u5a1.Lists.toString(myList) 76,15,22,3,32,null Mihai Bâce | | 31-Oct-19 14

  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) Mihai Bâce | | 31-Oct-19 15

  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! Mihai Bâce | | 31-Oct-19 16

  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 Mihai Bâce | | 31-Oct-19 17

  18. U5A3 Sorting lists § insertSorted § Insert a value in a sorted list § sort § Sort a given list Mihai Bâce | | 31-Oct-19 18

  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 Mihai Bâce | | 31-Oct-19 19

  20. Have Fun! Image Mihai Bâce | | 31-Oct-19 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend