creating a stack of objects with proper constructors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cerise
    New Member
    • Dec 2007
    • 15

    creating a stack of objects with proper constructors?

    I'm using a Linked list stack with objects. I figured out the reason for my earlier problem (where I couldn't access "rank" and "suit" from one of the objects in my stack), and it was because my object didn't have the attributes "rank" and "suit". Turns out my earlier cards were practically just strings. Can anyone help me create a whole deck of cards represented by 52 objects in a linked list stack with the attributes "suit" and "rank"?

    This is where I try to create the deck.
    Code:
    	Cards[] cards = new Cards[52];
    	Cards acards = new Cards();
    
            public void deck(){
    		for(int i=0; i<=51; i++) {
    			for(suit = 0; suit <= 3; suit++) {
    				for(rank = 0; rank <= 12; rank++){
    		            cards[i] = new Cards();
    	                String sRank = "";
    	                rank = acards.getRank();
    					suit = acards.getSuit();
    					System.out.println(cards[i]);
    				}
    			}
    		}
    	}
    This is the card class. I tried making a constructor, but I'm really not very familiar with using them (this is only the second time I've tried to make one), so sorry for the messiness and the overall chaos of my codes.
    Code:
    public Cards(String sRank, String sSuit, boolean avisibility) {
    			toStringRank() = sRank;
    			toStringSuit() = sSuit;
    			visibility = avisibility;
    		}
    		
    		public int getRank() {
    			return rank;
    		}
    		public int getSuit() {
    			return suit;
    		}
    		
    		public String toStringRank() {
    			if (a == 0)
    				sRank = "A";
    			else if (a == 1)
    				sRank = "2";
    			else if (a == 2)
    				sRank = "3";
    			else if (a == 3)
    				sRank = "4";
    			else if (a == 4)
    				sRank = "5";
    			else if (a == 5)
    				sRank = "6";
    			else if (a == 6)
    				sRank = "7";
    			else if (a == 7)
    				sRank = "8";
    			else if (a == 8)
    				sRank = "9";
    			else if (a == 9)
    				sRank = "10";
    			else if (a == 10)
    				sRank = "J";
    			else if (a == 11)
    				sRank = "Q";
    			else if (a == 12)
    				sRank = "K";
    			else
    				sRank = null;
    			return sRank;
    		}
    		
    		public String toStringSuit() {
    			if (a == 0)
    				sSuit = "D";
    			else if (a == 1)
    				sSuit = "H";
    			else if (a == 2)
    				sSuit = "S";
    			else if (a == 3)
    				sSuit = "C";
    			else
    				sSuit = null;
    			return sSuit;
    		}

    Thank you to everyone who has been answering question after question I post, and for their patience with me. They've all been so completely helpful. :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    @OP: you represent ranks and suits by integers. for any number n in [0,52)
    exclusive you can set the rank == n%13 and suit == n%4. That way you need
    only one single loop to fill the deck of cards.

    kind regards,

    Jos

    Comment

    Working...