# java代写/算法代写/数据结构代写/UML代写: Inheritance and Polymorphism CardGame Application

## Inheritance and Polymorphism CardGame Application

java代写/算法代写/数据结构代写/UML代写: 这是一个典型的通过java编写一个Blackjack游戏的代写，是对数据结构/算法/oop编程/java编程的综合性考察

In this project, you will design a class hierarchy to model a card game application. In this card
game, a player is playing the Game of 21 against the computer. The player gets some choice of
moves, but the computer’s moves are based on current standings in its hand when compared
to the player’s hand. Here are the rules for the Game of 21:
Game of 21
https://en.wikipedia.org/wiki/Blackjack
The objective of the game is get the closest to 21 without going over. The player and the
computer are dealt 2 cards. The player is allowed to draw additional cards to improve his
hand, without going over 21. The computer can then choose to draw cards; though the
computer will not draw if the player has busted.
Face cards have a value of 10. Aces can be high or low (that is, 11 points or 1 point)
depending on which is the best value which achieves the closest score to 21 without
going over.
After cards have been drawn by the player and the computer, the score is totaled and a
winner is declared.
The CardGame application includes the following classes:
 Suit, an enum class to store the suits of a playing card
 Card, an object class for a playing card
 CardSet, an abstract superclass, which creates a deck of Card objects or a hand of Card
objects
 Deck, a subclass of CardSet, which populates and shuffles a deck of cards and deals cards
 Hand, a subclass of CardSet, which holds an empty hand of cards, then draws cards,
finds the best value for an Ace, totals a hand
 GameOf21, a class which uses a Deck object and Hand objects
 GameOf21App, a tester class which allows a user to play the Game of 21
