Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal: Implement and test the Deck class as shown in the Card/Deck UML Diagram. Relevant Example: SimpleDeck Details: test1.py tests the Deck class using traditional

image text in transcribed

  • Goal: Implement and test the Deck class as shown in the Card/Deck UML Diagram.
  • Relevant Example: SimpleDeck
  • Details:
    1. test1.py tests the Deck class using traditional tests. Test these methods:
      Deck (constructor) deal add_to_bottom add_to_top count is_empty shuffle __str__ 
    2. Double check the UML diagram to see if the parameters that you are passing in to each method is correct.
    3. The bottom of the deck is index 0; the top of the deck is the maximum index of cards in the deck. For example, for a standard 52 card deck, the top of the deck has index 51. The cards in a new, unshuffled deck are ordered like this, bottom to top:
      2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS 
    4. Here are three things that you may want to do in your implementation of the Deck class. They use the list methods append, pop, insert, and the shufflemethod from the random module.
      1. Add a new card c to the top of the deck:
        self.cards.append(c) 
        Create a new card with the specified rank and suit and append it to the cards in the deck:
        c = Card(rank, suit) self.cards.append(c) # or, shorter, self.cards.append(Card(rank, suit)) 
      2. Remove the card from the top of the cards list and save it as the Card object c:
        c = the.cards.pop( ) 
      3. Insert the card c at the bottom of the deck (before the item with index 0):
        self.cards.insert(0, c) 
        When a card is inserted before index 0 or in the middle of a list, the index of each card that is greater than the insertion point is pushed up by 1 to make room for the insertion.
      4. Shuffle the Card objects in the deck:
        import random random.shuffle(self.cards) 
    5. Use a double loop in the __init__ method to add the cards to the deck:
      self.cards = [ ] for rank in range(2..15): for suit in ['C', 'D', 'H', 'S'] # create a new card with the specified rank # and suit and append it to the list. self.cards.append(Card(rank, suit)) 
    6. Use a single loop to show all the cards in the Deck __str__ method:
      output = "" for card in self.cards: # Concatenate card to # the output variable return output
      Use the list append method to concatenate all the cards to the output string. Get the string representation of this card before appending it. Don't forget to put spaces between the cards
    7. .
    8. Don't print anything in the __str__ method.
    9. Sometimes, double spacing your output improves readability. To double space, you can do this:
      print(d.count( )) print( ) 
      or this:
      print(d.count( ), " ") 
    10. The number of parameters you pass in to the constructor call (in this case Card) must be one less than the number of parameters of the __init__ method for that class. In the case of the Card class,
      c = Card(rank, suit) 
      has two input arguments because the header for __init__ also has three parameters:
      def __init__(self, the_rank, the_suit) 
  • In your traditional test file test1.py, test all of the Deck methods (there are no public instance variables to test):
    Deck pop add_to_bottom add_to_top count is_empty shuffle 
  • To test the __init__ and __str__ methods of the Deck class, use these lines:
    d = Deck( ) print(d, " ") 
    (The Deck method __init__ does not have any parameters, so the constructor Deck should not have any parameters either.) Test the constructor and the str methods before writing and testing any other methods.
  • The Deck methods add_to_top, add_to_bottom, and shuffle do not have return values, so you can't print them. For example, for add_to_top:
    # Don't do this: d = Deck( ) c = Card(12, "S") print(d.add_to_top(c)) # Do this: d = Deck( ) c = Card(12, "S") d.add_to_top(c) print(d) 
  • test2.py tests the Deck class using unit tests. Some of the Deck methods are difficult to test with unit tests, for example, __str__ because its return value is very long. Also, the shuffle method cannot be tested with a unit test because its action is random and cannot be tested with a a unit test. Only test these Deck methods:
    Deck (constructor) deal add_to_bottom add_to_top count is_empty 
    For example, you can use unit test statements to test the deal method like this:
    d = Deck( ) c = d.deal( ) self.assertEqual(str(c), "AS") self.assertEqual(d.count( ), 51) print(d.count( )) 
  • Inheritance is not used for Project 3, for the first line of the Deck class:
    # Incorrect: class Deck(Card): # Correct: class Deck:
Card + rank: int suit: str init (self: Card, rank: int, suit: str) + color(self: Card): str str(self str Deck -_cards: Cardl] init-self: Deck) _str_(sel str + 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 + shuffle(self: Deck)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

i need correct answrrs 1 0 2 .

Answered: 1 week ago