Programming Task - Pick a Card
This problem will give you practice in using a range of variable types and the arithmetic operations you can perform on them.
The problem:
Write a program that will display the rank and suit of a card chosen randomly from a standard pack.
You are provided with an incomplete Card Game program (code below).
It already includes the statement to generate a random number and some incomplete code to output the rank and suit for the card.
Your tasks are to:
- add in the operations to generate the rank and suit of the card (this is the part that uses \(\%\) and \(/\))
- complete the selection (
if
andcase
) statements (you can read about these in the next chapter) - test your algorithm by running the program, revising the algorithm where necessary
Extension Tasks
(These require the use of loops and are optional. You can find out how to use loops by looking further ahead in the text)
- Add code to enable a user to "Pick a Card", add exception handling to this block of your program
- Extend the program so that it repeatedly chooses a random card until the user enters 'q'
- Extend the program so that it shows every card in a standard pack in order from the two of clubs to the ace of spades.
Tip
HINT: Adding these extensions to the same program will produce a lot of repetition. Either, read ahead and look up how to implement a method, or use three separate projects
- Look up the Unicode representation for suits of cards and adapt your program to print those to the console rather than the word "Spades" or "Hearts" etc..