See the UML diagram for the classes on the next page. Then, carefully read the program
specifications that follow before you begin coding.
UML for CardGame Application:
Hand
+ Hand(userName : String) {create a hand,
+ hit(c : Card) : void
+ getCardValue(c:Card) : int
+ totalHand() : int
+ totalVisibleHand(): int
+ hasBlackJack(): boolean
+ emptyHand() : void
+ displayCards():void
+ displayAllCards():void
+ displayVisibleCards():void
Suit
Deck
+ SIZE = 52 : int
+ Deck() {create, populate, shuffle a deck}
+ populateDeck() : void
+ shuffle() : void
+ draw() : Card
+ size() : int
+ displayCards(start:int):void
+displayAllCards():void
+displayVisibleCards():void
GameOf21
– deck : Deck
– user : Hand
– computer : Hand
– in : Scanner
+ GameOf21(player : String)
{initialize fields}
+ getters for all fields
+ setters for all fields
+ playGame() : void
+ playHand(): void
+ playAgain() : void
+ userDraws() : void
+ computerDraws() : void
+ printWinner() : void
CardSet
# cards : ArrayList
+ CardSet() {initialize cards to default size}
+ CardSet(numberOfCards : int) {initialize cards to numberOfCards size}
+ displayAllCards() : void
+displayVisibleCards():void
Card
– suit : Suit
– rank : int
+ Card() {initialize suit to CLUBS and rank to 1}
+ Card(suit : Suit) {initialize rank to 1}
+ Card(suit : Suit, rank : int)
+ getSuit() : Suit
+ getRank() : int
+ setSuit(suit : Suit) : void
+ setRank(rank : int) : void
+ findFaceValue() : String
+ toString() : String
GameOf21App
+ main()
+printTitle()
+printInstructions()
Program Specifications:
1. Suit is an enum class
 It stores a list of enum constants for Card suits
2. Card is an aggregate class for CardSet
 The findFaceValue() method checks the rank of this card, and returns one of the following face values:
o {“Ace”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”, “Queen”, “King”}
3. CardSet is an abstract superclass
 The abstract methods displayAllCards() will be implemented in CardSet’s subclasses and displays the number of Card objects
in the deck or a hand and the String value of the Card objects.
 The abstract method displayVisibleCards() shows the computer’s visible cards but not the computer’s ‘hole’ card, which was
the first card dealt to the computer.

4. Deck is a subclass of CardSet and an aggregate class for GameOf21 class
 The populateDeck() method loads an ArrayList deck with 52 Card objects made up of 4 suits, each with 13 ranks, that
is, ranks from 1 to 13.
 The shuffle() method shuffles the ArrayList deck in random order.
 The draw() method returns and removes the “top” Card object from the deck, that is, the Card at Index 0.
 The size() method returns the size of the deck, or in other words how many cards are left in the deck as the game is played.
 The displayCards(int start) method accepts an int argument of either a 0 (zero), which means show all the cards, or a 1,
which means show all the cards EXCEPT the card at element 0.
 To implement the abstract methods inherited from the abstract class CardSet, have the implemented versions make a call to
displayCards(int start) and pass in either a 0 or a 1 depending on what you want to display.
5. Hand is a subclass of CardSet and an aggregate class for GameOf21 class
 The hit(Card c) method adds a Card object to a Player’s hand, which is dealt from the CardGame class deck.
 The getCardValue() method returns an int based on the card’s rank. For any face card, the int value returned will be 10.
 The totalHand() method() finds the total value of a Player’s hand based on the CardSet’s values. This method also checks if a
Card is an Ace and, if it is, determines if the Ace should be counted as a 1 or an 11. If having the Ace count as 11 makes the
total go over 21, then the Ace will be counted as a 1.
 The totalVisibleHand() method returns the total of the cards that are visible. It ignores the first card that is face down.
 The hasBlackJack() method returns a Boolean true only if the hand totals 21 and the size of the hand is 2 cards. In other
words, if the hand has an Ace and a ten, or an Ace and any face card, this method should return a true.
 The emptyHand() method empties a Player’s hand of cards.
 The displayCards(int start) method accepts an int argument of either a 0 (zero), which means show all the cards, or a 1,
which means show all the cards EXCEPT the card at element 0.
 To implement the abstract methods inherited from the abstract class CardSet, have the implemented versions make a call to
displayCards(int start) and pass in either a 0 or a 1 depending on what you want to display.
6. GameOf21 class
 The playGame() method displays the deck’s current size. Then, playGame() method calls the playHand() method, which
deals two cards alternately to the player and to the computer by having the player and the computer get cards from the
deck by calling their respective draws() methods. Then, playGame() displays the player’s hand by calling displayAllCards(),
and then displays the computer cards, although it hides the computer’s ‘hole’ card by calling displayVisibleCards().
 The user is allowed to draw additional cards to improve his hand, without going over 21. The userDraws() method
repeatedly asks the user if he wants to draw another card and, if he does, draws another card; otherwise the user stands.
 The computer can then choose to draw cards. The computer will not draw if the player has busted; in this case, the
computer stands. If the computer has a total of less than 17, then it must draw again. See the sample output to determine
the cases for when the computer stands!
 The printWinner() method checks the total points of each player. The player with the points closest to 21 without going
over is the winner. A tie score mean the computer wins.
 The playAgain() method prompts the user to play again, gets the user’s response to play again and, if yes AND the deck still
has enough cards to deal again (deck must have a minimum of 6 cards to play again, any less and the game is over), empties
the Players’ hands and plays the game again; otherwise prints an exit message.
To Play the CardGame Application use GameOf21App.java
1. Print a message to the screen, and get the user’s name.
2. Call the printTitle() method, which prints the title of the Game of 21.
3. Call the printInstructions() method which prints the instructions for the Game of 21.
4. Then create a GameOf21 object.
5. Begin the card game, by calling playGame().
See the Sample Output for the Game of 21 below and on the following PAGES
——————————————
This application allows a player to play
the Game of 21 against the computer.
——————————————
JAMES, Good Luck!
——————————————
Welcome to the Game of 21!
——————————————
HOW TO PLAY:
——————————————
…… Game instructions go here………..
Deck contains 52 cards.
***********************JAMES*******************
1)King of CLUBS
2)Five of CLUBS
***********************Computer*******************
1)Four of DIAMONDS
Do you want another card (Y/N)?N
***********************Computer*******************
1)Nine of CLUBS
2)Four of DIAMONDS
3)King of DIAMONDS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 15
Computer points: 23
——————————–
Computer Busts
Do you want to play again (Y/N)? Y
Deck contains 47 cards.
***********************JAMES*******************
1)Seven of DIAMONDS
***********************Computer*******************
1)Eight of CLUBS
Do you want another card (Y/N)?N
***********************Computer*******************
1)Two of HEARTS
2)Eight of CLUBS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 17
Computer points: 20
——————————–
Computer Wins!
Do you want to play again (Y/N)? Y
Deck contains 42 cards.
***********************JAMES*******************
1)Eight of HEARTS
2)King of HEARTS
***********************Computer*******************
1)Nine of HEARTS
Do you want another card (Y/N)?N
***********************Computer*******************
1)Six of CLUBS
2)Nine of HEARTS
4)Queen of CLUBS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 18
Computer points: 26
——————————–
Computer Busts
Do you want to play again (Y/N)? Y
Deck contains 36 cards.
***********************JAMES*******************
2)Jack of CLUBS
***********************Computer*******************
1)Four of HEARTS
Do you want another card (Y/N)?Y
***********************JAMES*******************
2)Jack of CLUBS
3)Three of CLUBS
Do you want another card (Y/N)?N
***********************Computer*******************
2)Four of HEARTS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 15
Computer points: 23
——————————–
Computer Busts
Do you want to play again (Y/N)? Y
Deck contains 29 cards.
***********************JAMES*******************
1)Nine of DIAMONDS
2)Ace of HEARTS
***********************Computer*******************
1)Ace of DIAMONDS
Do you want another card (Y/N)?N
***********************Computer*******************
2)Ace of DIAMONDS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 20
Computer points: 19
——————————–
JAMES Wins!
Do you want to play again (Y/N)? Y
Deck contains 25 cards.
***********************JAMES*******************
1)Six of HEARTS
2)Ten of CLUBS
***********************Computer*******************
Do you want another card (Y/N)?N
***********************Computer*******************
1)Ten of DIAMONDS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 16
Computer points: 20
——————————–
Computer Wins!
Do you want to play again (Y/N)? Y
Deck contains 20 cards.
***********************JAMES*******************
1)Seven of HEARTS
2)Three of HEARTS
***********************Computer*******************
1)Jack of HEARTS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 10
Computer points: 21
——————————–
***********************Computer*******************
1)Ace of CLUBS
2)Jack of HEARTS
Computer Wins with a Blackjack!
Do you want to play again (Y/N)? Y
Deck contains 16 cards.
***********************JAMES*******************
1)Five of HEARTS
2)Six of DIAMONDS
***********************Computer*******************
Do you want another card (Y/N)?Y
***********************JAMES*******************
1)Five of HEARTS
2)Six of DIAMONDS
3)Two of DIAMONDS
Do you want another card (Y/N)?Y
***********************JAMES*******************
1)Five of HEARTS
2)Six of DIAMONDS
3)Two of DIAMONDS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 23
Computer points: 9
——————————–
Player Busts
Do you want to play again (Y/N)? Y
Deck contains 10 cards.
***********************JAMES*******************
1)Seven of CLUBS
2)Jack of DIAMONDS
***********************Computer*******************
Do you want another card (Y/N)?N
***********************Computer*******************
1)Two of CLUBS
3)Queen of DIAMONDS
——————————–
Game of 21 – Final Score
——————————–
JAMES points: 17
Computer points: 22
——————————–
Computer Busts
The Deck has 5 cards. Thank you for playing. Bye.
CardGame Application – Marking Scheme

