0

There isn't an error message appearing in my code. However, I get different outputs when I type my code in the main and when i call my methods.

package card;

import java.util.Arrays;
import java.util.Random;

/**
 *

 */
public class Card {

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

   public Card(String rank, String suit)
   {
      this.rank = rank;
      this.suit = suit;
   }

   public static String getRank()
   {
       return rank;
   }

   public static void setRank(String r)
   {
       rank = r;
   }

    public static String getSuit()
   {
       return suit;
   }

   public static void setSuit(String s)
   {
       suit = s;
   }





    public static void init(Card[] deck)
    {
        for(int x = 0; x<deck.length; x++)
        {
            Card newCard = new Card(ranks[x%13], suits[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].getRank() + " of " + deck[x].getSuit());
        }

    }


    public static void main(String[] args) {
        // TODO code application logic here


    for(int x = 0; x<deck.length; x++)
    {
        Card cards = new Card(ranks[x%13], suits[x/13]);
        deck[x] = cards;
        System.out.println(deck[x].getRank() + " of " + deck[x].getSuit());

        //init(deck);
        //print(deck);
    }


  }

}

For doing my codes in the main manually without any methods, i will get the correct output:

Eg

10 of Diamonds
J of Diamonds
Q of Diamonds
K of Diamonds

But when i call the methods, i get the following output:

A of Diamonds
A of Diamonds
A of Diamonds
A of Diamonds
A of Diamonds

Whats wrong?

3
  • What do you mean "when i invoke the methods"? How are you running them without running the main method? Commented Jun 30, 2016 at 11:25
  • when i call the method eg: init(), print() Commented Jun 30, 2016 at 11:25
  • 1
    Have you tried to use a Debugger to find out what is happend? Commented Jun 30, 2016 at 11:26

1 Answer 1

2

Your following static members

static String rank;
static String suit;

shouldn't be static, since each Card should have different values :

String rank;
String suit;
Sign up to request clarification or add additional context in comments.

4 Comments

thank you. from my understanding, doesn't static refers to attribute that belongs to the class?
@Aloysius static means that all instances of the class share a single instance of the static variable. Hence all Card instances have the same rank and suit in your case.
Oh, as in the variable value won't change. am i right to say this?
@Aloysius It will change whenever you assign a new value to it, but each time you change it, that change will be seen by all the Card instances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.