//Card Class public abstract class Card implements Comparable<Card>{ /* handy ar
ID: 3918652 • Letter: #
Question
//Card Class
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"};
/** 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
* <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";
}
}
//Deck class
import java.util.List;
/* finish all of the constructors and methods given here */
/* do NOT add any public attributes or methods */
/* add any protected/private attributes you need */
public class Deck{
public Deck(){}
public Deck(int nun_jokers){}
public List<Card> getCards(int num_cards){return null;}
public Card getCard(){return null;}
public void addCard(Card c){}
}
//Hand class
import java.util.List;
public class Hand{
protected List<Card> cards;
public Hand(List<Card> cards){
this.cards = cards;
}
public int numberOfCards(){
if( this.cards == null ){
return -1;
}else{
return this.cards.size();
}
}
public List<Card> getCards(){ return this.cards; }
/* remove and return the specified card from the hand */
/* return null if the card is not in the hand */
public Card remove(Card card){return new StandardCard(Card.SUITS[4], Card.RANKS[1]);}
/* add the specified card to the hand */
public void add(Card card){}
}
//Player class
public abstract class Player{
protected Hand hand;
public Player(Hand hand){ this.hand = hand; }
/* play a card from the player's hand */
/* returns null if they cannot play a card and the deck becomes empty */
/* do NOT return null unless you cannot play a card and the deck is empty */
public abstract Card play(Card top_of_discard_pile, Deck deck);
public final int cardsLeft(){ return this.hand.numberOfCards(); }
}
Explanation / Answer
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StartCardGame {
public enum SUITE {
SPADE,
HEARTS,
CLUB,
DIAMOND
}
static String[] rank = new String[] {
"2", "3", "4",
"5", "6", "7", "8", "9", "10",
"JACK", "QUEEN", "KING","ACE"
};
public static void main(String[] args) {
List<Card> cardDeck = new ArrayList<Card>();
Card jockerCard=new Card("JOCKER","NONE");
jockerCard.rank=1;
cardDeck.add(jockerCard);
cardDeck.add(jockerCard);
for(SUITE suite : SUITE.values()){
for(int value = 0 ; value <rank.length ; value++){
if (rank[value].length() > 2) {
Card card=new Card(rank[value],suite.name());
card.rank=value+2;
cardDeck.add(card);
}
else
{
cardDeck.add(new Card(value+2,suite.name()));
}
}
}
Collections.sort(cardDeck, new Comparator<Card>() {
@Override
public int compare(Card card1, Card card2)
{
if (card1.rank==card2.rank && card1.SUITS.equalsIgnoreCase(card2.SUITS)) {
return 0;
}
if (card1.rank<card2.rank) return -1;
if (card1.rank>card2.rank) return 1;
return 1;
}
});
System.out.println("StartCardGame.main()"+cardDeck.size());
System.out.println("StartCardGame.main()"+cardDeck.toString());
}
}
##############################################################
package cardGame;
public abstract class AbstractCard {
public abstract int getRank();
public abstract String getRankString();
public abstract String getSuit();
}
##############################################################
package cardGame;
public class Card extends AbstractCard {
int rank;
String RANKS;
String SUITS;
public Card(int rank, String suits) {
this.rank = rank;
SUITS = suits;
}
public Card(String rank, String suits) {
RANKS = rank;
SUITS = suits;
}
@Override
public int getRank() {
return rank;
}
@Override
public String getRankString() {
return RANKS;
}
@Override
public String getSuit() {
return SUITS;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return RANKS==null?"("+rank+")"+SUITS:"("+RANKS+rank+")"+SUITS;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.