Download presentation
Presentation is loading. Please wait.
1
Review
2
Static vs non-static methods Two dimensional arrays
Loop structures while loop do-while loop for loop Methods Static vs non-static methods Two dimensional arrays Sorting – bubble sort, selection sort
3
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(…..)
4
int s=1; for(int j = 3; j >=0; j--) { s = s+ j; } System. out
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.
5
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);
6
Arrays.binarySearch(…)
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
7
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); }
8
terms Iteration infinite loop nested loop Overflow Sentinel
debugging technique variable trace Exception Array run time errors parameter passing to method pass by value pass by reference enhanced for loop Recursion
9
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()
10
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();
11
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; 3 7 6 4 5 i j a[j] min minIndex a[minIndex] a[i] 1 7 3 2 6 4 5
12
3 7 6 4 5 i j a[j] min minIndex a[minIndex] a[i] 1 2 6 7 6 1 2 3 4 5 a[3]=a[1]=7 a[1]=min=4 3 4 6 7 5
13
Chapters covered BPJ Lesson 8 BPJ Lesson 10 BPJ Lesson 12
*Also the same topics in the Barron’s book
14
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’
15
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
16
[ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ]
[ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ] enter row #: 1 enter column # : 1 [ ] [ O] [ ]
17
recursion Public void recurDigit(int n) { if (n > 0) { recurDigit(n -1); System.out.print(n); } What’s recurDigit(3)?
18
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();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.