Question

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 Card object. Each Card object contains two instance variables ─ num and suit. Study the Card class definition below for details.

dealCard: This method removes the highest-indexed card in the ArrayList and returns it. In general, a method should not do more than what it’s supposed to do. Thus, dealCard should not print anything.

toString: This method returns the deck’s contents using the format shown in the output session. In particular, note that toString should insert a newline after every fifth card. Hint: In coming up with a return value, use a String local variable. As you generate card values and newlines, concatenate those items to your local variable using the += operator.

Write your code such that the following classes produce the output shown in the subsequent output.

/*************************************************************

* DeckDriver.java

* <your name>

* This class tests the Deck class.

*************************************************************/

public class DeckDriver

{

public static void main(String[] args)

{

Deck deck = new Deck();

System.out.println(deck.dealCard());

System.out.println(deck.dealCard());

System.out.println();

System.out.println(deck);

} // end main

} // end DeckDriver class

/****************************************************************

* Card.java *

<your name>

*

* This class stores a Card's information.

****************************************************************/

public class Card

{

private int num; // hold a number between 1 and 13

private char suit; // holds 'C' for clubs, 'D' for diamonds,

// 'H' for hearts, 'S' for spades

//**************************************************

public Card(int num, char suit)

{

this.num = num; this.suit = suit;

} // end Card constructor

//************************************************** //

Return the card's value in the form of a concatenated

// number and character.

// For example, 1C = ace of clubs, 12H = queen of hearts.

public String toString()

{

return Integer.toString(num) + suit;

}

} // end class Card

----------------------------

Output:

13S

12S

1C 2C 3C 4C 5C

6C 7C 8C 9C 10C

11C 12C 13C 1D 2D

3D 4D 5D 6D 7D

8D 9D 10D 11D 12D

13D 1H 2H 3H 4H

5H 6H 7H 8H 9H

10H 11H 12H 13H 1S

2S 3S 4S 5S 6S

7S 8S 9S 10S 11S

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

code:

import java.util.Random;

import java.util.ArrayList;

//class DECK

public class Deck

{

//arraylist for deck of cards

private ArrayList<Card> cardsValue;

//constructor

public Deck()

{

cardsValue = new ArrayList<Card>();

for(int a1 =0; a1<=3; a1++)

{

for(char b1 =0; b1<=12;b1++)

{

//contains 2 instance varaibles

cardsValue.add(new Card(a,b));

}

}

}

//method deal card

public Card dealCard()

{

//removes the high indexed cards

Random generators = new Random();

int index1 = generators.nextInt(cardsValue.size());

return cardsValue.remove(index1);

}

public String toString()

{

//method to STIRNG to show the cards

String results = "CARDS: " + cardsValue;

// it returns the RESULT

return results;

}

}

Add a comment
Know the answer?
Add Answer to:
Java Write a complete program that implements the functionality of a deck of cards. In writing...
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
  • 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;...

  • 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...

  • //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...

  • 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...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • 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...

  • 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,...

  • 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...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...

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