The Card class Requirements: You are to write a class called Card-this will repr
ID: 3665762 • Letter: T
Question
The Card class Requirements: You are to write a class called Card-this will represent a playing card. Card should have the following: Data: Its data are the "rank" and "suit." The rank should be an int that is in the range 2-14 (2=2, 3=3,. 10=10, 11=Jack, 12=Queen, 13=King, 14=Ace). The suit should be a char that is either 'S', 'H', 'D', 'C', ' s', 'h', 'd', 'c', ('S'=Spade, 'H'=Heart, 'D'=Diamond, 'C'=Club in upper and lower case). Constructors: A default constructor that will set the values of the Ace of Spades. A parameterized constructor that will receive a rank and a suit, in that order. If either the rank or the suit is out of range, then: throw new IllegalArgumentException ("your descriptive String here"); A copy constructor that will receive a Card and initializes the data (of the new instance being created) to be the same as the Card that was received. Methods: A to String () method that returns a String representing the Card. The String it returns can just concatenate the rank and the suit (but it should change a rank of 1 or the ranks > 10 to what they mean). A rank of 11 would be a "J" that would then be concatenated to the suit). So a Card with rank of 13, suit of "S" would return "KS" in its to String(). If you want to show it, you could return it as "Ku0006". Character #3 is actually a picture of a heart, character #4 is a diamond, character #5 is a club, and character #6 is a spade. They will print if you output to the DOS window (so do not "Capture Output"). An equals method that returns true if what was received is equal to the current instance. Be sure that it follows the logic for equals exactly. A compare To method that receives another Card and returns a positive integer if the current instance is > the Card that was received, returns a negative integer if the current instance isExplanation / Answer
The problem statement is simple. You have to create an entity class represent Card( Playing cards' card).
it has two member variables rank and suit.
and it has a few methods like toString() and Euqals(). These two methods you need to override because there are the methods from Object class.
I have given you the program below along with test class(main) to check the same.
/**
*
* @author Krishna
*/
public class CardTest {
public static void main(String[] args) {
Card card1 = new Card(2, 'h');
System.out.println("card1 : "+card1); // bydefault it calls the toString() of this object i.e; card1 in this case.
Card card2 = new Card(2, 'S');
System.out.println("card2 :"+card2);
Card card3 = new Card(2, 'H');
System.out.println("card3 "+card3);
// equals method test
System.out.println("card1.equals(card2) : "+ card1.equals(card2));
System.out.println("card1.equals(card3) : "+ card1.equals(card3));
}
}
class Card implements Comparable<Card> {
int rank;
char suit;
// With rank and suit passed as parameters to Constructor
public Card(int rank, char suit) {
if(rank>=2 && rank<=14 && "SHDCshdc".contains(suit+"")){
this.rank = rank;
this.suit = Character.toUpperCase(suit);
} else {
throw new IllegalArgumentException("Either Rank or Suit is out range!!!");
}
}
// Default values are set Aces of Spade
public Card() {
this.rank = 14;
this.suit = 'S';
}
// Accepting card object as parameter and sets same values to current object
public Card(Card card) {
this.rank = card.rank;
this.suit = card.suit;
}
// Overriden compareTo method. For this, we need to implement Comparable interface. Plz see class declaration "class Card implements Comparable<Card> "
@Override
public int compareTo(Card card) {
return this.rank - card.rank;
}
// Equals method
@Override
public boolean equals(Object obj) {
Card card = (Card)obj;
return this.rank == card.rank && this.suit == card.suit;
}
// toString() method.
@Override
public String toString() {
return ""+rank+suit;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.