Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Using method on arraylist references have unwanted results, Need help!

  1. #1
    chasefooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Using method on arraylist references have unwanted results, Need help!

    hello im doing a school project for my ap computer programming class and we have to build a program that can deal 20 random cards( doesnt have to be of a 52 card deck) i have it set up so it will create 20 card object containing suit and value and reference them in an arraylist. then uses the deal_one method on each object individually and then prints each card in the array. the problem i am having is that each card printed is exactly the same, not sure why.below is my code.

    i plan on using some graphics with it later but now im just trying to get the logic of it right.


    Java Code:
    /**
     *   this is the driver class for a random card generator that generates 20 random cards 
     * 
     * @author    ___ _                       __  _     _           
     *           / __\ |__   __ _ ___  ___   / _\(_)___| | ___ _ __ 
     *          / /  | '_ \ / _` / __|/ _ \  \ \ | / __| |/ _ \ '__|
     *         / /___| | | | (_| \__ \  __/  _\ \| \__ \ |  __/ |   
     *         \____/|_| |_|\__,_|___/\___|  \__/|_|___/_|\___|_|           
     * @version (2/29/2012)
     */
    import java.util.ArrayList;
    public class Driver
    {
    
    
        public  void main (String[] args)
        {
            ArrayList deck = new ArrayList(); 
            Card crd = new Card();
            
            
            Card crd1 = new Card();   
            Card crd2 = new Card(); 
            Card crd3 = new Card(); 
            Card crd4 = new Card(); 
            Card crd5 = new Card(); 
            Card crd6 = new Card(); 
            Card crd7 = new Card(); 
            Card crd8 = new Card();
            Card crd9 = new Card(); 
            Card crd10 = new Card(); 
            Card crd11 = new Card(); 
            Card crd12 = new Card();
            Card crd13 = new Card(); 
            Card crd14 = new Card(); 
            Card crd15 = new Card(); 
            Card crd16 = new Card(); 
            Card crd17 = new Card(); 
            Card crd18 = new Card(); 
            Card crd19 = new Card(); 
            Card crd20 = new Card(); 
                
                deck.add (crd1);       
                deck.add (crd2); 
                deck.add (crd3); 
                deck.add (crd4); 
                deck.add (crd5); 
                deck.add (crd6); 
                deck.add (crd7);  //   <<-- fills the arraylist with 20 Card objects
                deck.add (crd8); 
                deck.add (crd9); 
                deck.add (crd10); 
                deck.add (crd11); 
                deck.add (crd12);
                deck.add (crd13); 
                deck.add (crd14); 
                deck.add (crd15); 
                deck.add (crd16); 
                deck.add (crd17); 
                deck.add (crd18); 
                deck.add (crd19); 
                deck.add (crd20); 
    
            
            
            for (int x=0;x<20;x++)
            {
                Card y = (Card)deck.get(x);    //
                y.deal_one();                 //     <<--  uses deal_one method to randomize each card in array
                deck.add (x,y);              //            then replaces the card back to the same location
            
            }
            
            for (int x=0;x<20;x++)
            {
                Card current_card = (Card)deck.get (x);
                int val = current_card.get_value();
                
                System.out.println (val);
            }
            
            
            
            
        }
    }



    Java Code:
    /**
     *   the card class has card object 
     * 
     * @author    ___ _                       __  _     _           
     *           / __\ |__   __ _ ___  ___   / _\(_)___| | ___ _ __ 
     *          / /  | '_ \ / _` / __|/ _ \  \ \ | / __| |/ _ \ '__|
     *         / /___| | | | (_| \__ \  __/  _\ \| \__ \ |  __/ |   
     *         \____/|_| |_|\__,_|___/\___|  \__/|_|___/_|\___|_|           
     * @version (2/29/2012)
     */
    import java.util.Random;
    public class Card
    {
        public  Random generator = new Random();
        private int value, suit;
        
        public Card()
        {   
            suit = 1;
            value = 1;
        }
        
        public void deal_one()
        {
            suit = generator.nextInt(4) + 1;
            value = generator.nextInt(13)+1;
        }
        
        public String get_suit()
        {
            String show = "nothing";
            
            int x = suit;
            if (x == 0){show = "Ace";}
            if (x == 1){show = "Heart";}
            if (x == 2){show = "Spade";}
            if (x == 3){show = "Club";}
            
            return show;
        }
        
        public int get_value()
        {
            return value;
        }
    
    
        
    }




    Edit:Also i forgot to ask if there is a better way to instantiate all those objects
    Last edited by chasefooo; 03-01-2012 at 04:56 AM.

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

    Default Re: Using method on arraylist references have unwanted results, Need help!

    If it were my program, I wouldn't put any randomization in my Card class but instead would just have that class be the blueprint for creating a single Card object and that's it. I'd not give it a parameterless constructor but rather would have two parameters passed in so that the card rank and suit are determined in the constructor.
    chasefooo likes this.

  3. #3
    chasefooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Using method on arraylist references have unwanted results, Need help!

    ok thank you ill try that now and report the results. i like that idea better

  4. #4
    chasefooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Using method on arraylist references have unwanted results, Need help!

    It Works!! thanks , what do you think about my last question? is there anyway to shorten this?


    Card crd2 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd3 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd4 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd5 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd6 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd7 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd8 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd9 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd10 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd11 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd12 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd13 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd14 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd15 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd16 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd17 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd18 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd19 = new Card(b,a); a = generator.nextInt(13)+1; b = generator.nextInt(4)+1;
    Card crd20 = new Card(b,a);

    deck.add (crd1);
    deck.add (crd2);
    deck.add (crd3);
    deck.add (crd4);
    deck.add (crd5);
    deck.add (crd6);
    deck.add (crd7); // <<-- fills the arraylist with 20 Card objects
    deck.add (crd8);
    deck.add (crd9);
    deck.add (crd10);
    deck.add (crd11);
    deck.add (crd12);
    deck.add (crd13);
    deck.add (crd14);
    deck.add (crd15);
    deck.add (crd16);
    deck.add (crd17);
    deck.add (crd18);
    deck.add (crd19);
    deck.add (crd20);

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

  6. #6
    chasefooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Using method on arraylist references have unwanted results, Need help!

    Sorry i have only been coding for about a month, could you give a little example

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Using method on arraylist references have unwanted results, Need help!

    It may be a bit over your head now, but read this tutorial; it has a nice implementation for playing cards.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    chasefooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Using method on arraylist references have unwanted results, Need help!

    Thanks! this seems like it will be really useful i cant quite follow it at the moment but im going to book mark it and keep reading it until i understand it.
    thanks again everyone i will defiantly be coming back to this forum.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Using method on arraylist references have unwanted results, Need help!

    Since you are not using the 'crd<n>' variables themselves, simply adding them to the array, Fubarable is suggesting you just do a for loop 20 times, generating a card and adding it to the 'deck'.

    You already have a for loop in your original code that would do the job, just change the code inside it to create a single card and add it to the deck.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. fillRect Coming Up With Strange Results?
    By MrFish in forum Java 2D
    Replies: 1
    Last Post: 11-27-2011, 06:01 PM
  2. Passing method results to each other
    By dexterc in forum New To Java
    Replies: 3
    Last Post: 11-10-2011, 11:49 AM
  3. Replies: 7
    Last Post: 01-17-2011, 07:02 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM

Tags for this Thread

Posting Permissions

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