CS305J Introduction to Computing Discussion Section Week 12
Announcements No lab 11, no A Pair assignment has changed lab 11 counts for 10 out of 100 total Pair assignment has changed check at the discussion board Set game rules
Recap from Lab 10 Input racecar testing a end Output racecar is a palindrome testing is not a palindrome a is a palindrome
To-do List Repeat these three steps until “end” Read a word from file input Check if the word is palindrome or not Output to the terminal if the word is (not) a palindrome print the result read from file check palindrome stop if “end”
Methods Method 1 (nextWord) Method 2 (isPalindrome) parameter: Scanner file return: String word Method 2 (isPalindrome) parameter: String word return: boolean result Method 3 (printResult) parameter: boolean result, String word return: void read from file word check palindrome result print the result
Use Java API BlueJ->Help->Java Class Libraries nextWord = next() in Scanner class printResult = System.out.print(word) printResult nextWord isPalindrome stop if “end” Main method only contains the “logic” part. while loop stops at “end” calls other methods
Class Palindrome public static boolean isPalindrome(String word) return true if the word is a palindrome public static void main(String[]) while (word is not end) read word using next() call isPalindrome(word) print the result
Recap from TicTacToe Input moves Output for each move, 1,1 0,1 1,0 . Output for each move, print error if illegal move display the board announce the winner
Design with many classes What objects? (Classes) What to remember? (Instance Variables) How the classes interact? game instance variables: int turn, board b b.display() b.move(x, y, turn) board instance variable: int[][] board public void display() public boolean move(int x, int y, int turn)
Design each Class Classes: game, board Instance variables What to do in each class? (Methods) game receive user input for the move announce the winner game instance variables: int turn, board b b.display() b.move(x, y, turn) board instance variable: int[][] board public void display() public boolean move(int x, int y, int turn)
Class Game private int turn private board myBoard public static void main(String[]) initialize myBoard while (there is no winner) read (x,y) location for the next move myBoard.move(x, y, turn) myBoard.display() myBoard.isWin() change the turn read the move (x,y) move on the board true/false display board check winner false change the turn
Class Board private int[][] board private boolean winner public boolean move(int x, int y, int turn) return false if the move is not allowed set winner to be true if this is the winning move public void display() display the current board public boolean isWin() return true if instance variable winner is true
Connect Four Turn-in by 10pm today Cover page: names and discussion sections Input/Output Class Interaction Design Design for each class (instance variables, methods) Diagram for the logic in the main method Turn-in by 10pm today rtf, doc, ppt, ps, pdf hard copy in homework box in TAY