| |
Mihai Bâce
mihai.bace@inf.ethz.ch
22-Oct-19 1
Informatik II
Tutorial 4
Mihai Bâce
Informatik II Tutorial 4 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation
Informatik II Tutorial 4 Mihai Bce mihai.bace@inf.ethz.ch Mihai Bce | | 22-Oct-19 1 Overview Debriefing Exercise 3 Briefing Exercise 4 Mihai Bce | | 22-Oct-19 2 U3.A1 Program verification a) What is the loop invariant for
| |
mihai.bace@inf.ethz.ch
22-Oct-19 1
Mihai Bâce
| | 22-Oct-19 Mihai Bâce 2
| |
22-Oct-19 Mihai Bâce 3
| | 22-Oct-19 Mihai Bâce 4
static int f(int i, int j) { assert(i >= 0 && j >= 0); int u = i, z = 0; // {z + u*j - i*j = 0 and u >= 0} ==> ok 0 + i*j - i*j = 0 and i >= 0 while (u > 0) { // {z + u*j - i*j = 0 and u >= 0 and u > 0} // {z + j - j + u*j - i*j = 0 and u > 0} z = z + j; // {z - j + u*j - i*j = 0 and u > 0} // {z - j + (u – 1 + 1)*j - i*j = 0 and u > 0} u = u - 1; // {(z - j) + (u + 1)*j - i*j = 0 and u + 1 > 0} => // Loop invariant holds because z - j + u*j + j - i*j = z + u*j - i*j = 0 // {z + u*j - i*j = 0 and u >= 0} } // {z + u*j - i*j = 0 and u >= 0 and u <= 0} return z; }
| |
22-Oct-19 Mihai Bâce 5
| |
22-Oct-19 Mihai Bâce 6
If condition and invariant Are true before The loop body and the invariant is true after the loop body then the negated condition (u=0) holds after the loop body But does it mean that the implementation is correct?
| |
22-Oct-19 Mihai Bâce 7
More information: https://en.wikipedia.org/wiki/Correctness_(computer_science) u is always > 0
| | 22-Oct-19 Mihai Bâce 8
| | 22-Oct-19 Mihai Bâce 9
| | 22-Oct-19 Mihai Bâce 10
ASCII Character Codes
| | 22-Oct-19 Mihai Bâce 11
§ Any modification leads to a new copy of the object.
| | 22-Oct-19 Mihai Bâce 12
| | 22-Oct-19 Mihai Bâce 13
Possible Impossible
| | 22-Oct-19 Mihai Bâce 14
Possible Impossible
| | 22-Oct-19 Mihai Bâce 15
| | 22-Oct-19 Mihai Bâce 16
private private static static int int parseTree(String String kd, int int offset) throws ParseException ParseException { if if (offset >= >= kd. .length()) { throw throw new new ParseException ParseException("Unexpected end of string", offset); } if if (kd. .charAt(offset) == == '-') { return return offset + + 1 1; } else else {
= parseNode(kd, offset); if if ((offset < < kd. .length()) && && (kd. .charAt(offset) == == '(')) {
+= 1 1;
= parseSubtree(kd, offset); if if ((offset < < kd. .length()) && && (kd. .charAt(offset) == == ')')) {
+= 1 1; } else else { throw throw new new ParseException ParseException("expected ')'", offset); } } } return return offset; }
| | 22-Oct-19 Mihai Bâce 17
private private static static int int parseSubtree(String String kd, int int offset) throws ParseException ParseException { if if (offset >= >= kd. .length()) { throw throw new new ParseException ParseException("unexpected end of string after '('", offset); }
= parseTree(kd, offset); while while ((offset < < kd. .length()) && && (kd. .charAt(offset) == == ',')) {
+= 1 1;
= parseTree(kd, offset); } return return offset; }
| | 22-Oct-19 Mihai Bâce 18
private private static static int int parseNode(String String kd, int int offset) throws ParseException ParseException { if if (offset >= >= kd. .length()) { throw throw new new ParseException ParseException("Expected a node", offset); } if if (Character Character. .isUpperCase(kd. .charAt(offset))) { return return offset + + 1 1; } else else { throw throw new new ParseException ParseException(String String. .format("'%c' is not a valid node name", kd. .charAt(offset)), offset); } }
| | 22-Oct-19 Mihai Bâce 19
public public static static void void parse(String String kd) throws ParseException ParseException { int int offset = = parseTree(kd, 0 0); if if (offset != != kd. .length()) { throw throw new new ParseException ParseException("Garbage at the end of the tree", offset); } }
| | 22-Oct-19 Mihai Bâce 20
| | 22-Oct-19 Mihai Bâce 21
Pizza boxes Stack
| | 22-Oct-19 Mihai Bâce 22
§ Initializes internal Array § Capacity is an argument to the constructor
§ Expected Output: "[e0, e1, e2, …]" § Concatenation
§
String: str += "bar";
§
StringBuffer: buf.append("bar");
§ Capacity doubled, copy old values
| | 22-Oct-19 Mihai Bâce 23
§ Standard stack functions § Arguments are of type int § If necessary, call grow()
§ Number of elements currently on the stack
§ Total number of elements which fit on the current stack until the next grow
| | 22-Oct-19 Mihai Bâce 24
Ackermann function § Recursive Definition § Grows extremely fast § A(3,3) = 61 § A(4, 2) has already 19729 decimal places!!
Wilhelm Ackermann (1986 – 1962, Germany)
| | 22-Oct-19 Mihai Bâce 25
| | 22-Oct-19 Mihai Bâce 26
Wikipedia: Ackermann function
| | 22-Oct-19 Mihai Bâce 27
| | 22-Oct-19 Mihai Bâce 28
§ Ackermann’s formula always requires (exactly) two values: § The currently required values should be at the top of the stack… § What does it means when there is one item left in the stack? Stack stack = new Stack(); stack.push(4); stack.push(7); while(stack.size()!=1) { . . . }
stack
| | 22-Oct-19 Mihai Bâce 29
stack
| | 22-Oct-19 Mihai Bâce 30
Stack A(1,1) n = 1 m = 1 Push Pop Start Iteration n = 1 m = 1 n == 0? m == 0? else No. No. Push n n - 1 m - 1 n = 1 m = 0 n = 0 n = 1 m = 0 1 n - 1 Push n = 0 m = 1 n = 0 m = 1 Push m + 1 m = 2 n = 0 m = 2 m = 3 Pop A(1,1) = 3 End size >= 2? No. A(1,1) A(1,0) A(0, 1) <- 2 <- 2 A(0, 2) <- 3 <- 3
By Leyna Sadamori
| | 22-Oct-19 Mihai Bâce 31
| | 22-Oct-19 Mihai Bâce 32
| | 22-Oct-19 Mihai Bâce 33
D:\Projects\DisassemblerDemo> javac JavapTip.java //compiler java JavapTip //run javap –c –private JavaTip //disassembler Common mistake: „javap is not recognized as an internal or external command,
Reason: java binaries are not defined in System variable PATH Solution: RClick on Computer à Properties à Advanced System Settings à Environment Variables à PATH à add (where you installed the Java JDK) save and restart Windows ;C:\Program Files\Java\jdkX.Y.Z\bin
| | 22-Oct-19 Mihai Bâce 34
| | 22-Oct-19 Mihai Bâce 35
§ E.g. if_icmple: le = less or equal
§ Ifeq: equal to 0 § Ifne: not equal to 0
| | 22-Oct-19 Mihai Bâce 36
Image