Results 1 to 9 of 9
- 03-01-2012, 04:42 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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 objectsLast edited by chasefooo; 03-01-2012 at 04:56 AM.
-
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.
- 03-01-2012, 05:13 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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
- 03-01-2012, 05:26 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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);
-
Re: Using method on arraylist references have unwanted results, Need help!
Yes, use arrays and a for loop.
- 03-01-2012, 05:35 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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
- 03-01-2012, 05:55 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-01-2012, 06:06 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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.
- 03-01-2012, 10:06 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Similar Threads
-
fillRect Coming Up With Strange Results?
By MrFish in forum Java 2DReplies: 1Last Post: 11-27-2011, 06:01 PM -
Passing method results to each other
By dexterc in forum New To JavaReplies: 3Last Post: 11-10-2011, 11:49 AM -
calling Graphics functions within for loop yields strange results
By lmtoe in forum AWT / SwingReplies: 7Last Post: 01-17-2011, 07:02 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks