473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to use multiple stacks?

15
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,

Expand|Select|Wrap|Line Numbers
  1. public class Cards {
  2.         String sRank = "";
  3.         String sSuit = "";
  4.         int rank = 1;
  5.         int suit = 1;
  6.         Object suitAndRank;
  7.  
  8.         public String switchSuit(int suit) {
  9.             if(suit == 0)
  10.                 sSuit = "D";
  11.             else if(suit == 1)
  12.                 sSuit = "H";
  13.             else if(suit == 2)
  14.                 sSuit = "S";
  15.             else if(suit == 3)
  16.                 sSuit = "C";
  17.             else
  18.                 sSuit = null;
  19.             return sSuit;
  20.         }
  21.  
  22.         public String switchRank(int rank) {
  23.             if (rank == 0)
  24.                 sRank = "A";
  25.             else if (rank == 1)
  26.                 sRank = "2";
  27.             else if (rank == 2)
  28.                 sRank = "3";
  29.             else if (rank == 3)
  30.                 sRank = "4";
  31.             else if (rank == 4)
  32.                 sRank = "5";
  33.             else if (rank == 5)
  34.                 sRank = "6";
  35.             else if (rank == 6)
  36.                 sRank = "7";
  37.             else if (rank == 7)
  38.                 sRank = "8";
  39.             else if (rank == 8)
  40.                 sRank = "9";
  41.             else if (rank == 9)
  42.                 sRank = "10";
  43.             else if (rank == 10)
  44.                 sRank = "J";
  45.             else if (rank == 11)
  46.                 sRank = "Q";
  47.             else if (rank == 12)
  48.                 sRank = "K";
  49.             else
  50.                 sRank = null;
  51.             return sRank;
  52.         }
  53.  
  54.  
  55.         public Object switchSuitAndRank(int rank, int suit) {
  56.             sSuit = switchSuit(suit);
  57.             sRank = switchRank(rank);
  58.             suitAndRank = switchRank(rank) + switchSuit(suit);
  59.             return suitAndRank;
  60.         }
  61.  
  62. }
  63.  
and here is the class I'm building to make the four stacks for each suit.

Expand|Select|Wrap|Line Numbers
  1. public class PushFourSuits {
  2.  
  3.     Object x;
  4.     Object toBeDisplayed;
  5.     int rank = 0;
  6.     LinkedStack diam = new LinkedStack();
  7.     LinkedStack heart = new LinkedStack();
  8.     LinkedStack spad = new LinkedStack();
  9.     LinkedStack club = new LinkedStack();
  10.     Cards cards = new Cards();
  11.  
  12.     public void diamondStack(){
  13.         int suit = 0;
  14.         for(rank = 0; rank <= 12; rank++){
  15.             x = cards.switchSuitAndRank(suit, rank);
  16.             diam.push(x);
  17.         }
  18.     }
  19.  
  20.     public void heartStack(){
  21.         int suit = 1;
  22.         for(rank = 0; rank <= 12; rank++){
  23.             x = cards.switchSuitAndRank(suit, rank);
  24.             heart.push(x);
  25.         }
  26.     }
  27.  
  28.     public void spadeStack(){
  29.         int suit = 2;
  30.         for(rank = 0; rank <= 12; rank++){
  31.         x = cards.switchSuitAndRank(suit, rank);
  32.         spad.push(x);
  33.         }
  34.     }
  35.  
  36.     public void clubStack(){
  37.         int suit = 3;
  38.         for(rank = 0; rank <= 12; rank++){
  39.         x = cards.switchSuitAndRank(suit, rank);
  40.         club.push(x);
  41.         }
  42.     }
  43. }
  44.  
Jan 3 '08 #1
8 3905
r035198x
13,262 8TB
Why not go object-oriented and have a Card class?
Jan 3 '08 #2
BigDaddyLH
1,216 Expert 1GB
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
Jan 3 '08 #3
cerise
15
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,we're actually required to use the stacks. is there any way you can help me out with that?
Jan 3 '08 #4
BigDaddyLH
1,216 Expert 1GB
oh, unfortunately,we'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.
Jan 3 '08 #5
cerise
15
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.
Jan 3 '08 #6
BigDaddyLH
1,216 Expert 1GB
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?
Jan 3 '08 #7
cerise
15
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.)
Jan 4 '08 #8
BigDaddyLH
1,216 Expert 1GB
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.
Jan 4 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers =...
7
by: Gabriele Farina | last post by:
Hi, I'd like to manage multiple socket connection without having to use Threads. I'd like to manage every connection when it is returned from socket_Accept(), without having to wait for the...
5
by: Vanessa T. | last post by:
Hello All! Is there a place where I can learn stacks, (push and pop) etc. Thanks,
6
by: Sathyaish | last post by:
I've searched Google and found a few, but I am not so satisfied. Any good reading on "stacks and heaps" about how and when memory is allocated from the heap?
10
by: Rich Kucera | last post by:
Holding all versions at 5.0.4, Multiple stacks with multiple-version configurations inevitable Will have to wait to see what the impact of problems such as http://bugs.php.net/bug.php?id=33643 ...
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
0
by: raghuveer | last post by:
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have #include<stdio.h>...
6
by: Bugs | last post by:
Does anyone have any recommendations on the best way to create multiple instances of the same class when the final number of instances is unknown? For example, I have a class and based on some...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.