Description Availabl
e
Awarde
d
Overall Coding Style – Each class includes:
 Javadoc style documentation for all methods
 Correct use of access modifiers for fields, methods, and constructors, as shown on
the UML
 Correct use of indenting and white space throughout classes /5
Card class
 Fields are declared with appropriate types and modifiers
 Three constructors are present and each correctly initializes Card fields
 Getters and setters are present for all fields
 Method header correct and method body correctly implemented for all methods:
o /10
CardSet abstract class
 Field is declared with appropriate type and modifiers
 Two constructors are present and each correctly initializes CardSet’ field
 Abstract method header is present and correctly declared /5
Deck class, a subclass of CardSet
 Field is declared with appropriate type and modifiers
 One constructor is present and each correctly initializes a deck
 Method header correct and method body correctly implemented for all methods:
o /10
Hand class, a subclass of CardSet
 Field is declared with appropriate type and modifiers
 One constructor is present and each correctly initializes a hand
 Getter and setter are present, as specified
 Method header correct and method body correctly implemented for all methods:
o
/10
GameOf21 class, a subclass of CardGame
 Fields are declared with appropriate types and modifiers
 One constructor is present and each correctly initializes GameOf21 fields
 Getters and setters are present for all fields
 Abstract methods are correctly implemented for the GameOf21:
o
/10
GameOf21App class
 Prints a message to the screen, and get the user’s name
 Calls the printTitle() and printInstructions() methods
 Creates a GameOf21 object
 Begins the card game, by calling playGame()
/5
CardGame Application Overall
 Project runs successfully (no compiler errors, no compiler warnings, no exceptions) /10
Total Marks /65