Question
Read the following program description entirely before deciding to panic. You are to write the following program. You are to write a program in which
Read the following program description entirely before deciding to panic.
You are to write the following program.
You are to write a program in which the computer chooses a random card from a card deck and the user tries to guess the chosen card. The user will be given hints when his (or her) guess is incorrect.
A Card Deck
A Deck consists of 52 unique cards. The cards are divided into four suits, each with 13 cards. The suits are Clubs, Diamonds, Hearts and Spades. Each of a suit's cards has a rank, which can be either a number or a face. The numbers range from 2 to 10 (inclusive), and the faces are Jack, Queen, King and Ace. The order of the ranks is that a 2 is the lowest, and Ace the highest. The entire ordering for rank is
lowest-> 2 3 4 5 6 7 8 9 10 Jack Queen King Ace <-highest
Rank is used to determine whether one card is higher than another. If two cards have the same rank, then the suit determines the higher card. The lowest suit is Clubs, and the highest Spades. Only if both rank and suit are identical are two cards tied. The entire ordering of suits is
lowest-> Clubs Diamonds Hearts Spades <-highest
Some examples are
a 3 of any suit is higher than a 2 of any suit
a 2 of Spades is higher than a 2 of Clubs, Diamonds or Hearts
a Jack of any suit is higher than any numbered card of any suit
a Queen of any suit is higher than a Jack (or any numbered card) of any suit
a King of any suit is higher than a Queen of any suit
an Ace of any suit is higher than a King of any suit
a King of Hearts is higher than a King of Clubs or Diamonds
For two cards to be equal requires that both the rank and the suit match.
Representing a card in your program
A card will be represented by two single-character Strings. One String will represent the suit, and will have a value
"c" "d" "h" or "s" (for clubs, diamonds, hearts or spades, respectively).
The other String will represent rank, and have a value of
"2" "3" "4" "5" "6" "7" "8" "9" "t" "j" "q" "k" or "a" (for 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace, respectively).
Your program
You are to write a program, GuessCard.
Inside the GuessCard class, you are to have a static method
public static String randomSuit()
that returns a randomly chosen single-character String representing a card suit, as described above.
Inside the GuessCard class, you are to have a static method
public static String randomRank()
that returns a randomly chosen single-character String representing a card rank, as described above.
Inside the GuessCard class, you are to have a static method
public static int compareSuit(String suit1, String suit2)
suit1 and suit 2 are single character Strings that represent card suits, as described above.
This method returns
an int value < 0 if suit1 is less than suit2. The actual int value doesn't matter, so long as it is < 0.
an int value > 0 if suit1 is greater than suit2. The actual int value doesn't matter, so long as it is > 0.
0 if suit1 and suit2 are equal.
Inside the GuessCard class, you are to have a static method
public static int compareRank(String rank1, String rank2)
rank1 and rank2 are single character Strings that represent card ranks, as described above.
This method returns
an int value < 0 if rank1 is less than rank2 . The actual int value doesn't matter, so long as it is < 0.
an int value > 0 if rank1 is greater than rank2 . The actual int value doesn't matter, so long as it is > 0.
a 0 if rank1 and rank2 are equal.
Inside the GuessCard class, you are to have a main method that implements the card guessing game. The main method should
implement a sentinel controlled loop to ask the user if he wishes to play a game.
use randomRank and randomSuit to choose a card
do all user input/output.
prompt the user to enter a card using two characters separated by whitespace (e.g., a character).
use compareRank to check whether the ranks of the randomly chosen card and the user's guess match. If they do not, display a hint that the user should try a higher or lower rank.
if the ranks match, use compareSuit to check whether the suits of the randomly chosen card and the user's guess match. If they do not, display a hint that the user should try a higher or lower suit.
if the cards are equal, display a message and ask the user if he wishes to play another game.
The following is a typical execution. User input is in bold.
Do you want to guess a card (1 for yes, 2 for no) 1 Enter rank followed by suit, where rank is 2, 3, ... 9, t, j, q, k, a and suit is c, d, h, s 4 d is the 4 of diamonds k c is the king of clubs t s is the 10 of spades What's your guess to find the card? 9 d guess a lower rank What's your guess to find the card? 6 d guess a lower rank What's your guess to find the card? 4 d guess a higher rank What's your guess to find the card? 5 d guess a higher suit What's your guess to find the card? 5 h Congratulations, you got it! Do you want to guess a card (1 for yes, 2 for no) 1 What's your guess to find the card? 9 h guess a lower rank What's your guess to find the card? 5 h guess a higher rank What's your guess to find the card? 7 h guess a higher rank What's your guess to find the card? 8 h guess a lower suit What's your guess to find the card? 8 c guess a higher suit What's your guess to find the card? 8 d Congratulations, you got it! Do you want to guess a card (1 for yes, 2 for no) 2
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