0

I'm not sure why I am getting this output. when I try to print a hand of cards for my poker program. I know there are lots of things I could use to make this more efficient like enums. but this is for a class so I am limited on what I can use. I have never worked with the Collections.Shuffle in my Deck class, so I'm not sure that I did it right. Same with the override in my card class.

Any suggestions on how to get this program to print out 5 cards?

Card:

public class Card 
{


    String suits;
    String values;

    public Card(int suit, int value)
    {
        if (suit == 0)
        {
            suits = "Spades";
        }
        else if (suit == 1)
        {
            suits = "Clubs";
        }
        else if (suit == 2)
        {
            suits = "Hearts";
        }
        else 
        {
            suits = "Diamonds";
        }

        if (value == 0)
        {
            values = "2";
        }
        else if (value == 1)
        {
            values = "3";
        }
        else if (value == 2)
        {
            values = "4";
        }
        else if (value == 3)
        {
            values = "5";
        }
        else if (value == 4)
        {
            values = "6";
        }
        else if (value == 5)
        {
            values = "7";
        }
        else if (value == 6)
        {
            values = "8";
        }
        else if (value == 7)
        {
            values = "9";
        }
        else if (value == 8)
        {
            values = "10";
        }
        else if (value == 9)
        {
            values = "Jack";
        }
        else if (value == 10)
        {
            values = "Queen";
        }
        else if (value == 11)
        {
            values = "King";
        }
        else
        {
            values = "Ace";
        }
    }
    @Override
    public String toString()
    {
        return values + " of " + suits;
    }

}

Deck:

import java.util.Arrays;
import java.util.Collections;
public class Deck 
{
    Card[] deck = new Card[52];

    public Deck()
    {
        int element;
        for(int iSuit = 0; iSuit < 4; iSuit++)
        {
            for(int iValue = 0; iValue < 13; iValue++)
            {
                element = iSuit * 13 + iValue;
                deck[element] = new Card(iSuit, iValue);
            }
        }
    }

    public void shuffle()
    {
        Card[] newDeck = new Card[52];
        int element = (int) (Math.random()*52);

        Collections.shuffle(Arrays.asList(deck[element]));

    public Card dealCard(int card)
    {
        return deck[card];
    }
}

Hand:

public class Hand 
{
    Card[] hand = new Card[5];

    public Hand(Deck deck)
    {
       int element;
       for(int card = 0; card < 4; card++)
        {
            hand[card]=deck.dealCard(card);
        }
    }
}

Main:

public class FiveCardPoker 
{

    public static void main(String[] args)
    {

        Deck timsDeck = new Deck();

        timsDeck.shuffle();

        Hand timsHand = new Hand(timsDeck);

        System.out.println(timsHand);


    }

}

user3769297
  • 179
  • 1
  • 2
  • 12

5 Answers5

2

Since you have not overriden toString() it takes the implementation from Object class which is implicit superclass of all classes.

public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

Hand doesn`t override the toString method of Object. So you get the standard toString method. Override the toString methode with something like that:

public String toString() {
  StringBuffer sb = new StringBuffer();
   for(int card = 0; card < 4; card++)
        {
            sb.append(hand[card]+",")
        }
  return sb.toString()
}
Jens
  • 67,715
  • 15
  • 98
  • 113
1

The output is not random.. it is: ClassName.ItsHashCode;. You have to override toString() for Hand class and display its contents / message(s).

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Override toString() method in Hand class this will remove your error. return the String value from toString() method i.e; what you want to print when the Hand Class's object is being printed.

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
0

Since you haven't overridden toString() in Hand it uses default implementation from Object
ref: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

Any suggestions on how to get this program to print out 5 cards?

You can either override toString() in Hand like:

        @Override
        public String toString() {
            //format according to your requirements
            return Arrays.asList(hand).toString();
        }

or create a separate method in class Hand to show cards:

    public void printCards() {
        //format according to your requirements
        System.out.println(Arrays.asList(hand));
    }

and use it in your main like:

timsHand.printCards();
Shailesh Aswal
  • 6,632
  • 1
  • 20
  • 27
  • Using `toString()` is probably more standard in this case, since using some random helper method that needs to be invoked in lieu of `toString` when printing feels wonky. – Makoto Jul 18 '14 at 07:13
  • `probably` says it all, that's up to one's convenience. May be good for logging etc. using helper is not prohibited, it can be an option. may be you can correct me. – Shailesh Aswal Jul 18 '14 at 07:19