Question
This programming assignment will look at using a class you design yourself. You will create a playing card class and use that class to create
This programming assignment will look at using a class you design yourself. You will create a playing card class and use that class to create a deck of cards. There are two parts to this programming project. Both parts should be submitted together as one file.
Part 1 - Defining a Playing Card Class
Implement a class to represent a playing card. Your class should have the following methods:
- init (self, rank, suit)
rank is a string ("2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace") indicating the rank of the card, and suit is a string ("spades", "hearts", "diamonds", "clubs") indicating the suit of the card. This constructor should create a card corresponding to the rank and suit given.
- setRank(self, rank)
Sets the rank of the card (mutator method).
- setSuit(self, suit)
Sets the suit of the card (mutator method).
- getRank(self)
Returns the rank of the card (accessor method).
getSuit(self)
Returns the suit of the card (accessor method).
_ _str_ _ (self)
Returns a string that names the card. For example, "ace of spades" or "9 of clubs."
Note: The method named _ _str_ _ is special in Python. If asked to convert an object into a string Python uses this method, if it is present.
Part 2 - Using the Playing Card Class
The second part of your program is to actually use the class you created in Part 1 to deal a random set of cards in a main() function. For this program, you must create a deck of cards (i.e. create a list of 52 playing card objects). The cards in your deck/list will be the following cards:
Spades: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace Hearts: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace
Diamonds: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace
Clubs: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace
Then, you will randomly select and display n cards from the deck of cards, where n is a number provided by the user. Hint: To easily create the deck/list of playing card objects you can use a nested for loop (one loop will control the suit and one loop will control the rank).
A Helpful Function
In order to deal n cards, we need to pick n cards at random. The deck of cards will be stored in a list - if we can pick n indices at random that will give us the n cards. The random library provides us with a perfect function for choosing the n cards at random.
import random random.sample(L,n)
This function returns a list containing n randomly selected items from the list L. In our case, we want n numbers between 0 and 51 (thinking of the indices for our deck of cards list). That means we can create our card list using the following code:
cardList = random.sample(range(0,52),n) Here, cardList will be a list of numbers. Each number in cardList gives an index into the deck
list. Hint: You can use the cardList in a for loop to print out the n cards. Another Helpful Function - Not Required/Bonus
If you would like an added challenge, sort the cards by suit in the order: spades, hearts, diamonds, clubs. In order to sort the list, we can use the following code:
cardList = sorted(cardList) The sorted function will order the list in numerical order. We can use this sorted list to determine
the cards we should print.
Other Information
Here are a couple of sample runs of the program. Due to the randomness of the program, your output will be different:
>>> main() How many cards should I deal? 2 Your 2 cards are: ace of spades 3 of clubs
>>> main() How many cards should I deal? 10 Your 10 cards are: 2 of spades 7 of spades jack of spades 8 of hearts 3 of diamonds 6 of diamonds 8 of diamonds 5 of clubs 10 of clubs jack of clubs
Here are some of the things I will be looking for when I grade your assignment:
For your pseudocode be sure to describe what instructions are part of your card class and what instructions are part of your main function. Describe what each card class method will do.
Did you use comments to describe what your code is doing? This is a good programming practice and also helps me follow your logic.
Did you use variable names that are descriptive? This is a good programming practice I would like you to follow.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started