Review

18
Review

description

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 - PowerPoint PPT Presentation

Transcript of Review

Page 1: Review

Review

Page 2: Review

• Loop structures1) while loop2) do-while loop3) for loop

• Methods• Static vs non-static methods• Two dimensional arrays• Sorting – bubble sort, selection sort

Page 3: Review

for loop

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

1)Scope of j: only inside the loop2)break used to exit the loop immediately3)Braces4)No ; immediately after the for(…..)

Page 4: Review

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.

Page 5: Review

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

Page 6: Review

• Arrays classArrays.sort(..)Arrays.binarySearch(…)

• ArrayList classArrayListObj.add(…)ArrayListObj.get(…)ArrayListObj.remove(…)ArrayListObj.set(…)ArrayListObj.size()

• Wrapper classesInteger, Character, Boolean, Double

• Recursion

Page 7: Review

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

}

Page 8: Review

• 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

Page 9: Review

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

Page 10: Review

Static vs non-static methodspublic 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();

Page 11: Review

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

i j a[j]

min

minIndex

a[minIndex] a

[i]

0 1 7 3 02 6 3 03 4 3 04 5 3 0

3 3

3 7 6 4 5

Page 12: Review

i j a[j] min minIndex a[minIndex] a[i]

1 2 6 7 6 1 23 4 4 34 5 4 3

a[3]=a[1]=7 a[1]=min=4

3 7 6 4 5

3 4 6 7 5

Page 13: Review

Chapters covered• BPJ Lesson 21• BPJ Lesson 34• BPJ Lesson 35• BPJ Lesson 40• BPJ Lesson 41• BPJ Lesson 43• BPJ Lesson 48

• BPJ Lesson 8• BPJ Lesson 10• BPJ Lesson 12• BPJ Lesson 18• BPJ Lesson 19• BPJ Lesson 20*Also the same topics in the Barron’s book

Page 14: Review

TicTacToe application• Write a program to simulate the tic tac toe game.

The program should have the following functions.a)startOver(): empty all the cells.b)makeMove(..): prompt user for a row and column

# and mark the cell with ‘X’ or ‘O’.c) displayBoard(): display the board with related

marked ‘X’ or ‘O’.d)winner(): return the winner ‘X’ or ‘O’

Page 15: Review

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

Page 16: Review

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

[ ] [ ] [ ]

Page 17: Review

recursion

Public void recurDigit(int n){ if (n > 0)

{ recurDigit(n -1);System.out.print(n);}

}

What’s recurDigit(3)?

Page 18: Review

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