Presentation is loading. Please wait.

Presentation is loading. Please wait.

What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)

Similar presentations


Presentation on theme: "What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)"— Presentation transcript:

1 What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion) grouping entities (arrays)

2 METHOD EXECUTION

3 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); }

4 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5

5 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5

6 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

7 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

8 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6

9 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 56

10 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 56

11 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 56 11

12 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 56 11

13 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 517 11

14 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 2217 11

15 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 2217 39

16 void mystery(int x, int y) { int z = x + y; y = y + z; x = x + y; z = x + y; return z; } void test() { int a = 5; int b = 6; int c = mystery(a, b); } 5 6 2217 39

17 Boolean Operators && || (row == 3) && (topCoins > 5) || (botCoins > 5) is equivalent in Java to ((row == 3) && (topCoins > 5)) || (botCoins > 5)

18 int i = 1; FOR loop and WHILE loop while (i < 10) { sum = sum + i; i = i + 1; } int sum = 0; for ( ; ; ) { } i < 10 i = i + 1 int i = 1; int sum = 0; sum = sum + i;

19 NESTED LOOPS

20 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :0 j :3

21 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :0 j :3 k :0 v :1 k :1 3, 0, 1

22 int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :1 k :2 Tracing Through Nested Loops 3 v :1 j :3 v :2 3, 0, 1 3, 1, 2

23 int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :2 k :3 Tracing Through Nested Loops 3 v :2 j :3 v :3 3, 0, 1 3, 1, 2 3, 2, 3

24 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :3 j :3 j :2 3, 0, 1 3, 1, 2 3, 2, 3

25 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :3 j :2 k :0 v :4 k :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4

26 int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } k :1 k :2 Tracing Through Nested Loops 3 v :4 j :2 v :5 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5

27 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :5 j :2 j :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5

28 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :5 j :1 k :0 v :6 k :1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

29 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :1 j :0 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

30 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :0 k :0 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

31 Tracing Through Nested Loops int mystery(int n) { int v = 0; for (int j = n; j >= 0; j = j – 1) { for (int k = 0; k < j; k = k + 1) { v = v + 1; print j, k, v; } return v; } 3 v :6 j :0 j :-1 3, 0, 1 3, 1, 2 3, 2, 3 2, 0, 4 2, 1, 5 1, 0, 6

32 2D ARRAYS

33 2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. 5692314 84325 374 6 5692314 7843259 6237418 9576423 1946572 4687153 9247186

34 2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 6] 1 [1.. 5] 2 [2.. 4] 3 [3.. 3] 0 1 2 3 4 5 6 5692314 84325 374 6 row columns 0 [0.. 7) 1 [1.. 6) 2 [2.. 5) 3 [3.. 4)

35 2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 7) = [0.. size-0) 1 [1.. 6) = [1.. size-1) 2 [2.. 5) = [2.. size-2) 3 [3.. 4) = [3.. size-3) 0 1 2 3 4 5 6 r [?.. ?) r [r.. size-r) 5692314 84325 374 6

36 2D Arrays Write a method sliceTriangle which takes as parameter a 2D array of integers and returns a copy of the original array such that only the elements on or above the two main diagonals are retained; the rest of the elements are set to 0. row columns 0 [0.. 7) = [0.. size-0) 1 [1.. 6) = [1.. size-1) 2 [2.. 5) = [2.. size-2) 3 [3.. 4) = [3.. size-3) int[][] sliceTriangle(int[][] table) { int size = table.length; int[][] result = new int[size][size]; for (int r = 0; r <= size/2; r++) { for (int c = r; c < size-r; c++) { result[r][c] = table[r][c]; } return result; } r [?.. ?) r [r.. size-r)

37 ALGORITHMS

38 Algorithms Sorting – Bubble Sort and Selection Sort Comparison – best case, average, case, worst case Searching – Linear Search and Binary Search Assumptions and Comparison

39 RECURSION

40 void mystery(int n) { if (n == 0) { System.out.println("done!"); } else { System.out.println(n); mystery(n - 1); } } void mystery(int n) { if (n == 0) { System.out.println("done!"); } else { mystery(n - 1); System.out.println(n); } } 5 4 3 2 1 done done 1 2 3 4 5 What is displayed after the call mystery(5) ?

41 Draw the “ears” first, then draw the “face” so it covers the “ears”. Step 1: Each “ear” is itself a Mickey – draw recursively! Step 2: Draw the “face” – a simple circle! Generate the following pattern

42 void drawMickey(double x, double y, double radius, int depth) { if(depth>0) { // DRAW EARS drawMickey(x-radius,y-radius,radius/2,depth-1); drawMickey(x+radius,y-radius,radius/2,depth-1); // DRAW FACE – will cover the ears canvas.drawCircle(x,y,radius+2,"white"); canvas.drawCircle(x,y,radius,"black"); } } void drawMickey(double x, double y, double radius, int depth) { if(depth>0) { // DRAW FACE canvas.drawCircle(x,y,radius+2,"white"); canvas.drawCircle(x,y,radius,"black"); // DRAW EARS – will cover the face drawMickey(x-radius,y-radius,radius/2,depth-1); drawMickey(x+radius,y-radius,radius/2,depth-1); } }

43 Things to Keep in Mind Write for people not for the machine –indent the code –put comments –design self-contained functions –use appropriate names –develop good test cases

44 What’s Next CS I – focus on describing procedures and how they interact CS II – focus on describing objects and how they interact CS III – focus on algorithms and data structures


Download ppt "What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)"

Similar presentations


Ads by Google