Review Loop structures 1 while loop 2 dowhile

  • Slides: 18
Download presentation
Review

Review

 • Loop structures 1) while loop 2) do-while loop 3) for loop •

• Loop structures 1) while loop 2) do-while loop 3) for loop • • Methods Static vs non-static methods Two dimensional arrays Sorting – bubble sort, selection sort

for loop for (int j = 0; j < 10; j++) {. . break;

for loop for (int j = 0; j < 10; j++) {. . break; } 1) 2) 3) 4) Scope of j: only inside the loop break used to exit the loop immediately Braces No ; immediately after the for(…. . )

int s=1; for(int j = 3; j >=0; j--) { s = s+ j;

int s=1; for(int j = 3; j >=0; j--) { s = s+ j; } System. out. println(s); =>? ? ? Write a for loop that will print the numbers 3, 6, 12, 24.

What is value of sum? int sum = 0; for(int j = 0; j

What is value of sum? int sum = 0; for(int j = 0; j < 5; j++) { for(int k =0; k < 8; k++) { sum += k; } } System. out. println(sum);

QUIZ TOPICS • Arrays class Arrays. sort(. . ) Arrays. binary. Search(…) • Array.

QUIZ TOPICS • Arrays class Arrays. sort(. . ) Arrays. binary. Search(…) • Array. List class Array. List. Obj. add(…) Array. List. Obj. get(…) Array. List. Obj. remove(…) Array. List. Obj. set(…) Array. List. Obj. size() • Wrapper classes Integer, Character, Boolean, Double • Recursion

 • What’s the difference between while and dowhile loop? • Rewrite while loop

• What’s the difference between while and dowhile loop? • Rewrite while loop using for loop or vice versa • Rewrite the following for loop to a while loop int j =0; sum = 0; for(j = 3; j <=79; j++) { sum = sum + j; System. out. println(sum); }

TERMS Iteration infinite loop nested loop Overflow Sentinel debugging technique • variable trace •

TERMS Iteration infinite loop nested loop Overflow Sentinel debugging technique • variable trace • • Exception • Array run time error • parameter passing to method pass by value pass by reference • enhanced for loop • Recursion

methods • Suppose there is method called calculate. Avg, which returns a double and

methods • Suppose there is method called calculate. Avg, which returns a double and has an int array as parameter, write the method signature(access level return type variable name…). No need to write the codes inside the method. • Try to call the method from main()

Static vs non-static methods public class Nerd { public static double abc; public int

Static vs non-static methods public class Nerd { public static double abc; public int xyz; public Nerd(){…} public double method. A(int x){…} public static void method. B(){. . } } to access method. A=> Nerd nerd. Class = new Nerd(); nerd. Class. method. A; to access method. B=> Nerd. method. B();

int min, min. Index; for (int i = 0; i < a. length; i++)

int min, min. Index; for (int i = 0; i < a. length; i++) { min= a[i]; min. Index = i; for (int j = i+1; j < a. length; j++) { if (a[j] < min) { min = a[j]; min. Index = j; } } a[min. Index] = a[i]; a[i] = min; } 3 i 7 6 j a m [j i ] n 0 1 2 3 4 7 6 4 5 3 3 4 5 min. Ind ex a[min. In dex] a [i ] 3 3 0 0

3 7 6 4 5 i j a[j] min 1 2 3 4 6

3 7 6 4 5 i j a[j] min 1 2 3 4 6 4 5 76 4 4 min. Index a[min. Index] a[i] 12 3 3 a[3]=a[1]=7 a[1]=min=4 3 4 6 7 5

Chapters covered • BPJ Lesson 8 • BPJ Lesson 10 • BPJ Lesson 12

Chapters covered • BPJ Lesson 8 • BPJ Lesson 10 • BPJ Lesson 12 • BPJ Lesson 18 • BPJ Lesson 19 • BPJ Lesson 20 • • BPJ Lesson 21 BPJ Lesson 34 BPJ Lesson 35 BPJ Lesson 40 BPJ Lesson 41 BPJ Lesson 43 BPJ Lesson 48 *Also the same topics in the Barron’s book

Tic. Tac. Toe application • Write a program to simulate the tic tac toe

Tic. Tac. Toe application • Write a program to simulate the tic tac toe game. The program should have the following functions. a) start. Over(): empty all the cells. b) make. Move(. . ): prompt user for a row and column # and mark the cell with ‘X’ or ‘O’. c) display. Board(): display the board with related marked ‘X’ or ‘O’. d) winner(): return the winner ‘X’ or ‘O’

Remarks • What is the control condition for the game to continue? • As

Remarks • What is the control condition for the game to continue? • As long as no winner yet(3 ‘X’ or ‘O’ in a row) • The board is not fully occupied. How to determined this? • Whenever there is a valid move, counter increments, the maximum moves=9 • What is a valid move? • Within row and column range • Is not occupied

[ ][ ][ ] enter row #: 0 enter column #: 0 [X][ ][

[ ][ ][ ] enter row #: 0 enter column #: 0 [X][ ][ ][ ] enter row #: 1 enter column # : 1 [X][ ][ ] [ O] [ ][ ][ ]

recursion Public void recur. Digit(int n) { if (n > 0) { recur. Digit(n

recursion Public void recur. Digit(int n) { if (n > 0) { recur. Digit(n -1); System. out. print(n); } } What’s recur. Digit(3)?

Rewrite using Array. List object ary. List int x = 76; => Integer x

Rewrite using Array. List object ary. List int x = 76; => Integer x = new Integer(76); ary[4] = x; => ary. List. add(x); int y = ary[22]; => ary. List. get(22); int s = ary. length; => ary. List. size();