Results 1 to 13 of 13
  1. #1
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default [SOLVED] Stuck in need of help!

    I'm trying to write a program using two seperate classes to make a deck of cards. One will create the cards assigning a number 0-51 for each card along with a value (Ace-King) and a suit. Then the other class will construct an arraylist that creates the deck. I'm not sure how to go about doing this...I can use the system.out.print to make the deck, or I think maybe with arrays. But I don't know how to use arraylists. :confused:

    Thanks for any help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd give Google a try looking for an ArrayList tutorial. Have a look here as some of these links look promising. Then why not come back if you have any questions after reading this?

    java arraylist tutorial collections - Google Search

  3. #3
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Okay, I see it's like using an array but you can continue adding to it or remove items. Which is really cool! But I'm not seeing how I would construct the card in one class then use that to make the arraylist in another. I could prolly do it in the same class (which I did with an array) but I guess the seperate classes is throwing me off... here's my code for my array of the deck.

    public class Deck
    {
    public static void main(String[] args)
    {
    String[] suit = { "Clubs", "Diamonds", "Spades", "Hearts" };
    String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
    "T", "J", "Q", "K" };


    int SUIT = suit.length;
    int VALUE = value.length;
    int D = VALUE * SUIT;

    // initialize deck
    ArrayList deck = new String[D];
    for (int i = 0; i < SUIT; i++)
    {
    for (int j = 0; j < VALUE; j++)
    {
    deck[VALUE*i + j] = value[j] + " of " + suit[i];
    }
    }


    // print deck
    for (int i = 0; i < D; i++)
    {
    System.out.println(deck[i]);
    }
    }

    }

  4. #4
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Sorry, this is the right code, I'm trying to make an arraylist.

    public class Deck
    {
    public static void main(String[] args)
    {
    String[] suit = { "Clubs", "Diamonds", "Spades", "Hearts" };
    String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
    "T", "J", "Q", "K" };


    int SUIT = suit.length;
    int VALUE = value.length;
    int D = VALUE * SUIT;

    // initialize deck
    String[] deck= new String[D];
    for (int i = 0; i < SUIT; i++)
    {
    for (int j = 0; j < VALUE; j++)
    {
    deck[VALUE*i + j] = value[j] + " of " + suit[i];
    }
    }


    // print deck
    for (int i = 0; i < D; i++)
    {
    System.out.println(deck[i]);
    }
    }

    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, this is the right code, I'm trying to make an arraylist.
    There is no ArrayList to be found in that code. Please read and study a decent tutorial before rushing into code.

  6. #6
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    I know, I'm trying to write an arraylist now using a tutorial. What I'm asking is how I would define an object in one class then use that to create an arraylist in a seperate class. Like would I need to import the class I wrote into it or is there some other way? (Sorry if I'm not making sense...)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You simply create a Card class, then in your Deck class, have an ArrayList<Card> and start adding Cards to this ArrayList.

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    static void cardList(int numHands,int cardsPerHand){
              List<String> deck,hand;
              //52-card deck
    
             Srting[] suit=new String[]{"S","H","D","C"};
             String[] rank=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
             deck=new ArrayList<String>();
             for(int i=0;i<deck.length;i++){
    			for(int j=i;j<deck.length;j++){
    				deck.add(rank[i]+"-"+suit[j]);
    			}
    		}
    		Collections.shuffle(deck);//shuffles the heap of the crads
    
                   //dealing the cards
                  for(int i=0;i<numHands;i++){
                            hand=deck.subList(deck.size()-cardsPerHand,deck.size());
                            System.out.println(hand);
                            //hand.clear();//removes the sub-list from the list
    }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by serjant View Post
    Java Code:
    static void cardList(int numHands,int cardsPerHand){
    * sigh ... *

  10. #10
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Heh, I don't believe that code means anything to my program.

  11. #11
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    shitttttttt,i was hurrying
    LOL
    Java Code:
    public void cardList(int numHands,int cardsPerHand){

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Good. Keep studying and learning. We'll help you as we can, but you'll gain much more by attempting as much as you can on your own.

  13. #13
    Zombie_Leg! is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Okay I got this. Now if I had a Card class, think this would work?

    import java.util.ArrayList;

    public class DeckOfCards
    {
    ArrayList<Card> deck = new ArrayList();
    Card cards[] = new Card[52];
    for(int i = 0; i < 4; i++)
    {
    for(int j = 0; j < 13; j++)
    {
    cards[i * 13 + j] = new Card(j + 1, i + 1);
    deck.add(cards[i * 13 + j]);
    }
    }

    }

Similar Threads

  1. stuck on an assignment
    By starchildren3317 in forum New To Java
    Replies: 11
    Last Post: 11-20-2008, 12:03 AM
  2. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 03:22 AM
  3. Stuck on Two Questions, Please Help
    By sylo18 in forum New To Java
    Replies: 5
    Last Post: 03-11-2008, 02:03 AM
  4. musically stuck cry for help 2
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 03:09 PM
  5. musically stuck
    By geork in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •