Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems defining a method

Status
Not open for further replies.

gemann

Technical User
May 21, 2002
9
CA
Hi, I have an interesting problem on my hands here. When I define the method :
Code:
public Card addCardBackInHand()
  {
    return (Card)hand.push();
  }

I then get this javac error :
Code:
C:\java\War>javac TryDeal.java
.\Hand.java:30: push(java.lang.Object) in java.util.Stack cannot be applied to (
)
    return (Card)hand.push();
                     ^
1 error

But if I add a object to the method to look like this :
Code:
public Card addCardBackInHand()
  {
    return (Card)hand.push(card);
  }

Then I get this error :
Code:
C:\java\War>javac TryDeal.java
.\Hand.java:30: cannot resolve symbol
symbol  : variable card
location: class Hand
    return (Card)hand.push(card);
                           ^
1 error

I'm not sure what to do next?
***********************************************************
Here is what my classes look like :

Code:
import java.util.*;


class Hand
{

  private Stack hand = new Stack();    


  public void add(Card card)
  {
    hand.push(card);
  }


// This method pulls a single card from the Hand.

  public Card getCard()
  {
    return (Card)hand.pop();
  }    

  public Card addCardBackInHand()
  {
    return (Card)hand.push(card);
  }


  public String toString()
  {
    Iterator cards = hand.iterator();

    StringBuffer str = new StringBuffer();
    while(cards.hasNext())
      str.append(" "+ (Card)cards.next());
    return str.toString();
  }  
  
}
**********************************************************
Code:
class TryDeal
{
  public static void main(String[] args)
  {
    CardDeck deck = new CardDeck();
    deck.shuffle();

    Hand myHand = deck.dealHand(5);
    Hand yourHand = deck.dealHand(5);
  

    Card myCard   = (Card)myHand.getCard();
    Card yourCard = (Card)yourHand.getCard(); 
    
    System.out.println("\nYour hand is"+yourCard);
    System.out.println("\nMy hand is"+myCard);
   }
}

 
public Card addCardBackInHand()
{
return (Card)hand.push(card);
}

The compiler can't figure out where card comes from. It's not a static or class variable, therefore it would have to be passed as an argument to the method to compile:

public Card addCardBackInHand(Card card)
{
return (Card)hand.push(card);
}

but that doesn't make much sense to me... you have a method to add a Card, and a method to get the last Card... when would you use that method?

HTH

BTW, you wouldn't happen to be coding a blackjack game, would you?
 
Hi daniel135,
Thanks for the help I'll give it a try. The game I'm trying to code is the card game War. The reason I need the method addCardBackInHand(), is because in the game two players are drawing one card each and displaying them the player with the highest card wins and then I want to take those 2 cards and add them back into the hand of the winning player.

again I want to thank you for all your valuable help..gemann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top