Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please I need help with the assignment below. The assignment must be done in python 3. This programming assignment will look at using a class

Please I need help with the assignment below. The assignment must be done in python 3.

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 the 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 Class

The second part of your program is write a main function that will use the class you created in Part 1 to deal a random set of cards. 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

You will create a different PlayingCard object for each of the 52 playing cards listed above. 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 PlayingCard 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 list of indices using the following code:

cardListIndices = random.sample(range(0,52),n) 

Here, cardListIndices will be a list of numbers. Each number in cardListIndices gives an index into the deck list. Hint: You can use the cardListIndices in a for loop to print out the n cards.

Other Information

Your program must meet the following specifications:

Create the PlayingCard class and implement all methods (as described above).

Implement a main function to test the PlayingCard class. The main function must create a list of PlayingCard objects (i.e., create a deck of cards) and randomly deal n cards.

For the pseudocode, clearly indicate what code will be in the PlayingCard class methods and which code will be in the main function.

Document the program using Python comments.

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: 7 of diamonds 4 of spades 
>>> main() How many cards should I deal? 12 Your 12 cards are: 4 of hearts 4 of clubs king of diamonds ace of clubs 8 of spades 4 of diamonds 10 of clubs 2 of hearts 5 of hearts 8 of clubs 3 of diamonds 10 of diamonds 

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_2

Step: 3

blur-text-image_step3

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

Evaluate 3x - x for x = -2 Answer:

Answered: 1 week ago