Note that there are some explanatory texts on larger screens.

plurals
  1. POIm trying to figure out how to deal a card from the deck in my card game class
    primarykey
    data
    text
    <p>Im finding this hard since im not allowed to use arrays for the Deck class instead i have to create an object for it like <code>private Deck deck;</code>(you 'll see what i mean in the code)</p> <p>This method takes a card from the deck and adds it to the player's hand.</p> <p>Heres the Card, Deck, Game Class</p> <pre><code>public class Card { public static final String HEARTS = "hearts"; public static final String DIAMONDS = "diamonds"; public static final String CLUBS = "clubs"; public static final String SPADES = "spades"; private String description; private String suit; private int value; public Card() { description = "Joker"; suit = "Spades"; value = 0; } /** * Constructor for objects of class Card * @param description e.g. "Ten" * @param suit e.g. "Hearts" * @param value e.g. 10 */ public Card(String description, String suit, int value) { setDescription(description); setSuit(suit); setValue(value); } /** * Gets the suit. * @return suit as a String */ public String getSuit() { return suit; } /** * Gets the value. * @return value as an int */ public int getValue() { return value; } /** * Gets the description. * @return description as a String */ public String getDescription() { return description; } /** * Sets the suit * @param suit e.g."Hearts" */ public final void setSuit(String suit) { if((suit != null)&amp;&amp; (suit.equalsIgnoreCase(HEARTS)) || (suit.equalsIgnoreCase(DIAMONDS)) || (suit.equalsIgnoreCase(CLUBS))|| (suit.equalsIgnoreCase(SPADES))){ this.suit = suit; } else { this.suit = "unknown suit"; } } /** * Sets the description * @param description e.g. "Ten" */ public final void setDescription(String description) { if(description != null) { this.description = description; } else { this.description = "unknown description"; } } /** * Sets the value * @param value of this card e.g. 10 */ public final void setValue(int value) { if(value &gt; 0 &amp;&amp; value &lt;= 10) { this.value = value; } else { this.value = 0; } } /** * @return string containing description and suit */ public String getInfo() { return description + " of " + suit; } } </code></pre> <p>Deck Class:</p> <pre><code>import java.util.ArrayList; /** * Deck of cards. * * * */ public class Deck { private ArrayList&lt;Card&gt; deck; /** * Constructor for objects of class Deck * Creates a new container for Card objects */ public Deck() { deck = new ArrayList&lt;Card&gt;(); } /** * Add a card to the deck. * @param Card to be added */ public void addCard(Card cardToAdd) { deck.add(cardToAdd); } /** * Take the first card from the deck. * @return Card or null */ public Card takeCard() { if(deck.isEmpty()) { return null; } else { Card top = deck.get(0); deck.remove(0); return top; //return deck.remove(0); // get the top card } } /** * Show the contents of the deck. */ public void showDeck() { for(Card eachCard : deck) { System.out.println(eachCard.getDescription()+ " of " + eachCard.getSuit()); } } /** * Get the size of the deck * @return deck size */ public int getDeckSize() { return deck.size(); } } </code></pre> <p>Heres where im a little stuck. also with my </p> <pre><code>public void showHand() is getting a java.lang.NullPointerException at Game.showHand(Game.java:31) </code></pre> <p>Game Class:</p> <pre><code> import java.util.ArrayList; /** * Write a description of class Game here. * * @author (your name) * @version (a version number or a date) */ public class Game { private InputReader reader; private Deck deck; private ArrayList&lt;Card&gt; hand; private static final int MAX = 5; public Game() { reader = new InputReader(); deck = new Deck(); hand = new ArrayList&lt;Card&gt;(); } public void dealCard() { //&lt;---- not sure what to do with this one } } public void showHand()//&lt;----- not sure if this one is even right { //compiles fine but when i call the method it gets // for(Card eachCard: hand){ System.out.println(eachCard.getDescription()+ " of " + eachCard.getSuit()); } } } </code></pre> <p>Any help appreciated!! p.s i know how to add a card from the Card class but not sure how to do it from the Deck class</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload