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

he class holds fields that con tain a. In Chapter 3, you designed a Card class.

ID: 3709446 • Letter: H

Question

he class holds fields that con tain a. In Chapter 3, you designed a Card class. T 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 the class to hold the string representation of a Card's rank based on its value. Within the Card class setvalueO method, besides setting the numeric value, also set the string rank value as follows. String value for rank Ace "2" through "10 "Jack" "Queen" uKing Numeric value 2 through 10 12 13

Explanation / Answer

Create Card.java and War2.java in the same package and the following code files should work as desired.

Each and every important step is explained with comments.

Card.java

==========================

import java.util.HashMap;

public class Card {

// Card value in integers ranging between 1-13

private int value;

// Card suit as String - "Spades","Hearts","Diamonds" Or "Clubs"

private String suit;

// Card rank as String based on the card value ranging from "1" to "King"

private String rank;

// A map that holds mapping between value and its rank

private static HashMap<Integer,String> valueToRankMap;

// A static block that gets executed only once for the class

// This avoids recreating a static-non changing map every time a card object is created

static{

// Create a map that holds mapping between value and its corresponding rank

valueToRankMap = new HashMap();

valueToRankMap.put(1, "Ace");

valueToRankMap.put(2, "2");

valueToRankMap.put(3, "3");

valueToRankMap.put(4, "4");

valueToRankMap.put(5, "5");

valueToRankMap.put(6, "6");

valueToRankMap.put(7, "7");

valueToRankMap.put(8, "8");

valueToRankMap.put(9, "9");

valueToRankMap.put(10, "10");

valueToRankMap.put(11, "Jack");

valueToRankMap.put(12, "Queen");

valueToRankMap.put(13, "King");

}

// Default Constructor

public Card() {

super();

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

// apart from setting the value, it also sets the proper rank

this.rank = valueToRankMap.get(value);

}

public String getSuit() {

return suit;

}

public void setSuit(String suit) {

this.suit = suit;

}

public String getRank() {

return rank;

}

}

War2.java

==========================

import java.util.Random;

public class War2 {

// All the cards as array of Card objects

private static Card cards[] = new Card[52];

// All the four suits listed using array for easy access to run thru and create cards

private static String suits[] = new String[]{"Spades","Hearts","Diamonds","Clubs"};

// Driver method

public static void main(String[] args) {

// fill up cards array

int index = 0;

for(String suit : suits){

for(int value=1;value <= 13;value++){

Card card = new Card();

card.setSuit(suit);

card.setValue(value);

cards[index++] = card;

}

}

// pick a card for the computer

Card compCard = getRandomCard();

System.out.println("Computer selected card "+compCard.getRank()+"-"+compCard.getSuit());

// pick another for the player

Card playerCard = getRandomCard();

System.out.println("Player selected card "+playerCard.getRank()+"-"+playerCard.getSuit());

// now compare both and announce the winner

if(compCard.getValue() == playerCard.getValue()){

System.out.println("Tie!!");

}else if(compCard.getValue() > playerCard.getValue()){

System.out.println("Computer is the winner");

}else{

System.out.println("Player wins!!");

}

}

private static Card getRandomCard(){

// Use java's in built random generator

Random random = new Random();

// Get a random number between 0-51

int randomIndex = random.nextInt(52);

// Use this random number as index to get the card

return cards[randomIndex];

}

}