Create a Card class that represents a single playing card. Every playing card co
ID: 3640544 • Letter: C
Question
Create a Card class that represents a single playing card. Every playing cardcontains two pieces of information: a value and a suit. For the purposes of this
program, we will assume that all card values are integers (so that Jacks are 11,
Queens are 12, Kings are 13, and Aces are 14). The suit of a card may be
represented by your choice of a String (i.e., "Clubs") or a character (i.e., "C").
(a) Every Card object should contain a constructor that sets its value and suit,
appropriately named methods that return its value and suit, and a toString()
method that returns its textual representation.
(b) The toString() method should return a new String containing the card's
value (or J, Q, K, or A for jacks, queens, kings, and aces respectively) and a
character representing its suit. For example, a three of diamonds would be
represented as 3D, and the queen of clubs as QC. toString() always has the
following header:
public String toString()
Note that this method does not print anything directly; it simply returns a String
that can be printed by some other part of the code!
Explanation / Answer
public class Card {private int value;
private String suit;
Card (int value,String suit) {
this.suit = suit;
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String toString() {
String output = null;
if(this.value > 10) {
if (this.value==11) output="J";
if (this.value==12) output="Q";
if (this.value==13) output="K";
if (this.value==14) output="A";
} else {
output = ""+this.value;
}
if(this.suit!=null) {
char[] id = suit.toCharArray();
output = ""+output+ id[0];
}
return output;
}
public static void main(String[] args) {
Card> new Card(12,"Hearts");
System.out.println(one.toString());
}
} public class Card {
private int value;
private String suit;
Card (int value,String suit) {
this.suit = suit;
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String toString() {
String output = null;
if(this.value > 10) {
if (this.value==11) output="J";
if (this.value==12) output="Q";
if (this.value==13) output="K";
if (this.value==14) output="A";
} else {
output = ""+this.value;
}
if(this.suit!=null) {
char[] id = suit.toCharArray();
output = ""+output+ id[0];
}
return output;
}
public static void main(String[] args) {
Card > new Card(12,"Hearts");
System.out.println(one.toString());
}
}