1 / 18

Review

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

lindsay
Download Presentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review

  2. 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.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. 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 • 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

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

  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

  13. 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’

  14. 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

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

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

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

More Related