0

How can i improve on my code such that it would not produce hash values when i call my print method? I reckon the @2a.... etc behind the Card keyword are hash values.

My codes produce the following output when i call my method to print them out:

run:
card.Card@2a139a55
card.Card@15db9742
card.Card@6d06d69c
card.Card@7852e922

My code:
public class Card {

    /**
     * @param args the command line arguments
     */
    static String[] rank = {"2","3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    static String[] suit = {"Spades","Hearts", "Clubs", "Diamonds"};

    public Card(String r, String s)
    {


    }

    public static void init(Card[] deck)
    {
        for(int x = 0; x<deck.length; x++)
        {
            Card newCard = new Card(rank[x%13], suit[x/13]);
            deck[x] = newCard;
        }
    }

    public static void swap(Card[] deck, int a, int b)
    {
        Card temp = deck[a];
        deck[a] = deck[b];
        deck[b] = temp;
    }

    public static void shuffle(Card[] deck)
    {
        Random rnd = new Random();
        for(int x = 0; x<deck.length; x++)
        {
            swap(deck, x, (rnd.nextInt(deck.length)));
        }
    }

    public static void print(Card[] deck)
    {
        for(int x = 0; x<deck.length; x++)
            System.out.println(deck[x]);
    }


    public static void main(String[] args) {
        // TODO code application logic here
        Card[] deck = new Card[52];
        init(deck);
        print(deck);
    }
Aloysius
  • 29
  • 5

1 Answers1

0

You should override the toString() method of your class Card.

public String toString()
{
return "I want to print this";
}
Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49
  • can you elaborate abit more? – Aloysius Jun 29 '16 at 11:42
  • 1
    This question was correctly marked as duplicate by @Tunaki. The question that this one is now linked to, contains a more extensive explanation. That answer is good and there is no need for me to duplicate that here :-) – Dylan Meeus Jun 29 '16 at 11:43