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

Playing cards are used in many computer games, including versions of such classi

ID: 3558158 • Letter: P

Question

Playing cards are used in many computer games, including versions of such classics
as solitaire, hearts, and poker. Design a Card class that contains a character data field
to hold a suit (s for spades, h for hearts, d for diamonds, or c for clubs) and an integer
data field for a value from 1 to 13. (When you learn more about string handling in
the chapter Characters, Strings, and the StringBuilder, you can modify the class to
hold words for the suits, such as spades or hearts, as well as words for some of the
values

Explanation / Answer

/ *

@author User

*/
import java.io.*;
public class Card
{
char suit;
int value;

public void setValue(char ch,int var)
{
suit=ch;
value=var;
}

public void getValue(char ch,int var)
{
switch(ch)
{
case 'c':
System.out.println("The card chosen is:" + var + " of " + "clubs");
break;

case 'd':
System.out.println("The card chosen is:" + var + " of " + "diamonds");
break;

case 'h':
System.out.println("The card chosen is:" + var + " of " + "hearts");
break;

case 's':
System.out.println("The card chosen is:" + var + " of " + "spades");
break;

}

}

public static void main(String args[])throws IOException
{
Card ob1=new Card();
Card ob2=new Card();
char ch1='c';
char ch2='s';
final int CARDS_IN_SUIT = 13;
int myValue1 = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);
int myValue2 = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);
ob1.getValue(ch1, myValue1);
ob2.getValue(ch2, myValue2);
ob1.setValue(ch1, myValue1);
ob2.setValue(ch2, myValue2);
}
}