Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using DrJava create a program that does the following: 1. Write a class named Ca

ID: 3695291 • Letter: U

Question

Using DrJava create a program that does the following:

1. Write a class named Card. It can be used to represent a (single) card from a deck of cards. The class should be able to store a card's suit and face value. A card's suit is one of the following: Hearts, Diamonds, Spades, or Clubs. A card's face value is one of: Ace, Jack, Queen, King, or a value in the range of two through ten.

2. Write a class named Deck. This class's constructor should create a list of 52 Card objects, each representing a valid card in a deck of cards. You must implement the list of card objects using one of the classes or interfaces found in chapter 19 or 20. You cannot use facilities discussed further in the book.

3. Your Deck class must have a method called shuffle(). This method randomly shuffles the Card objects in the list.

4. You Deck class must have a method called deal(). It deals a card by removing the Card object at the beginning of the list and returns a reference to that object (i.e. the Card object).

5. Write a CardPlayer class. This class needs to keep a list of Card objects that have been dealt to it. (See 4). This can be used to represent a hand of cards.

6. Write a method in the CardPlayer class called getCard().   getCard gets passed to it a reference to a Card object and adds it to the list (maintained by CardPlayer).

7. Write a method in the CardPlayer class called showCards().   showCards displays (all of the) Card objects in the list.

8. Write a driver/test program which

   a) Creates a Deck object

   b) shuffles the cars it contains

   c) deals 5 cards from the Deck to a CardPlayer object.

   d) displays the cards in the CardPlayer object using the showCards method.

Explanation / Answer

public class Card {

private int cardNum;
final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};

Card (int theCard) {
setCardNum (theCard);
}

public void setCardNum (int theCard) {
cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
}

public int getCardNum() {
return cardNum;
}

public String toString() {
return ranks[cardNum%13] + " of " + suits[cardNum/13];
}

public String getSuit() {
return suits[cardNum/13];
}

public String getRank() {
return ranks[cardNum%13];
}

public int getValue() {
return cardNum%13;
}
}
public class Deck {

private Card[] deck = new Card[52];
private int topCard;

Deck() {

topCard = 0;

for (int i = 0; i < deck.length; i++)
deck[i] = new Card(i);

}

public void shuffle() {

topCard = 0;

for (int i = 0; i < 1000; i++) {
int j = (int)(Math.random()*52);
int k = (int)(Math.random()*52);
Card tmpCard = deck[j];
deck[j] = deck[k];
deck[k] = tmpCard;
}
}

public Card dealCard() {
Card theCard;
if (topCard < deck.length) {
theCard = deck[topCard];
topCard++;
}
else
theCard = null;

return theCard;
}
}

import java.util.Scanner;

public class CardPlayer {

public static void main(String[] args) {

Card[][] hands = new Card[2][1];
Deck myDeck = new Deck();

//reduced this to 26 iterations because two cards are dealt each iteration
for (int i = 0; i < 26; i++) {
System.out.printf(" Round %s of The War ", i);

//You really don't need to loop just once here...
//Simply assign the card to hands[player][0] since that's the only option
//for (int c = 0; c < 1; c++)
for (int player = 0; player < hands.length; player++)
hands[player][0] = myDeck.dealCard();

for (int player = 0; player < hands.length; player++) {
System.out.printf("Player %d: ", player);
showCards(hands[player]);
}

int player1 = hands[0][0].getValue(); //get the value from the Card object
int player2 = hands[1][0].getValue();

if (player1 > player2)
System.out.println("Player One Wins The War");
else if (player2 > player1)
System.out.println("Player Two Wins The War");
else
System.out.println("The War Is A Tie");

}
}
public static void showCards(Card[] hand) {

for (int card = 0; card < hand.length; card++)
System.out.printf("%s", hand[card].toString());

System.out.println();

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote