USING JAVA a. Create a card class that represents a typical playing card. It sho
ID: 3683257 • Letter: U
Question
USING JAVA
a. Create a card class that represents a typical playing card. It should hid 2 attributes (rank 1-13 representing A through King) and Suit(1 through 4 representing clubs, hearts, spades and diamonds). A constructor accepts rank and suit as int parameters. Provide the standard getter methods and a toString method.
For Example the client code might produce this output.
Card c = new Card(1,4);
System.out.println(c.getSuit());
System.out.println(c);
4
Ace of Spades
Create a driver program that demonstrates the functionality of the Card class.
b. Design a DeckofCards class that represents a typical deck of 52 playing cards using a private array of Card. The construcotr of the class will initialize the hidden array to contain the standard deck of cards. The class should implement this interface.
public interface DeckofCardsInterface
{
public void shuffle()
//shuffles the deck and resets deal to beginning of deck
public in cardsLeft();
//returns # of undealt cards
public Card dealCard();
//if all cards dealth then shuffle, returns next card
public String toString();
//returns a string representing the entire deck
}
Design a test driver that shows DeckofCards.java works correctly.
Explanation / Answer
public class Deck
{
private Card [] deck;
private int numberOfCards;
/**
* Creates a deck of 52 playing cards
* The deck is set up in the order SPADES, DIAMONDS, CLUBS,HEARTS
*/
public Deck()
{
String [] suits = {"SPADES","DIAMONDS","CLUBS","HEARTS"};
for ( int suit = 0; suit <= 3; suit++ )
{
for ( int rank = 1; rank <= 13;rank++ )
{
numberOfCards = 0;
deck [numberOfCards] = new Card(rank,suit);
numberOfCards ++;
}
}
numberOfCards = 52;
}
/**
* shuffles the deck of cards.
*
*/
/* public void shuffle()
{
Card temp; // Temporary object to hold value whilst swapping
//loop through each card in the deck and swap it with some random card.
//FOR EACH CARD IN THE DECK
{
int rand = (int)(Math.random()*52);
//TAKE THE CURRENT CARD...
//AND SWAP IT WITH THE RAND CARD...
}
}
/**
* Returns a representation of the deck as a string
* one card per line
* @return the deck as a String
*/
public String toString()
{
String deckString = "New deck created ";
for (int cards =0; cards <52; cards++)
{
deckString = deckString + deck[cards] + " ";
}
System.out.println(deckString);
return deckString;
}
}
----------------------------------------------------------------------------------------------
/**
* This class represents a simple playing card.
* The attributes of the card are both of type String:
* rank - takes values ACE,1,2,3,.., JACK, QUEEN, KING
* suit - takes values SPADES, DIAMONDS,CLUBS, HEARTS
*/
public class Card
{
private String rank;
private String suit;
/*Constructor for objects of class Card
* Each card has a rank and a suit. No checks are made for invalid values
* @param rank an int representing the valus of the card, range 1..13
* @param suit a String representing the suit of card: values
"SPADES","DIAMONDS","CLUBS","HEARTS"
*/
public Card(int rank, int suit)
{
if(suit == 0)
{
this.suit = "Spades";
}
else if(suit == 1)
{
this.suit = "Diamonds";
}
else if(suit ==2)
{
this.suit = "Clubs";
}
else
{
this.suit = "Hearts";
}
// must convert the integer value of rank to an appropriate String
if (rank == 1)
{
this.rank = "Ace";
}
else if (rank == 11)
{
this.rank = "Jack";
}
else if (rank == 12)
{
this.rank = "Queen";
}
else if (rank == 13)
{
this.rank = "King";
}
else
{
this.rank = "" + rank;
}
}
public String getSuit()
{
return suit;
}
public String getRank()
{
return rank;
}
}
-------------------------------------------------------------------------------
public class DeckTester
{
public static void main(String[] args)
{
Deck deck1 = new Deck();;
System.out.println(deck1.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.