Question: Hand.java define a Hand class, that is a set of cards held by a player. This class also keeps an ArrayList of cards and should

Hand.java

define a Hand class, that is a set of cards held by a player. This class also keeps an ArrayList of cards and should support the following API:

public Hand(int m) //specifies initial size of hand

public Card drawNext() //draws "next" card from hand

public Card draw(String s)

//draws specific card from hand, in format "8s", "1c", "11h", "13d"

public void addCard(Card c) //adds card to hand

public ArrayList getCards() // gets the list of Cards in hand

Custom Exception classes

Define the following custom exception classes, and throw them at appropriate places in the preceding classes:

CardException extends RuntimeException

DeckOrHandEmptyException extends CardException

InvalidCardException extends CardException

CardPlayer.java

define the abstract class CardPlayer , with the following API:

public CardPlayer(String name) //player name

public abstract Card takeTurn()

//returns the card to be played, from the hand held by this player

public abstract void dealCard(Card card)

//adds a card to the hand held by this player

public String toString()

CardGame.java

define the abstract class CardGame, with the following API:

public CardGame(CardPlayer [] players)

public abstract void play()

/* loops through all players while game is not over, having each player take a turn until game is over, then uses getWinner to display winner */

public abstract boolean isGameOver()

/* check if game is over */

public abstract String getWinner()

/* returns the winner(s) */

Here is my code so far:

public enum Suit { //Defined in order of rank CLUBS, DIAMONDS, HEARTS, SPADES }

public enum Rank { TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);

private int cardRank;

private Rank (int value) { this.cardRank = value; }

public int getCardValue() { return cardRank; }

//Returns ENUM value for respective integer public static Rank getRank(int rankint) { for (Rank l : Rank.values()) { if (l.cardRank == rankint) return l; } throw new IllegalArgumentException("Leg not found. Amputated?"); } }

public class Card implements Comparable {

private Suit suit; private int rank;

//constructor public Card(int rank, Suit suit) { this.rank = rank; this.suit = suit; }

// To String method public String toString() { return Rank.getRank(rank) + " of " + suit.name(); }

// Overriding compareTo method @Override public int compareTo(Card o) {

if (this.suit == o.suit) { // suit is identical: compare number if (this.rank < o.rank) { return -1; } else if (this.rank > o.rank) { return 1; } else { return 0; } } else return this.suit.compareTo(o.suit); }

// Getters public int getRank() { return rank; }

public Suit getSuit() { return suit; }

}

import java.util.ArrayList; import java.util.Collections; import java.util.Iterator;

public class Deck { private ArrayList deck;

public Deck() { this.deck = new ArrayList();

//adding Cards to deck for (Rank card : Rank.values()) { for (Suit suit : Suit.values()) { deck.add(new Card(card.getCardValue(),suit)); } }

}

//Returning first card index 0 public Card deal() { return deck.get(0);

}

// TOString implementation @Override public String toString() { Iterator i = deck.iterator(); if (!i.hasNext()) return "...";

StringBuilder sb = new StringBuilder(); for (;;) { Card e = i.next(); sb.append(e.toString()); if (!i.hasNext()) return sb.toString(); sb.append(" "); } }

// returns true if deck is empty public boolean isDeckEmpty() { return deck.isEmpty(); }

// shuffles cards in deck public void shuffle() { Collections.shuffle(deck); }

//Sorting cards in deck public void sort() { Collections.sort(deck); } }

Any help will be much appreciated

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!