I need some help with a lab. Below is my code and the API it should follow. You
ID: 3710672 • Letter: I
Question
I need some help with a lab. Below is my code and the API it should follow. You can add private methods and insatnce variables but it must follow that API.
Hand.java
Then define a Handclass, 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"
//for reference “8s” = 8 of spades, “1c” = ace of clubs, “11h” = 11 of hearts
public void addCard(Card c) //adds card to hand
public ArrayList<Card> getCards() // gets the list of Cards in hand
this is what i have so far, but im looking for some direction. Thanks in advance
import java.util.*; ublic class Hand private ArrayList Card> hand; //the cards in the hand /Ispecifies the initial size of the hand public Hand(int m){ hand new ArrayList(m) public void addCard(Card c) if (c == null){ throw new CardException (); hand.add(c): public Card drawNext)( k/Explanation / Answer
Please find the Hand class code below with detailed inline comments.
CODE
=====================
class Card {
String face, suit;
public Card(String f, String s) {
face = f;
suit = s;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return face + " of " + suit;
}
}
class Hand {
private ArrayList<Card> hand;
public Hand(int m) {
hand = new ArrayList<Card>(m);
}
public void addCard(Card c) {
if (c == null)
throw new CardException();
hand.add(c);
}
public Card drawNext() {
return hand.remove(0);
}
public Card draw(String s) {
String face = s.substring(0, s.length()-2);
String suit = s.substring(s.length() - 1);
Card c = new Card(face, suit);
if(hand.contains(c))
hand.remove(c);
return c;
}
}