130 likes | 140 Views
This discussion section covers the announcements, rules for a pair assignment, designing a palindrome checker, and designing a Tic Tac Toe game.
E N D
CS305J Introduction to Computing Discussion Section Week 12
Announcements • No lab 11, no A • 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 read from file stop if “end” check palindrome print the result • 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
Methods • Method 1 (nextWord) • 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 nextWord stop if “end” isPalindrome printResult • BlueJ->Help->Java Class Libraries • nextWord = next() in Scanner class • printResult = System.out.print(word) • 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 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 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) • What objects? (Classes) • What to remember? (Instance Variables) • How the classes interact?
Design each Class 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) • Classes: game, board • Instance variables • What to do in each class? (Methods) • game • receive user input for the move • announce the winner
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 • 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