Assignment 4: Game: pick a card. Write a program that simulates picking a card f
ID: 3753034 • Letter: A
Question
Assignment 4: Game: pick a card. Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card. Here is a sample run of the program: The card you picked is Jack of Hearts Note: I have uploaded two program examples to Canvas for your reference. Name: (5 points) Introduction: Write about the design of the code you will use to solve this problem. (5 points) References and assumptions: Where could we learn a successful solution? 5 points) Code: Please copy and paste your code here to asuime to find (5 points) Output: Please copy and paste the results from your code here. Remember, you might need to run your code a few times to show that your code works.Explanation / Answer
You have not mentioned the language. I assume its Java. Here is the completed code for this problem in JAVA. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Card.java
public class Card {
private String suit; // suit name
private int rank; // rank value (1-13)
// cosntructor taking suit and rank value
public Card(String suit, int rank) {
this.rank = rank;
this.suit = suit;
}
// getters and setters
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
String rankText = "";
// finding the rank, if the rank is 1, 11, 12 or 13, using proper rank
// names
if (rank == 1) {
rankText = "Ace";
} else if (rank == 11) {
rankText = "Jack";
} else if (rank == 12) {
rankText = "Queen";
} else if (rank == 13) {
rankText = "King";
} else {
rankText = rank + "";
}
return rankText + " of " + suit;
}
}
// PickRandomCard.java
import java.util.ArrayList;
public class PickRandomCard {
/**
* method to create a deck of cards, containing 4 suits of 13 cards each
*
* @return an array list of cards
*/
static ArrayList<Card> createDeck() {
ArrayList<Card> deck = new ArrayList<Card>();
// filling the array list with all 52 cards
for (int i = 1; i < 14; i++) {
Card card1 = new Card("HEARTS", i);
Card card2 = new Card("SPADES", i);
Card card3 = new Card("CLUBS", i);
Card card4 = new Card("DIAMONDS", i);
deck.add(card1);
deck.add(card2);
deck.add(card3);
deck.add(card4);
}
return deck;
}
/**
* method to simulate random picking of a card.
*
* @param deck
* deck of cards
* @return a Card, randomly chosen.
* @precondition the ArrayList is not null and not empty
*/
static Card pickRandom(ArrayList<Card> deck) {
int index = (int) (Math.random() * deck.size());
return deck.get(index);
}
public static void main(String[] args) {
//creating the deck
ArrayList<Card> deck = createDeck();
//picking a random card and displaying it
Card randomCard = pickRandom(deck);
System.out.println("The card you picked is " + randomCard);
}
}
/*OUTPUT (random)*/
The card you picked is Jack of CLUBS