1 / 10

Programming Fundamentals I Java Programming Spring 2007

Programming Fundamentals I Java Programming Spring 2007. XuanTung Hoang tung_hx@icu.ac.kr. Lecture No. 12. Encapsulation: Case Study. CardGame Project. Creating Reusable Packages. Package name. Your internet domain name. Add package declaration. package ice0124.lib ; class RNG { … …

benard
Download Presentation

Programming Fundamentals I Java Programming Spring 2007

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. Programming Fundamentals IJava ProgrammingSpring 2007 XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 12

  2. Encapsulation: Case Study CardGame Project

  3. Creating Reusable Packages Package name. Your internet domain name • Add package declaration package ice0124.lib; class RNG { … … } Main code Package name and the directory are matched • Compile the source code with –d switch • javac –d <path> RNG.java • This will create directories: <path>/ice0124/lib • And class files inside the directory above

  4. Using the package • Reuse classes in a package: • add import statement • Use fully qualified class name • Use javac with –classpath switch to specify the location of the package • javac –classpath <path_to_package> yourcls.java • To run a java main class (class with main method) also use -classpath • java -classpath <path_to_package>;<path_to_mainclass> yourcls

  5. CardGame project: Game rules • 4 players • Each player has 13 random cards from a deck of cards • Play in rounds. In each round, each player submits 1 of his card and compares to cards from other players. The winner get 1 point, other get 0 point. • Card comparison: • King > Queen > Jack > Ten > … > Two > Ace • Heart > Diamond > Spades > Clubs • For example: Ace of Heart (1oH) > Ace of Diamond (1oD), 2oC > AoH • At the end of the game, the winner is the player whose score is highest.

  6. CardGame: Analysis • List up all objects possibly existing in the game: • Card: Card class • Deck of cards: DeckOfCards class • Player: Player class • Game: GameControl class • Card class: represents a card • face and suit • DeckOfCards: represents a deck of cards • array of Card and related actions (deal and shuffle) • Player: represent player • Cards that a player has • Score that he gains • Submit a card to play in a round • GameControl: Represent a game • Game rules: card comparison, start a game and collaborate other objects according to a game rule

  7. CardGame: Analysist Card DeckOfCards + faceDesc: String[] + suitDesc: String[] - face: int - suit: int + toString(): String - cards: Card[] - currentCard: int + shuffling(): void + deal(): Card Player GameControl • - name: String • - cards: Card[] • - currentCard: int • score: int • + incScore(inc:int ): void • + submit(): Card - deck: DeckOfCards - players: Player[] + compare(c1:Card, c2: Card) + prepare(): void + play(): void

  8. GameControl • Constructor: • Create deck, and players • prepare() method: • shuffle the deck of cards • Deal cards to players • play() method: • while (game is not ended) • Ask each player to submit a card to play • Compare those cards to find biggest ones and the winner of this round • Increase score of the winner. • Display the winner

  9. GameControl: • play() method: • How to compare cards? • If card1.face > card2.face  card1 > card2 • If (card1.face == card2.face) and (card1.suit > card2.suit)  card1 > card2 • The order of elements in faceDesc and suitDesc matters • How to find the winner in one round? • winner = player 0 • For i from 0 to 3 • Compare winner’s card with player i’s card. • If player i's card > winner’s card  winner = player i • How to find the final winner when game ends? • winner = player 0 • For i from 0 to 3 • Compare winner’s score with player i’s score. • If player i's score > winner’s score  winner = player i

  10. What do you learn from this project? • How to use array • Scanning arrays with for loop • Array of string to store descriptions • Array index to compare strings • … • Group actions into methods • Object oriented programming is fun • Encapsulation is good • …. • …. • And the most important thing is … Make a plan before you actually write code!! Every line of code you write is for that plan!

More Related