you need to complete the methods needed to get TestPlayingCard to run properly.
ID: 3562285 • Letter: Y
Question
you need to complete the methods needed to get TestPlayingCard to run properly. You should do this step by step. First get the accessors for suit and value working properly, then get the toString() method to work properly. Pay particular attention to how the value of 10 is supposed to be displayed by toString(). Every time you make a change to the PlayingCard class, run the TestPlayingCard code to make sure that your change worked. When you are done, the test code should run properly and give you all of the correct outputs. For this exercise, each time you modify the PlayingCard class, add another test case to your TestPlayingCard code. When you are done, you should have come up with at least 3 additional test cases.
public class TestPlayingCard {
public static void main(String[] args) {
PlayingCard c1 = new PlayingCard('S', 'A');
displayCard(c1, "Ace of Spades");
PlayingCard c2 = new PlayingCard('D', '3');
displayCard(c2, "3 of Diamonds");
PlayingCard c3 = new PlayingCard('H', 'Q');
displayCard(c3, "Queen of Hearts");
PlayingCard c4 = new PlayingCard('C', 'X');
displayCard(c4, "10 of Clubs");
}
public static void displayCard(PlayingCard c1, String truth) {
System.out.println("Should be: " + truth);
System.out.println("Suit is: " + c1.getSuit());
System.out.println("Value is: " + c1.getValue());
System.out.println("String value of card is: " + c1);
System.out.println();
}
}
public class PlayingCard implements Comparable {
// TODO - Setup private variables here
public PlayingCard(char suit, char value) {
// TODO - Implement constructor
// This constructor should create the playing card
// Note that there are no mutators in this class - once
// a card is created its values never change
}
public char getSuit() {
// TODO - Implement getSuit()
return 'x';
}
public char getValue() {
// TODO - implement getValue()
return 'x';
}
// TODO - Implement the toString() method inherited from the
// Object class. This should be implemented so that
// when a PlayingCard object is printed, it displays
// it's suit and value in the proper way. For example,
// the 9 of Hearts should display as 9H while the Queen
// of Diamonds should display as QD. Note that the 10
// of any suit should display as a 10 and not as an X,
// so the 10 of Hearts should display as 10H and not
// as XH.
@Override
public String toString() {
return "THIS IS A TEST";
}
Explanation / Answer
// only need to modify PlayingCard.java
public class PlayingCard implements Comparable {
private char suit;
private char value;
public PlayingCard(char suit, char value) {
this.suit = suit;
this.value = value;
// This constructor should create the playing card
// Note that there are no mutators in this class - once
// a card is created its values never change
}
public char getSuit() {
return suit;
}
public char getValue() {
return value;
}
// TODO - Implement the toString() method inherited from the
// Object class. This should be implemented so that
// when a PlayingCard object is printed, it displays
// it's suit and value in the proper way. For example,
// the 9 of Hearts should display as 9H while the Queen
// of Diamonds should display as QD. Note that the 10
// of any suit should display as a 10 and not as an X,
// so the 10 of Hearts should display as 10H and not
// as XH.
@Override
public String toString() {
if (value == 'X')
return "10" + suit;
return value + "" + suit;
}
private int getInt() {
int v1 = 0;
if (value == 'A')
v1 = 14;
else if (value == 'K')
v1 = 13;
else if (value == 'Q')
v1 = 12;
else if (value == 'J')
v1 = 11;
else if (value == 'X')
v1 = 10;
else
v1 = value - '0';
return v1;
}
@Override
public int compareTo(Object obj)
{
PlayingCard c = (PlayingCard)obj;
return getInt() - c.getInt();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.