A standard deck of playing cards consists of 52 cards. Each card has a rank (2,
ID: 3918957 • Letter: A
Question
A standard deck of playing cards consists of 52 cards. Each card has a rank (2, 3, . . . , 9, 10, Jack, Queen, King, or Ace) and a suit (spades m, hearts n, clubs p, or diamonds o).
You will create a class called StandardCard that will simulate cards from a standard deck of cards. Your class will extend the Card class (provided).
The ordering of the cards in a standard deck (as defined for this assignment) is first specified by the suit and then by rank if the suits are the same. The suits and ranks are ordered as follows:
suits: The suits will be ordered
diamonds o < clubs p < hearts n < spades m
ranks: The ranks will be ordered
2 < 3 < · · · < 9 < 10 < Jack < Queen < King < Ace
A Joker card is a special card that is “greater” than any other card in the deck (any two jokers are equal to each other). A joker has no suit “None” from Card.SUITS.
Again, the overall ordering for non-joker cards is specified by suit first and then rank; for example, all club cards are “less than” all heart cards. Two cards with the same rank and suit are considered equal.
2 First
Next, write a Java class called StandardCard that extends the provided Card class. Your class must have two constructors:
Submit a single file called assignment3-ID.zip to cuLearn. Here ID is your student ID number.
Modify the Card class by adding appropriate protected attributes to store the state (suit/rank) of a card.
COMP1006/1406 - Summer 2018 1
Assignment #3 Due Monday, July 30 at 5:30 pmNote that the case of strings is important here. The input strings must be exactly the same as
those found in Card.SUITS or Card.RANKS.
The specification for the three abstract methods declared in the Card class are given by:
Be sure to add attributes to the abstract Card class. You can add any (non-static) at- tributes and helper methods that you need for your StandardCard class.
Example:
12
?????
"Queen"
"Diamonds"
displays
12D
evaluates to
????????
evaluates to
????????
evaluates to
????????
evaluates to
????????
evaluates to
????????
evaluates to
????????
11
0
"Spades"
"J"
"Joker"
returns
????? ?????
returns returns
?????
displays
?????
toString() method has been updated to output J for Joker
????? ?????
returns returns
?????
COMP1006/1406 - Summer 2018
2
returns
1
"None"
evaluates to
????????
////// Card Class/////
Explanation / Answer
here is your classes : --------->>>>>>>
Card.java : --------------------------------------------------
public abstract class Card implements Comparable<Card>{
/* handy arrays for ranks and suits */
/* do NOT change these */
public final static String[] RANKS = { "None", "Joker",
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
public final static String[] SUITS = { "Diamonds",
"Clubs", "Hearts", "Spades", "NONE"};
protected int rank;
protected int suit;
/** creates a card with specified suit and rank
*
* @param suit is the suit of the card (must be a string from Card.SUITS)
* @param rank is the rank of the card (must be a string from Card.RANKS)
*/
public Card(String suit, String rank){
// add code here if needed
boolean st = true;
for(int i = 0;i<Card.RANKS.length;i++){
if(rank.equals(Card.RANKS[i])){
if(i == 0 || i == 1){
this.rank = i;
this.suit = 4;
st = false;
break;
}else{
this.rank = i;
break;
}
}
}
if(st){
for(int i = 0;i<Card.SUITS.length;i++){
if(suit.equals(Card.SUITS[i])){
if(i == 4){
this.rank = 0;
this.suit = 4;
break;
}else{
this.suit = i;
break;
}
}
}
}
}
/** the numerical representation of the rank of the current card
* <p>
* ranks have the numerical values
* Joker = 1,
* 2 = 2, 3 = 3, ..., 10 = 10
* Jack = 11, Queen = 12, King = 13, Ace = 14
*
* @return the numerical rank of this card
*/
public abstract int getRank();
/** the string representation of the rank of the current card
*
* @return the string representation of the rank of this card (must be a string from Card.RANKS)
*/
public abstract String getRankString();
/** the suit of the current card
*
* @return the suit of this card (must be a string from Card.SUITS)
*/
public abstract String getSuit();
@Override
public final String toString(){
// outputs a string representation of a card object
int r = getRank();
if( r >= 2 && r <= 14 ){
return r + getSuit().substring(0,1);
}
if (r == 1){
return "J";
}
return "invalid card";
}
}
StandardCard.java : ------------------------------
public class StandardCard extends Card{
public StandardCard(String rank,String suit){
super(suit,rank);
}
public StandardCard(int rank,String suit){
super(suit,Card.RANKS[rank]);
}
public int getRank(){
return rank;
}
public String getRankString(){
return Card.RANKS[rank];
}
public String getSuit(){
return Card.SUITS[suit];
}
public int compareTo(Card card){
if(card instanceof StandardCard){
StandardCard c = (StandardCard)card;
if(c.getRank() == 1 && getRank() == 1){
return 0;
}
if(c.getRank() == 1 && getRank() != 1){
return -1;
}
if(getRank() == 1 && c.getRank() != 1){
return 1;
}
int s = 0;
for(int i = 0;i<Card.SUITS.length;i++){
if(c.getSuit().equals(Card.SUITS[i])){
s = i;
break;
}
}
if(s < this.suit){
return -1;
}
if(s > this.suit){
return 1;
}
if(s == this.suit){
if(this.rank < c.getRank()){
return -1;
}
if(this.rank > c.getRank()){
return 1;
}
if(this.rank == c.getRank()){
return 0;
}
}
}
return -2;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.