Question

I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it is complete, shuffles the deck, and then deals each card from a shuffled deck, displaying each card as it is dealt.

This is what I have so far. I just don't know how to integrate it with the DeckOfCards class as an array.


public class Card {

private int faceValue;
private int suit;
  
public static final int HEARTS = 1;
public static final int DIAMONDS = 2;
public static final int CLUBS = 3;
public static final int SPADES = 4;
private static final int MAX_SUITS = 4;
private static final int MAX_FACES = 13;
//sets the face and suit of card at random
public Card() {
faceValue = (int) (Math.random() * MAX_FACES) + 1;
suit = (int) (Math.random() * MAX_SUITS) + 1;
}

public Card(int faceValue, int suit) {
this.faceValue = faceValue;
if (faceValue < 1 || faceValue > 13) {
System.out.println("ERROR: Invalid face value");
this.faceValue = 1;
}
this.suit = suit;
if (suit != HEARTS && suit != DIAMONDS && suit != CLUBS && suit != SPADES) {
System.out.println("ERROR: Invalid suit value");
this.suit = HEARTS;
}
}
//return the face value
public int getFaceValue() {
return faceValue;
}
//returns the suit
public int getSuit() {
return suit;
}
//converts suit/face number to name as string
public String toString() {
String s = "";
switch (faceValue) {
case 1:
s += " Ace";
break;
case 11:
s += " Jack";
break;
case 12:
s += " Queen";
break;
case 13:
s += " King";
break;
case 10:
s += " 10";
break;
default:
s += " " + faceValue;
break;
}

switch (suit) {
case HEARTS:
s += " of Hearts";
break;
case DIAMONDS:
s += " of Diamonds";
break;
case CLUBS:
s += " of Clubs";
break;
case SPADES:
s += " of Spades";
break;
}

return s;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class aDeckOfCards
{
Card array = new Card[52]; // Array of type Card to store deck of cards
int index = 0;
int currentCountOfCards = 52;
public void makeDeck() // for formation of all 52 cards
{
for(int suit =1; suit<=4; suit++)
{
for(int face = 1; face<=13; face++ )
{
Card newCard;
newCard.suit = suit;
newCard.faceValue = face;
array[index++] = newCard;
}
}
}
  
public void reverse(Card& array1[],int start, int end) // to reverse an array
{
while(start<end)
{
Card temp = array1[start];
array1[start] = array1[end];
array1[end] = temp;
start++;
end--;
}
}
  
public void shuffle(int partition) // partition from where the cards are shuffled
{
//simply a rotation of array from partition index
// 1 2 3 4 5
// if partition index is 2
// rotated array = 4 5 3 1 2 , hence shuffled
reverse(array, 0,partition -1);
reverse(array, partition +1,51);
reverse(array, 0 , 51);
}
  
public void dealACard(int index) // index of card picked
{
if(index > currentCountOfCards)
System.out.println("Invalid option selected");
Card toDeal = array[index-1];
System.out.println(toDeal.toString()); // prints card picked
currentCountOfCards--;
System.out.println("Number of cards left: ");
System.out.print(currentCountOfCards);
}
  
   public static void main (String[] args) throws java.lang.Exception
   {
       aDeckOfCards deck;
       deck.makeDeck();
       deck.shuffle(18); // all cards from 19th card will be moved up
       deck.dealACard(26);
   }
}

Add a comment
Know the answer?
Add Answer to:
I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

    JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main point of the exercise is to demonstrate your ability to use various types of nested classes. Of course, sorting is important as well, but you don’t really need to do much more than create the class that does the comparison. In general, I like giving you some latitude in how you design and implement your projects. However, for this assignment, each piece is very...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • Now, create a Deck class that consists of 52 cards (each card is an instance of...

    Now, create a Deck class that consists of 52 cards (each card is an instance of class Card) by filling in the template below. Represent the suit of cards as a string: "Spades", "Diamonds", "Hearts", "clubs" and the rank of cards as a single character or integer: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", class Deck: def init (self): pass # Your code here. def draw (self): Returns the card at the top of the deck, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT