Question
2 Class Card This class models a playing card. Recall that a playing card has one of the following four suits: HEART, SPADE, CLUB, and
2 Class Card
This class models a playing card. Recall that a playing card has one of the following four suits:
HEART, SPADE, CLUB, and DIAMOND. Each suit has 13 cards
A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K. We often represent A as 1, J as 11, Q as 12, and K as 13. Read the code of the classes and Card and try to understand the functionality of the class.
Create a class named TestCard with main method. Create Card objects to represent each of the following: HEART 2, SPADE 4, CLUB Q, and DIAMOND A. A Card object to represent HEART 2 can be done by
Card c1 = new Card("HEART", 2);
Using System.out.println to print each of the objects. For example System.out.println(c1);
3 Class Deck
You job is to create a class to represent a deck of playing cards. Recall that a deck has 52 distinct Cards. Thus this class will have an instance variable deck which is an array of type Card
In this class write a default constructor. The constructor should initialize the array deck so that this array has all 52 distinct cards. Partial code for the constructor is given below
public Deck() {
deck = new Card[52];
for(int i = 0; i<52; i++) {
if (i / 13 == 0) {
deck[i] = new Card("SPADE", i % 13 +1);
//WRITE REST OF THE CODE HERE
}
}
Your class must have following methods.
getCard(int i): return the ith card of deck. Note that the type of this method is Card.
getHand(): Returns a Card array of size 5, consisting of first 5 cards of deck.
shuffle(): The purpose of this method is to shuffle the deck. Shuffling can be done as follows:
For i in range 0 to 51 do the following: Randomly pick an integer named pos among between 0 and 51 (including 0 and 51). Exchange Card at index i (of array deck) with Card at index pos (of array deck). You can generate a random integer as follows. First import
java.util.*
Random r = new Random();
int pos = r.nextInt(52);
Once you finish writing the class Deck, run the program CardPanel. What happens? Now look at the code of CardPanel. In the method createAllImage, uncomment the line example.shuffle() and run the program again. What happens? Run the program multiple times.
4 Check Point
You should have all four classes (Card, Deck, CardPanel, and TestCard).
------------------------------------------------------------------------------------------------------
Card.java:
/**
* A class to represent a playing card by taking in a Suit and Number to
* represent it (Ace - 1, Jacks - 11, Queens - 12, King - 13)
*
* @author bruckna
*
*/
public class Card {
// Associative table for the GUI to identify the card
private static final String[] identifier = {
// Spade
"SA", "S2", "S3", "S4", "S5", "S6", "S7",
"S8", "S9", "ST", "SJ", "SQ", "SK",
// Heart
"HA", "H2", "H3", "H4", "H5", "H6", "H7",
"H8", "H9", "HT", "HJ", "HQ", "HK",
// Club
"CA", "C2", "C3", "C4", "C5", "C6", "C7",
"C8", "C9", "CT", "CJ", "CQ", "CK",
// Diamond
"DA", "D2", "D3", "D4", "D5", "D6", "D7",
"D8", "D9", "DT", "DJ", "DQ", "DK" };
private String id;
public Card(String suit, int num) {
int identity = 0;
if (suit.toUpperCase().equals("HEART")) {
identity = 13;
} else if (suit.toUpperCase().equals("CLUB")) {
identity = 26;
} else if (suit.toUpperCase().equals("DIAMOND")) {
identity = 39;
}
id = identifier[identity + num];
}
@Override
public String toString() {
return id;
}
}
----------------------------------------------------------
CardPanel.java:
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Font;
@SuppressWarnings("serial")
public class CardPanel extends javax.swing.JPanel {
private BufferedImage[] cardImage = createAllImage();
public static CardPanel getInstance() {
CardPanel panel = new CardPanel();
panel.setBackground(Color.BLUE);
panel.setPreferredSize(new java.awt.Dimension(660, 180));
return panel;
}
public BufferedImage[] createAllImage() {
/*******************************************************************
************************
* Here is where you can test your code. Currently, it
creates a Deck object and gets a
* "Hand" of Card objects to display. Once you can
display the first 5 cards of the Deck,
* remove the comment // on the shuffleDeck line to test
your shuffle method.
**************************************************************************
*****************/
Deck example = new Deck();
//example.shuffleDeck();
Card[] hand = example.getHand();
BufferedImage[] imageArray = new
BufferedImage[hand.length];
for (int i = 0; i < imageArray.length; i++) {
imageArray[i] =
createImage(hand[i].toString());
}
return imageArray;
}
public BufferedImage createImage(String card) {
int cardWidth = 120, cardHeight = 160;
BufferedImage image = new BufferedImage(cardWidth,
cardHeight,
BufferedImage.TYPE_INT_ARGB);
java.awt.Graphics2D gr = (java.awt.Graphics2D)
image.getGraphics();
gr.setColor(java.awt.Color.WHITE);
gr.fillRect(0, 0, cardWidth, cardHeight);
gr.setColor(java.awt.Color.BLACK);
gr.drawRect(0, 0, cardWidth - 1, cardHeight - 1);
Font font = new Font("Dialog", Font.PLAIN, 50);
gr.setFont(font);
String prefix = card.substring(0, 1);
String postfix = card.substring(1, 2);
String suit = "";
java.awt.Color color = java.awt.Color.BLACK;
if (prefix.equals("S")) {
suit = "\u2660";
} else if (prefix.equals("H")) {
suit = "\u2665";
color = java.awt.Color.RED;
} else if (prefix.equals("C")) {
suit = "\u2663";
} else if (prefix.equals("D")) {
suit = "\u2666";
color = java.awt.Color.RED;
}
String point = postfix;
int x = 20;
if (postfix.equals("T")) {
x = 10;
point = "10";
}
gr.setColor(color);
gr.drawString(suit + point, x, 100);
return image;
}
public void paintComponent(java.awt.Graphics graphics) {
super.paintComponent(graphics);
int w = getWidth();
int h = getHeight();
java.awt.Graphics2D gr = (java.awt.Graphics2D) graphics;
int y = 10;
int x = 10;
for (int i = 0; i < cardImage.length; i++) {
gr.drawImage(cardImage[i], x, y, this);
x += 130;
if ((i + 1) % 13 == 0) {
y += 170;
x = 0;
}
}
}
public static void main(String[] args) throws Exception {
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setTitle("COMS207 Deck");
CardPanel panel = CardPanel.getInstance();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
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