By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Need Help on Java Calculation Card Game!!!!!!

P: 5
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me.
Thanks!!
-----------------------------------------------------------------------
The code for the Stock class:
-----------------------------------------------------------------------
public class Stock
{
private Deck deck = new Deck(52);
private int X;
private int Y;

public Stock(Deck deck)
{
this.deck = deck;
}


/**
* Draw the stock as a facedown card and then the top card of
* the stock slightly to the right of and below the face-up card.
*/
public void draw(java.awt.Graphics g)
{

}

/**
* 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()
{
return ?????????????
}

/**
* 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
{
return ?????????
}

}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
in order to solve the problems you need to know Deck class too, but we can't modify this class as assignment requires.

-----------------------------------------------------------------------
The code for Deck class:
-----------------------------------------------------------------------

import java.util.*;
import java.awt.*;

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);
}

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);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
so basically my problems are how to get public Card top() and public Card takeTop() methods working in the Stock class without modify the Deck class.
Thanks!!!!!!!!! :)
May 16 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.