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

In the last assignment, you created a card class. Modify the card class so the s

ID: 3723817 • Letter: I

Question

In the last assignment, you created a card class. Modify the card class so the setValue() method does not allow a card’s value to be less than 1 or higher than 13. If the argument to setValue() is out of range, assign 1 to the card’s value.

You also created a PickTwoCards application that randomly selects two playing cards and displays their values. In that application, all card objects were arbitrarily assigned a suit represented by a single character, but they could have different values, and the player observed which of two card objects had the higher value. Now, modify the application so the suit and the value both are chosen randomly. Using two card objects play a very simple version of the card game 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 the suit is). For this game, assume the Ace (value 1) is low. Make sure that the two cards dealt are not the same card. For example, a deck cannot contain more than one card representing the 2 of spades. If two cards are chosen to have the same value, change the suit for one of them. Save the application as War.java

Now modify the game using the newly modified card class so that when a tie is declared, that each player “puts down 10 cards each” and compares the 11th card to see if there is a clear winner. If there is a tie, repeat the process until there is a clear winner. The table below shows four typical executions. Recall that in this version of War, you assume

that the ace is the lowest-valued card. Save the game as War2.java.

So I expect a Card.java, War.java and War2.java file; Each working off the other. No need to reinvent the wheel! Lastly I expect, when WAR is called, to see all ten cards displayed.

I did the part 1 war.java and i just need help on part 2

Explanation / Answer

My Card Class:

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;
}
}

My Deck Class

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;
}
}

My War Game Main Program:

import java.util.Scanner;

public class WarGame {

public static void main(String[] args) {

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

for (int i = 0; i < 53; i++) {
System.out.printf(" Round %s of The War ", i);

for (int c = 0; c < 1; c++)
for (int player = 0; player < hands.length; player++)
hands[player][c] = myDeck.dealCard();

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

int player1;
int player2;

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

}
}
}

public static void printHand(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