IN JAVA - COMMENT CODE WELL
Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace.
Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the deck. It also needs a method to deal one card at a time.
Write a CardPlayer class. This class should keep a list of card objects. (The class represents a hand of cards.) This class should have a method getCard, that accepts a reference to a card object and adds it to the CardPlayer. The class should also have a method named showCards that will display all of the cards held by a CardPlayer object.
Write a separate Demonstration that creates a deck of cards, creates two CardPlayer objects, deals 5 cards to each CardPlayer object, one at a time, and displays both hands.
Please make sure you comment your code thoroughly.
The code should be nicely formatted and should use proper variables.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Please find the code for Card.java, Deck.java, CardPlayer.java and Demonstration.java below. You must place these classes in separate .java files, not in a single file.
// Card.java
public class Card {
// attributes
int suit;
int rank;
int CLUBS = 1;
int DIAMONDS = 2;
int HEARTS = 3;
int SPADES = 4;
int ACE = 1;
int JACK = 11;
int QUEEN = 12;
int KING = 13;
// constructor to initialize a Card with suit and rank values. Assuming
// values are valid
// @param suit - suit value (1-4)
// @param rank - rank value (1-13)
Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
// getter method for the suit
int getSuit() {
return this.suit;
}
// getter method for the rank
int getRank() {
return this.rank;
}
// returns a String containing card information like "King of Hearts"
public String toString() {
String ans = "";
// add the rank of the card
if (rank == ACE)
ans += "Ace";
else if (rank == JACK)
ans += "Jack";
else if (rank == QUEEN)
ans += "Queen";
else if (rank == KING)
ans += "King";
else
ans += String.valueOf(rank);
ans += " of ";
// add the suit of the card
if (suit == SPADES)
ans += "Spades";
else if (suit == CLUBS)
ans += "Clubs";
else if (suit == HEARTS)
ans += "Hearts";
else if (suit == DIAMONDS)
ans += "Diamonds";
return ans;
}
}
// Deck.java
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
// list to store Card objects
private ArrayList<Card> cards;
// constructor initializes deck
public Deck() {
// initializing deck
initialize();
}
// method to initialize/reset the deck
public void initialize() {
// initializing array list
cards = new ArrayList<Card>();
// looping through each suit value
for (int suit = 1; suit <= 4; suit++) {
// looping through each rank value
for (int rank = 1; rank <= 13; rank++) {
// creating a Card and adding to list
Card card = new Card(suit, rank);
cards.add(card);
}
}
}
// method to shuffle the cards list
public void shuffle() {
// using Collections, shuffling the cards
Collections.shuffle(cards);
}
// method to remove and return the first card after shuffling
public Card dealCard() {
if (cards.isEmpty()) {
// deck is empty
return null;
}
// removing top card and returning it
return cards.remove(0);
}
}
// CardPlayer.java
import java.util.ArrayList;
public class CardPlayer {
// list of cards
ArrayList<Card> hand;
// constructor to initialize the empty cards list
public CardPlayer() {
hand = new ArrayList<Card>();
}
//accepts a card and adds to hand
public void getCard(Card c) {
hand.add(c);
}
//displays all the cards in the list
public void showCards() {
for (Card c : hand) {
System.out.println(c);
}
}
}
// Demonstration.java
public class Demonstration {
public static void main(String[] args) {
// creating a Deck
Deck deck = new Deck();
// creating two players
CardPlayer player1 = new CardPlayer();
CardPlayer player2 = new CardPlayer();
// shuffling the deck
deck.shuffle();
// dealing 5 cards to each player
for (int i = 0; i < 5; i++) {
player1.getCard(deck.dealCard());
player2.getCard(deck.dealCard());
}
// showing both players' hands.
System.out.println("Player 1 Hand:");
player1.showCards();
System.out.println("\nPlayer 2 Hand:");
player2.showCards();
}
}
/*OUTPUT*/
Player 1 Hand:
Ace of Spades
Queen of Diamonds
5 of Hearts
7 of Clubs
King of Hearts
Player 2 Hand:
9 of Clubs
8 of Hearts
Jack of Spades
4 of Diamonds
Jack of Hearts
IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...
Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...
CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...
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....
Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!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 new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...
Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...
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...
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...
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...
War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War: Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...
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,...