The class holds fields that contain a card’s value and suit. Currently, the suit is represented by a single character (s

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

The class holds fields that contain a card’s value and suit. Currently, the suit is represented by a single character (s

Post by answerhappygod »

The class holds fields that contain a card’s value and suit.Currently, the suit is represented by a single character (s, h, d,or c). Modify the class so that the suit is a string (“Spades”,“Hearts”, “Diamonds” or “Clubs”). Also, add a new field to theclass to hold the string representation of a card’s rank based onits value. Within the card class setValue() method, besides settingthe numeric value, also set the string rank value as follows: (saveagain as Card.java)
1 “ A c e”
2 t h r ou g h 1 0
11 “J a ck”
1 2 “ Qu een”
13 “K i n g”
Now, create an array of 52 card objects, assigning a differentvalue to each card, and display each card. Save the application asFullDeck.java.
create a game that plays 26 rounds of War, dealing a full deckwith no repeated cards. Some hints:  Use the FullDeck class. Select a random number for the deck position of the player’s firstcard and assign the card at that array position to the player. Move every higher-positioned card in the deck “down” one to fill inthe gap. In other words, if the player’s first random number is 49,select the card at position 49, move the card that was in position50 to position 49, and move the card that was in position 51 toposition 50. Only 51 cards remain in the deck after the player’sfirst card is dealt, so the available-card array is smaller by one. In the same way, randomly select a card for the computer and“remove” the card from the deck.  Display the values of theplayer’s and computer’s cards, compare their values, and determinethe winner.  When all the cards in the deck are exhausted, displaya count of the number of times the player wins, the number of timesthe computer wins, and the number of ties.
Part of the code is as follows
public class War2 {
/*
* War Class
*/
public void getResult(){
War card = newWar();
String compValue=card.getValue();
String humanValue=card.getValue();
String compSuit =card.getSuit();
String humanSuit =card.getSuit();
//when war is called, 10 cards arediscarded between Computer and Human
for(int i=0; i<10;i++){
compValue=card.getValue();
humanValue=card.getValue();
compSuit =card.getSuit();
humanSuit =card.getSuit();
String compCard = compValue + " of " + compSuit;
String humanCard = humanValue + " of " + humanSuit;
System.out.println(compCard+" "+ humanCard+" Discard");
}
compValue =card.getValue();
humanValue=card.getValue();
if(compValue=="Ace")
compValue="1";
if (humanValue=="Ace")
humanValue="1";
if(compValue=="Jack")
compValue="11";
if (humanValue=="Jack")
humanValue="11";
if(compValue=="Queen")
compValue="12";
if (humanValue=="Queen")
humanValue="12";
if(compValue=="King")
compValue="13";
if (humanValue=="King")
humanValue="13";
if(Integer.parseInt(compValue)== Integer.parseInt(humanValue)){
System.out.println("\nWar is Called!");
getResult();
}
elseif(Integer.parseInt(compValue) >Integer.parseInt(humanValue)){
System.out.println(compValue+" of "+ card.getSuit()+ " " +humanValue+" of "+ card.getSuit()+ " "+" Computer Wins");}
else if (Integer.parseInt(compValue) <Integer.parseInt(humanValue)){
System.out.println(compValue+" of "+card.getSuit()+ " " +humanValue+" of " +card.getSuit()+ " "+" Human Wins");}
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply