what’s in a language naming objects (variables) naming processes (methods/functions) making...

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

Post on 20-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

What’s in a Language

• naming objects (variables)

• naming processes (methods/functions)

• making decisions (if)

• making repetitions (for, while) (recursion)

• grouping entities (arrays)

METHOD EXECUTION

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); }

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

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

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);}

56

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);}

56

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);}

56

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);}

56

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);}

56

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); }

56

5 6

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);}

56

5 6

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);}

56

5 17

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); }

56

22 17

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);}

56

22 17

39

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); }

56

22 17

39

39

Boolean Operators && ||

(row == 3) && (topCoins > 5) || (botCoins > 5)

is equivalent in Java to

((row == 3) && (topCoins > 5)) || (botCoins > 5)

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;

NESTED LOOPS

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 :0j :3

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 :0j :3

k :0

v :1

k :1

3, 0, 1

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 :1k :2

Tracing Through Nested Loops

3

v :1j :3

v :2

3, 0, 13, 1, 2

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 :2k :3

Tracing Through Nested Loops

3

v :2j :3

v :3

3, 0, 13, 1, 23, 2, 3

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 :3j :3j :2

3, 0, 13, 1, 23, 2, 3

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 :3j :2

k :0

v :4

k :1

3, 0, 13, 1, 23, 2, 32, 0, 4

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 :1k :2

Tracing Through Nested Loops

3

v :4j :2

v :5

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 5

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 :5j :2j :1

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 5

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 :5j :1

k :0

v :6

k :1

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 51, 0, 6

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 :6j :1j :0

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 51, 0, 6

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 :6j :0

k :0

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 51, 0, 6

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 :6j :0j :-1

3, 0, 13, 1, 23, 2, 32, 0, 42, 1, 51, 0, 6

2D ARRAYS

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 diagonalsare retained; the rest of the elements are set to 0.

5 6 9 2 3 1 4

8 4 3 2 5

3 7 4

6

5 6 9 2 3 1 4

7 8 4 3 2 5 9

6 2 3 7 4 1 8

9 5 7 6 4 2 3

1 9 4 6 5 7 2

4 6 8 7 1 5 3

9 2 4 7 1 8 6

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 diagonalsare 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

5 6 9 2 3 1 4

8 4 3 2 5

3 7 4

6

row columns 0 [0 .. 7) 1 [1 .. 6) 2 [2 .. 5) 3 [3 .. 4)

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 diagonalsare 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)

5 6 9 2 3 1 4

8 4 3 2 5

3 7 4

6

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 diagonalsare 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)

ALGORITHMS

Algorithms

• Sorting – Bubble Sort and Selection Sort

• Comparison – best case, average, case, worst case

• Searching – Linear Search and Binary Search

• Assumptions and Comparison

RECURSION

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); } }

54321done

done12345

What is displayed after the call mystery(5) ?

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

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); } }

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

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