Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Hand {
  4. /* declare a private array list of type Card called cards */
  5. private ArrayList<Card> cards;
  6. /*
  7. * Instantiate the cards as a new arraylist.
  8. */
  9. public Hand() {
  10. cards = new ArrayList<Card>();
  11. }
  12.  
  13. /*
  14. * Add card to cards at index 0 (how do you add to an ArrayList at a given index?)
  15. */
  16. public void addCard(Card card) {
  17. cards.add(0, card);
  18. }
  19.  
  20. /*
  21. * Returns the card from the top of the hand meaning, return the card at
  22. * cards.size() - 1.
  23. * Make sure you use the ArrayLists' remove method since we are removing the card from our hand!
  24. * If the size is 0, return null instead
  25. */
  26. public Card playCard() {
  27. if( cards.size() == 0 ){
  28. return null;
  29. }else{
  30. return cards.remove( cards.size() - 1 );
  31. }
  32. }
  33.  
  34. /*
  35. * Return the size of the ArrayList
  36. */
  37. public int totalInHand() {
  38. return cards.size();
  39. }
  40.  
  41. @Override
  42. public String toString() {
  43. String retVal = "Currently in hand: ";
  44. for (Card c : cards) {
  45. retVal += c.getShortName() + ", ";
  46. }
  47. return retVal;
  48. }
  49.  
  50. public static void main(String[] args) {
  51. Card c = new Card(0, 0);
  52. Hand h = new Hand();
  53. h.addCard(c);
  54. h.addCard(new Card(0, 1));
  55. h.addCard(new Card(0, 2));
  56. System.out.println(h);
  57. System.out.println(h.totalInHand());
  58. System.out.println(h.playCard());
  59. System.out.println(h);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement