Question: Goal: Implement and test the Card class as shown in the Card/Deck UML Diagram. Relevant Examples: Person Pet Details: The ranks of the cards from
-
Goal: Implement and test the Card class as shown in the Card/Deck UML Diagram. - Relevant Examples: Person Pet
- Details:
- The ranks of the cards from lowest to highest:
2 3 4 5 6 7 8 9 10 11 (Jack) 12 (Queen) 13 (King) 14 (Ace)
- The order of the suits from lowest to highest:
"C" (Club) "D" (Diamond) "H" (Heart) "S" (Spade)
- The color method returns "black" if the suit is "C" or "S"; it returns "red" otherwise (if the suit is "D" or "H").
- In the __str__ method, define a list named symbols that contains the possible symbols for each index, specified by the rank instance variable.
symbols = ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
Here are the indices (ranks) and values of the elements in the symbols list:
The values for the indices 0 and 1 are empty strings ("") because no card has rank 0 or 1. You can use this __str__ method for the Card class:Indices: 0 1 2 3 4 5 6 7 Values: "" "" "2" "3" "4" "5" "6" "7" Indices: 8 9 10 11 12 13 14 Values: "8" "9" "10" "J" "Q" "K" "A" def __str__(self): symbols = ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] return symbols[self.rank] + self.suit
- The output of the statement
c = Card(11, "S") print(c)
is JS, where "J" is obtained from the symbols list and "S" is the suit. - The name of the file that implements the Card class is card.py.
- Submit two test scripts test1.py and test2.py.
- test1.py tests the Card class in the traditional way using print statements. Test both instance variables (rank and suit) and the methods (Card, suit, __str__). Print a Card object c1 like this
print(c1)
to test the __str__ method. - test2.py tests the Card class using unit tests. Test the rank and suit instance variables; also test the color method. Finally test the __str__ method like this:
c1 = Card(12, "S") self.assertEqual(str(c1), "QS")
- test1.py tests the Card class in the traditional way using print statements. Test both instance variables (rank and suit) and the methods (Card, suit, __str__). Print a Card object c1 like this
- The ranks of the cards from lowest to highest:
Card rank: int suit: str init (sel ank: int, suit: str) color(self: Card: str -str (sel) . Str Deck _cards Cardll _init_(self: Deck) str (sel:st deal self Deck) Card add_to_top(self: Deck, c Card) +add to_bottom(self: Deck, c: Card) +count(self: Deck): int is_empty(self: Deck):bool +shufme self: Deck)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
