Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hand.java define a Hand class, that is a set of cards held by a player. This cla

ID: 3712229 • Letter: H

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 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

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Hand.java
--------


import java.util.ArrayList;

public class Hand
{
private ArrayList<Card> cards;

public Hand(int m)
{
cards = new ArrayList<Card>(m);
}

public Card drawNext() //draws "next" card from hand
{
if(cards.isEmpty())
throw new DeckOrHandEmptyException("Hand is empty");

return cards.remove(0);
}

public Card draw(String s)

//draws specific card from hand, in format "8s", "1c", "11h", "13d"
{
if(cards.isEmpty())
throw new DeckOrHandEmptyException("Hand is empty");

for(Card c : cards)
{
if(c.toString().equals(s))
{
cards.remove(c);
return c;
}
}
return null;
}
public void addCard(Card c) //adds card to hand
{
cards.add(c);
}

public ArrayList getCards() // gets the list of Cards in hand
{
return cards;
}
}

CardException.java
------------

public class CardException extends RuntimeException {

public CardException() {
}

public CardException(String message) {
super(message);
}

}


DeckOrHandEmptyException.java
------------------------------
public class DeckOrHandEmptyException extends CardException {

public DeckOrHandEmptyException() {
}

public DeckOrHandEmptyException(String message) {
super(message);
}

}

InvalidCardException.java
---------------------
public class InvalidCardException extends CardException {

public InvalidCardException() {
}

public InvalidCardException(String message) {
super(message);
}

}

CardPlayer.java
----------------
public abstract class CardPlayer {
private String name;
public CardPlayer(String name) //player name
{
this.name = 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()
{
return name;
}

}

CardGame.java
--------------
public abstract class CardGame {
private CardPlayer[] players;
public CardGame(CardPlayer [] players)
{
this.players = 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) */

}