Willing to give more points PART A: The Card class The Card class represents a s
ID: 647945 • Letter: W
Question
Willing to give more points
PART A: The Card class The Card class represents a single card in a Deck of Cards. These cards are from one of 4 suits (HEART, DIAMOND, SPADE, CLUB) and has a value of) through 13 with 1, 11, 12, or 13 being the Ace (A), Jack (J), Queen (Q) or King (K) respectively. Each Card object can give a String representation of itself, and see if it is equal to another Card. The Card class also has a class method that can take a string version of a Card and produce a new instance of a Card with the correct value and suit. 1. Implement a Card class using the structure below. Private instance, or class variables may be added as required. The constructor must be modified to check that the value ola created card is between I to 13 inclusive and that the suit is one of; HEART, DIAMOND, SPADE or CLUB. If not then the constructor should print an error and use exit o to terminate the program. 2. Provide a public boolean equals (Object obj) method that returns true if the parameter obj is a Card obec1 that has the same value and suit as receiver. 3. Provide a public String toString method that returns the Card?s value and suit separated by a space. The values should be represented A, J, Q, K if the numerical values are 1, 11, 12, or 13 respectively. And the suits should be outputted as a single capital character; H, D, S or C 4. Provide apublic static Card cardFromString(String stringRep) method that takes the string representation of a card as a parameter and returns the appropriate instance of a card. The string representation will be the value and suit (as described in step 3 above) with a single separating space. If the string is not in the mentioned format return null.Explanation / Answer
public class Card {
public final static int Heart = 1;
public final static int DIAMOND = 2;
public final static int SPADE = 3;
public final static int CLUB = 4;
private int value;
private int suit;
public Card(int initVal, int initSuit){
if((initVal > 13 || initVal < 1) || initSuit < Heart || initSuit > CLUB){
System.err.println("Invalid card input");
System.exit(1);
}
value = initVal;
suit = initSuit;
}
public boolean equals(Object obj){
Card c = (Card)obj;
return c.value == value && c.suit == suit;
}
public String toString(){
char S = 'H';
String str = "";
if(value == 1){
str = str + "A";
}
else if(value == 11){
str = str + "J";
}
else if(value == 12){
str = str + "Q";
}
else if(value == 13){
str = str + "K";
}
else{
str = str + Integer.toString(value);
}
str = str + " ";
if(suit == 1){
S = 'H';
}
else if(suit == 2){
S = 'D';
}
else if(suit == 3){
S = 'S';
}
else if(suit == 4){
S = 'C';
}
str = str + S;
return str;
}
public static Card cardFromString(String stringRep){
String str[] = stringRep.split(" ");
String s0 = str[0];
String s1 = str[1];
int value;
int suit;
if(s0.equals("A")){
value = 1;
}
else if(s0.equals("J")){
value = 11;
}
else if(s0.equals("Q")){
value = 12;
}
else if(s0.equals("K")){
value = 13;
}
else if(Integer.parseInt(s0) <= 10 && Integer.parseInt(s0) >= 2){
value = Integer.parseInt(s0);
}
else{
return null;
}
if(s1.equals("H")){
suit = 1;
}
else if(s1.equals("D")){
suit = 2;
}
else if(s1.equals("S")){
suit = 3;
}
else if(s1.equals("C")){
suit = 4;
}
else{
return null;
}
return new Card(value, suit);
}
public static void main(String args[]){
Card c1 = new Card(13, 4);
Card c2 = cardFromString(c1.toString());
if(c2 != null)
System.out.println(c1.equals(c2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.