In: Computer Science
Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Use an enum type to represent rank and another enum type to represent suit. Write another class whose instances represent a full deck of cards. Both these classes should have toString methods. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards by calling its toString method.
(You might need to create the following files: Suit.java for enum type, Rank.java for enum type, Card.java, Deck.java and DeckDemo.java)
Code Image:
Sample Output:
Code to Copy:
Suit.java:
//Suit.java
//Declare Suit as enum type
public enum Suit {
//SPADE,CLUB,DIAMOND,HEART as type of
//enum variables
SPADE, CLUB, DIAMOND, HEART
}
Rank.java:
//Rank.java
//Declare Rank as type of enum
public enum Rank {
//Decalre ONE, TWO, THREE, FOUR, FIVE, SIX,
//SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
//as type of enum variables
ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
Card.java:
//Card.java
//Implementation of Card class
public class Card {
//Declae suit as type of Suit
private Suit suit;
//Declare rank as type of Rank
private Rank rank;
//Implementation of parameterized constructor
public Card(Suit s, Rank r) {
//assign s to this.suit
this.suit = s;
//assign r to this.rank
this.rank = r;
}
//Implementation of getRank function
public Rank getRank() {
return rank;
}
//Implementation of getSuit function
public Suit getSuit() {
return suit;
}
//Implementation of toString function
public String toString()
{
//Declare str as type of String
String str = rank +" of "+suit;
//return str
return str ;
}
}
Deck.java:
//Deck.java
import java.util.ArrayList;
//Implementation of Deck class
public class Deck {
//Declare listOfCards as type of ArrayList<Card>
private ArrayList<Card> listOfCards;
//and assign 0
private int cardCount = 0;
//Implementation of Default constructor
public Deck() {
listOfCards = new ArrayList<>();
//Iterate the loop
for (Suit each : Suit.values())
{
//Iterate the loop
for (Rank eachRank : Rank.values())
{
//Create Card class object through new
Card requiredCard = new Card(each, eachRank);
//call add function
listOfCards.add(requiredCard);
}
}
//Initilaize cardCount with 0
cardCount = 0;
}
//Implementation of dealCards function
public Card dealCards() {
//increment cardCount by 1
//call get functon
return listOfCards.get(cardCount++);
}
//Implementation of containCard function
public boolean containCard() {
//return the resultant value of cardCount is
return cardCount < listOfCards.size();
}
//Implementation of toString function
public String toString() {
return listOfCards.toString();
}
}
DeckDemo.java:
//DeckDemo.java
//Implementation of DeckDemo class
public class DeckDemo {
//Implementation of main function
public static void main(String[] args)
{
//Declare an object deckDetails of Deck class
//and initialize
Deck deckDetails = new Deck();
//Iterate the loop
while (deckDetails.containCard()){
//Display statement
System.out.println(deckDetails.dealCards());
}
}
}