Question: Write a Java class called StandardCard that extends the provided Card class. Your class must have two constructors: public StandardCard(String rank, String suit) // purpose:
Write a Java class called StandardCard that extends the provided Card class. Your class must have two constructors:
public StandardCard(String rank, String suit) // purpose: creates a card with given rank and suit // preconditions: suit must be a string found in Card.SUITS // rank must be a string found in Card.RANKS // Note: If the rank is Card.RANKS[15] then any // valid suit from Card.SUITS can be given // but the card's suit will be set to Card.SUITS[4]
public StandardCard(int rank, String suit) // purpose: creates a card with the given rank and suit // preconditions: suit must be a string found in Card.SUITS // rank is an integer satisfying 1 <= rank <= 14, where // 1 for joker, 2 for 2, 3 for 3, .., 10 for 10 // 11 for jack, 12 for queen, 13 for king, 14 for ace // Note: as with the other constructor, if a joker is created, any valid suit can be passed // but the card's suit will be set to Card.SUITS[4] Note 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 specifcation for the three abstract methods declared in the Card class are given by: public int getRank() // Purpose: Get the current card's rank as an integer // Output: the rank of the card // joker -> 1, 2 -> 2, 3 -> 3, ..., 10 -> 10 // jack -> 11, queen -> 12, king -> 13, ace -> 14
public String getRankString() // Purpose: Get the current card's rank as a string // Returns the cards's rank as one of the strings in Card.RANKS // (whichever corresponds to the card)
public String getSuit() // Purpose: Get the current card's suit // Returns the card's suit as one of the strings in Card.SUITS // (whichever corresponds to the card) Do not change the abstract Card class. You can add any (non-static) attributes and helper methods that you need for your StandardCard class.
THIS IS THE CODE FOR THE CARD CLASS:
public abstract class Card implements Comparable{ /* 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"}; /** 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 } /** the numerical representation of the rank of the current card ** 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"; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
