Here are the links for the skeletons http://web.cse.ohio-state.edu/cse2123/curre
ID: 3562375 • Letter: H
Question
Here are the links for the skeletons
http://web.cse.ohio-state.edu/cse2123/currentsem/closedlabs/ClosedLab04.code/CardDeck.java
http://web.cse.ohio-state.edu/cse2123/currentsem/closedlabs/ClosedLab04.code/TestCardDeck.java
http://web.cse.ohio-state.edu/cse2123/currentsem/closedlabs/ClosedLab04.code/CardGameTest.java
public class PlayingCard implements Comparable {
// TODO - Setup private variables here
private char suit;
private char value;
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
this.suit = suit;
this.value = value;
}
public char getSuit() {
// TODO - Implement getSuit()
return this.suit;
}
public char getValue() {
// TODO - implement getValue()
return this.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 (this.value == 'X') {
return "10" + this.suit;
}
return this.value + " " + this.suit;
}
// TODO - SEE EXERCISE 3. Do not try to implement this
// method until you reach EXECRISE 3
@Override
public boolean equals(Object obj) {
boolean result = true;
if (!(obj instanceof PlayingCard)) {
result = false;
}
return result;
}
@Override
public int compareTo(PlayingCard arg0) {
// TODO SEE EXERCISE 3. Do not try to implement this
// method until you reach EXERCISE 3
return 0;
}
}
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();
}
}
For Exercise 2, you need to implement the methods to get TestCardDeck to run properly. First implement a constructor that will create an empty deck of playing cards. You should decide how to store a deck of playing cards as a data structure - I suggest using an ArrayList in the code, but if you have a better idea you can try it. Next implement a toString() method that displays a list of cards to the console. If you have used an ArrayList for your deck, you can try just returning the result of calling the ArrayList's toString() method and see what it does. Next implement remainingCards() and newDeck(). Note that after you implement newDeck() your test code will give an infinite loop (why is that?) until you get dealNext() working properly. You do not need to implement shuffle() yet.
Exercise 3
Now that you have the basic PlayingCard and CardDeck classes working properly, we need to extend them a bit to be able to use them to play games. You will first need to implement some extensions to the PlayingCard class to allow us to compare cards to each other. Then you will need to modify CardDeck to allow us to shuffle the cards.
Look at the methods equals() and compareTo() in the PlayingCard class. equals() is inherited from the Object class, and so you will need to override this method with your own. The first part of this method is already written for you - it checks to see if the Object passed in as a parameter is of the PlayingCard class (see the discussion on instanceof in section 9.5.3 in the textbook). If two objects have different classes, they cannot be equal and so this method will return false.
You need to modify the equals() method to properly return true ONLY when the suit and the value on the two cards match. To do this, you will need to cast your object of type Object into an object of type PlayingCard. The following line of code will do that:
PlayingCard p1 = (PlayingCard) obj;
Once you have done that, implement the logic to check whether the two playing cards are equal and return true only when they have the same suit and value.
Write a test program to test a variety of values for equality. Make sure you come up with a number of test cases, including a number of cards that should be equal and should not. You may modify TestPlayingCard.java for these cases, or write a completely different program for them.
Next you need to modify compareTo(). The compareTo() method is inherited from the Comparable interface (we'll find out more about this interface in the coming weeks). You need to implement it so that it correctly follows the rules for Comparable objects:
For this assignment, playing card are first checked on value - a higher card beats a lower card regardless of suit, and Ace is the highest card. Ace beats King, King beats Queen, Queen beats Jack, Jack beats 10, 10 beats 9 and so on. If two cards have the same value, then the suit of the card is considered. Spades beat Hearts, Hearts beat Diamonds, Diamonds beat Clubs. So a 10 of Spades will have a higher value than a 10 of Hearts, and a Queen of Hearts has a higher value than a Queen of Clubs (and so on).
Write a test program to test a variety of values for comparison. Make sure you come up with a number of test cases, including a number of cards that should be equal, of lower value and of higher value. You may modify TestPlayingCard.java for these cases, or write a completely different program for them.
Next you should implement shuffle() for your CardDeck class. If you used an ArrayList for this method you can use the Collections.shuffle() method as part of your implementation. Modify TestCardDeck.java to provide some test cases showing that you have correctly implemented the shuffle method.
Once you have that finished, here are some further extensions/modifications:
Explanation / Answer
public class Card
{
public final static int S = 0; // Codes for the 4 suits, plus Joker.
public final static int H = 1;
public final static int D = 2;
public final static int C = 3;
public final static int J = 4;
public final static int ACE = 1; // Codes for the non-numeric cards.
public final static int JACK = 11; // Cards 2 through 10 have their
public final static int QUEEN = 12; // numerical values for their codes.
public final static int KING = 13;
/**
* This card's suit, one of the constants SPADES, HEARTS, DIAMONDS,
* CLUBS, or JOKER. The suit cannot be changed after the card is
* constructed.
*/
private final int suit;
/**
* The card's value. For a normal card, this is one of the values
* 1 through 13, with 1 representing ACE. For a JOKER, the value
* can be anything. The value cannot be changed after the card
* is constructed.
*/
private final int value;
/**
* Creates a Joker, with 1 as the associated value. (Note that
* "new Card()" is equivalent to "new Card(1,Card.J)".)
*/
public Card()
{
suit = J;
value = 1;
}
/**
* Creates a card with a specified suit and value.
* @param theValue the value of the new card. For a regular card (non-joker),
* the value must be in the range 1 through 13, with 1 representing an Ace.
* You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING.
* For a Joker, the value can be anything.
* @param theSuit the suit of the new card. This must be one of the values
* Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER.
* @throws IllegalArgumentException if the parameter values are not in the
* permissible ranges
*/
public Card(int theValue, int theSuit)
{
if (theSuit != S && theSuit != H && theSuit != D &&
theSuit != C && theSuit != J)
throw new IllegalArgumentException("Illegal playing card suit");
if (theSuit != J && (theValue < 1 || theValue > 13))
throw new IllegalArgumentException("Illegal playing card value");
value = theValue;
suit = theSuit;
}
/**
* Returns the suit of this card.
* @returns the suit, which is one of the constants Card.SPADES,
* Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER
*/
public int getSuit()
{
return suit;
}
/**
* Returns the value of this card.
* @return the value, which is one of the numbers 1 through 13, inclusive for
* a regular card, and which can be any value for a Joker.
*/
public int getValue()
{
return value;
}
/**
* Returns a String representation of the card's suit.
* @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs"
* or "Joker".
*/
public String getSuitAsString()
{
switch ( suit )
{
case S: return "S";
case H: return "H";
case D: return "D";
case C: return "C";
default: return "J";
}
}
/**
* Returns a String representation of the card's value.
* @return for a regular card, one of the strings "Ace", "2",
* "3", ..., "10", "Jack", "Queen", or "King". For a Joker, the
* string is always numerical.
*/
public String getValueAsString()
{
if (suit == J)
return "" + value;
else {
switch ( value )
{
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
default: return "King";
}
}
}
/**
* Returns a string representation of this card, including both
* its suit and its value (except that for a Joker with value 1,
* the return value is just "Joker"). Sample return values
* are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades",
* "Joker", "Joker #2"
*/
public String toString()
{
if (suit == J)
{
if (value == 1)
return "J";
else
return "J #" + value;
}
else
return getValueAsString() + " of " + getSuitAsString();
}
} // end class Card
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.