The code for Stock class:
In order to understand Stock class you need Deck class as well, but we’re not allowed to modify this class as assignment required.Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.awt.*;
- /**
- * The Stock is the deck of unused cards. It is subtly different from the deck
- * itself in several ways. Firstly, it does not come into existence until after
- * the four cards have been extracted from the deck to form the start of the
- * foundation. Secondly, the deck is essentially just a list of cards, while
- * the stock consists of an upturned top card plus the rest of the deck.
- * Thirdly, the deck does not have any graphical representation whereas the stock
- * provides methods that allow the user to interact with it via the mouse. At all
- * times, the top card on the stock is displayed face up, offset from the rest of
- * the stock which is displayed as a single face-down card.
- */
- public class Stock
- {
- public java.util.List<Card> cards;
- private Deck deck = new Deck(52);
- private Card topCard;
- /**
- * Constructor for objects of class Stock, when they are
- * not going to be displayed on a Graphics
- * @param deck the deck of cards used to make Stock
- */
- public Stock(Deck deck)
- {
- this.deck = deck;
- }
- X = x;
- Y = y;
- }
- /**
- * Add the given card to the end of the stock of cards.
- */
- public void addAtEnd(Card card)
- {
- cards.add(card);
- }
- /**
- * The number of cards in the stock(including the top card).
- * @return the number of cards.
- */
- public int numCardsRemaining()
- {
- return deck.numCardsRemaining();
- }
- /**
- * Take the top card from the stock.
- * @return the card that was on top of the stock or null if the stock was empty.
- * A call to this method reduces the number of cards remaining in the stock by one.
- */
- public Card takeTop()
- {
- if(deck.numCardsRemaining()==0)
- {
- return null;
- }
- else
- {
- return this.deck.nextCard();
- }
- }
- /**
- * The top card of the stock.
- * @return the top card of the stock, or null if the stock is empty.
- * Acall to this method does not remove the card from the top but
- * just returns a reference to it.
- */
- public Card top()
- {
- if(deck.numCardsRemaining()==0)
- {
- return null;
- }
- else
- {
- Card topCard = deck.nextCard();
- deck.addAtStart(topCard);
- }
- return topCard;
- }
- }
- }
So, my problem is in my “public Card top()” method in the “Stock” class, I defined 'topCard' as 'deck.nextCard()', then put it back, and return it, but it seems not right, can someone help me on this. As I’m just learning Java a month ago, please excuse me if I make no sense.Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.awt.*;
- /**
- * A full deck of cards. It is shuffled when first constructed
- * and thereafter cards are removed from the deck by calls to
- * nextCard until no further cards remain. Cards can also be
- * placed back on the deck at either the start or the end.
- */
- public class Deck
- {
- private java.util.List<Card> cards; // The list of cards
- private static java.util.Random generator = null;
- /**
- * Constructor for a shuffled deck of cards.
- * @param seed the seed for the random number generator used to
- * shuffle the deck. A different unique shuffle is obtained for
- * each possible value of seed.
- */
- public Deck(long seed)
- {
- // Create a deck of cards in sorted order of suit then pip count.
- cards = new ArrayList<Card>();
- for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
- for (int pips = 1; pips <= Card.CARDS_IN_SUIT; pips++) {
- cards.add(new Card(suit, pips));
- }
- }
- // Now shuffle the deck, using a random number generator based
- // on the seed value given as a parameter.
- java.util.Random generator = new java.util.Random(seed);
- java.util.Collections.shuffle(cards, generator);
- }
- /**
- * The number of cards still remaining in the deck.
- * @return the number of cards left in the deck.
- */
- public int numCardsRemaining()
- {
- return cards.size();
- }
- /**
- * Get the next card from the deck, reducing the number of cards
- * remaining in the deck by one.
- * @return the "top" card in the deck
- */
- public Card nextCard()
- {
- return cards.remove(0);
- }
- /**
- * Add the given card to the start (front) of the current deck.
- * @param card the card to be placed at the start of the deck
- */
- public void addAtStart(Card card)
- {
- cards.add(0, card);
- }
- /**
- * Add the given card to the end (back) of the current deck.
- * @param card the card to be placed on the end of the deck
- */
- public void addAtEnd(Card card)
- {
- cards.add(card);
- }
- }
Thanks!
:D