1 / 13

CS305J Introduction to Computing

This discussion section covers the announcements, rules for a pair assignment, designing a palindrome checker, and designing a Tic Tac Toe game.

riverag
Download Presentation

CS305J Introduction to Computing

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. CS305J Introduction to Computing Discussion Section Week 12

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

  3. Recap from Lab 10 Input racecar testing a end Output racecar is a palindrome testing is not a palindrome a is a palindrome

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

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

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

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

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

  9. 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?

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

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

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

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

More Related