how to use multiple stacks?

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

    #1

    how to use multiple stacks?

    I can't figure out how to make and handle multiple stacks and use them so I could create four linked list stacks representing each suit of cards, one stack each for diamonds, hearts, spades, and clubs. I use objects as my cards. I don't know how to make more than one stack.

    Can anyone help me out? If I'm not being clear, I'll be happy to clarify the problem I have.


    here is the class I used to make the Objects for cards,

    Code:
    public class Cards {
    		String sRank = "";
    		String sSuit = "";
    		int rank = 1;
    		int suit = 1;
    		Object suitAndRank;
    		
    		public String switchSuit(int suit) {
    			if(suit == 0)
    				sSuit = "D";
    			else if(suit == 1)
    				sSuit = "H";
    			else if(suit == 2)
    				sSuit = "S";
    			else if(suit == 3)
    				sSuit = "C";
    			else
    				sSuit = null;
    			return sSuit;
    		}
    		
    		public String switchRank(int rank) {
    			if (rank == 0)
    				sRank = "A";
    			else if (rank == 1)
    				sRank = "2";
    			else if (rank == 2)
    				sRank = "3";
    			else if (rank == 3)
    				sRank = "4";
    			else if (rank == 4)
    				sRank = "5";
    			else if (rank == 5)
    				sRank = "6";
    			else if (rank == 6)
    				sRank = "7";
    			else if (rank == 7)
    				sRank = "8";
    			else if (rank == 8)
    				sRank = "9";
    			else if (rank == 9)
    				sRank = "10";
    			else if (rank == 10)
    				sRank = "J";
    			else if (rank == 11)
    				sRank = "Q";
    			else if (rank == 12)
    				sRank = "K";
    			else
    				sRank = null;
    			return sRank;
    		}
    		
    		
    		public Object switchSuitAndRank(int rank, int suit) {
    			sSuit = switchSuit(suit);
    			sRank = switchRank(rank);
    			suitAndRank = switchRank(rank) + switchSuit(suit);
    			return suitAndRank;
    		}
    		
    }
    and here is the class I'm building to make the four stacks for each suit.

    Code:
    public class PushFourSuits {
    	
    	Object x;
    	Object toBeDisplayed;
    	int rank = 0;
    	LinkedStack diam = new LinkedStack();
    	LinkedStack heart = new LinkedStack();
    	LinkedStack spad = new LinkedStack();
    	LinkedStack club = new LinkedStack();
    	Cards cards = new Cards();
    	
    	public void diamondStack(){
    		int suit = 0;
    		for(rank = 0; rank <= 12; rank++){
    			x = cards.switchSuitAndRank(suit, rank);
    			diam.push(x);
    		}
    	}
    	
    	public void heartStack(){
    		int suit = 1;
    		for(rank = 0; rank <= 12; rank++){
    			x = cards.switchSuitAndRank(suit, rank);
    			heart.push(x);
    		}
    	}
    	
    	public void spadeStack(){
    		int suit = 2;
    		for(rank = 0; rank <= 12; rank++){
    		x = cards.switchSuitAndRank(suit, rank);
    		spad.push(x);
    		}
    	}
    	
    	public void clubStack(){
    		int suit = 3;
    		for(rank = 0; rank <= 12; rank++){
    		x = cards.switchSuitAndRank(suit, rank);
    		club.push(x);
    		}
    	}
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Why not go object-oriented and have a Card class?

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Enums would clean your code up nicely, for suits and ranks. This page uses playing cards, gulp, as their example:

      http://java.sun.com/j2se/1.5.0/docs/...age/enums.html

      Comment

      • cerise
        New Member
        • Dec 2007
        • 15

        #4
        Originally posted by BigDaddyLH
        Enums would clean your code up nicely, for suits and ranks. This page uses playing cards, gulp, as their example:

        http://java.sun.com/j2se/1.5.0/docs/...age/enums.html
        oh, unfortunately,w e're actually required to use the stacks. is there any way you can help me out with that?

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by cerise
          oh, unfortunately,w e're actually required to use the stacks. is there any way you can help me out with that?
          Sure. In your initial message you said you didn't know how to create more than one stack, but then you went on in class PushFourSuits (odd name, that) to create four stacks. I'm not sure what your question is.

          Comment

          • cerise
            New Member
            • Dec 2007
            • 15

            #6
            Originally posted by r035198x
            Why not go object-oriented and have a Card class?
            oh gosh, I'm so sorry, I'm a total noob. I had thought that what I posted was my card class, and I also didn't post any main method. was I doing it completely wrong? I'm really sorry.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by cerise
              oh gosh, I'm so sorry, I'm a total noob. I had thought that what I posted was my card class, and I also didn't post any main method. was I doing it completely wrong? I'm really sorry.
              Your class Cards seems to only form Strings that are the names of cards, which you push onto your stacks. So I wouldn't call that a card class, per se. Instances of a card class would have a rank and a suit, and you would be pushing those instances onto your stacks, not the strings that you do.

              But what I think you really need to do is step back and be clearer. What is the problem statement? What are you trying to do? What is your goal? Are there any restrictions on how you solve the problem? How will you know if you have succeeded?

              Comment

              • cerise
                New Member
                • Dec 2007
                • 15

                #8
                Originally posted by BigDaddyLH
                Sure. In your initial message you said you didn't know how to create more than one stack, but then you went on in class PushFourSuits (odd name, that) to create four stacks. I'm not sure what your question is.
                oh wait, so does that mean I'm creating the stacks properly? (Sorry, I'm being completely difficult. I only have little idea of what I'm doing.)

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by cerise
                  oh wait, so does that mean I'm creating the stacks properly? (Sorry, I'm being completely difficult. I only have little idea of what I'm doing.)
                  I can't say because I don't know your requirements.

                  Comment

                  Working...