180 likes | 402 Views
Review. Loop structures while loop do-while loop 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; } Scope of j: only inside the loop break used to exit the loop immediately
E N D
Loop structures • while loop • do-while loop • 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; } • 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; } 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 < 5; j++) { for(int k =0; k < 8; k++) { sum += k; } } System.out.println(sum);
Quiz topics • Arrays class Arrays.sort(..) Arrays.binarySearch(…) • ArrayList class ArrayListObj.add(…) ArrayListObj.get(…) ArrayListObj.remove(…) ArrayListObj.set(…) ArrayListObj.size() • Wrapper classes Integer, Character, Boolean, Double • Recursion
What’s the difference between while and do-while 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 • Exception • Array run time errors • parameter passing to method • pass by value • pass by reference • enhanced for loop • Recursion • Iteration • infinite loop • nested loop • Overflow • Sentinel • debugging technique • variable trace
methods • Suppose there is method called calculateAvg, 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 xyz; public Nerd(){…} public double methodA(int x){…} public static void methodB(){..} } to access methodA=> Nerd nerdClass = new Nerd(); nerdClass.methodA; to access methodB=> Nerd.methodB();
int min, minIndex; for (int i = 0; i < a.length; i++) { min= a[i]; minIndex = i; for (int j = i+1; j < a.length; j++) { if (a[j] < min) { min = a[j]; minIndex = j; } } a[minIndex] = a[i]; a[i] = min; }
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
TicTacToe application • Write a program to simulate the tic tac toe game. The program should have the following functions. • startOver(): empty all the cells. • makeMove(..): prompt user for a row and column # and mark the cell with ‘X’ or ‘O’. • displayBoard(): display the board with related marked ‘X’ or ‘O’. • winner(): return the winner ‘X’ or ‘O’
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 #: 1 enter column # : 1 [ X ] [ ] [ ] [ ] [ O] [ ] [ ] [ ] [ ]
recursion Public void recurDigit(int n) { if (n > 0) { recurDigit(n -1); System.out.print(n); } } What’s recurDigit(3)?
Rewrite using ArrayList object aryList int x = 76; => Integer x = new Integer(76); ary[4] = x; => aryList.add(x); int y = ary[22]; => aryList.get(22); int s = ary.length; => aryList.size();