• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Shuffle Method  RSS feed

 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I been at this for hours and i don't know what is happening.

As a lab assignment, I have an ArrayList of type Card called cards (I already defined type Cards ), the ArrayList is supposed to represent a deck with 52 different Card objects. Then I have toString method that converts all objects stored in the ArrayList to String so I can later print them out. Now what I'm supposed to do is shuffle the Objects (Cards) in the ArrayList with a helper method so when they are converted to String they are printed in a random order.

If I didn't explain myself well this are the the exact instructions for the Lab;
"you need to add a shuffle method, which is called from the constructor. The shuffle method is a private helper method in the Deck class. For this version you need to shuffle the deck by swapping the cards. Generate two random numbers in the [0..51] number range that will represent the indexes of the cards array and swap the cards. Make 1000 swaps and then display the cards"

And my code so far



 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain what's going to happen here first:

1. We like to help people figure out problems for themselves. We like to see them ShowSomeEffort.
2. Most probably, moderators and/or other ranchers who respond in this thread will already know how to fix your program. However, we won't just give you the solution.
3. I'm going to ask you a series of questions designed to lead you to discovering how to fix the problem yourself. Others may ask you the same kind of questions as well. You mustn't think things like "What a stupid question!" but rather think things like "Ok, what is he/she trying to get me to look at?"

Now that we've got that covered, here's the first guiding question:

On lines 12 and 13, you use Math.random() - is there any particular reason you are doing that instead of using java.util.Random?
On the same lines, you are using the variable size. Do you know what the value of size is at that point? Are you sure that's the value?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your arraylist set up looks ok. This would be my first iteration. I would have a function called say, swap. I would have a class called swapped that would contain a card object and an integer. At the top of swap I would create two instances of swapped then I would create a random integer between 0 and 51. I would assign it to the integer of one of the swapped. I would use that number as an index into the arraylist to get a card object from the arraylist. I would store that in the card object of the second swapped. Next in swap I would do the same thing a second time. Then simply write the two swapped objects backinto the arraylist using the ints as indexes into the arraylist. I suggest first doing it with say five cards and doing one swap to make sure it's working. Then put the swap function into a loop to make it repeat 1000 times. Done.
 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Thanks! I didnt say that i wanted the solution right away, I just want help understanding the problem and help on how to solve it

On lines 12 and 13, you use Math.random() - is there any particular reason you are doing that instead of using java.util.Random?
On the same lines, you are using the variable size. Do you know what the value of size is at that point? Are you sure that's the value?


I can't use java.util.Random, i'm strutted that the random number should be generated using the Math.random() method.

And yeah at this point size = 52
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vargas wrote:
I can't use java.util.Random, i'm strutted that the random number should be generated using the Math.random() method.

Ok... why is that not mentioned here then:
Daniel Vargas wrote:
If I didn't explain myself well this are the the exact instructions for the Lab;
"you need to add a shuffle method, which is called from the constructor. The shuffle method is a private helper method in the Deck class. For this version you need to shuffle the deck by swapping the cards. Generate two random numbers in the [0..51] number range that will represent the indexes of the cards array and swap the cards. Make 1000 swaps and then display the cards"

Also, you might want to look at some of the results for Math.random() vs util.Random() and discuss the pros and cons with whoever is requiring you to use Math.random(), especially to generate 2000 random numbers.


And yeah at this point size = 52

Ok, one problem solved. Nitpick: Since the value 52 doesn't ever change is this program, do you think size should be a variable or a constant? Have you been taught how to declare constants?

Next, look carefully at lines 14, 15, and 16. Using pen and paper, pretend you generated two random numbers just as lines 12 and 13 would have. Now follow the instructions on lines 14, 15, 16 exactly as you have written it, not what you know should be happening. Hint: what you instructed the computer to do is exactly what it will do. It won't do what you know should be happening.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitpick: ramd1 and ramd2 are very poorly chosen variable names.

The first bad thing about them is that they are abbreviations and abbreviations make the reader (including you) work harder than he/she needs to.

Second, the abbreviation is not even a good one for what it's supposed to stand for: the word "random" is not abbreviated as "ramd", at least not in my world. When I read "ramd1" and "ramd2" out loud it sounds like I'm saying "rammed one" and "rammed two"! (Wait, what?! You're ramming these numbers?! Hmmm)

Third, there is only character that's different between the two. This is probably what confused you in the first place and led to you writing buggy code. If, instead, you had chosen better variable names like first and second, then it might have been easier to spot the bug.

Always pick names for variables, methods, classes, etc. that
1) You can read out loud and NOT sound like you are mumbling or have some kind of speech impediment
2) Convey their intent well - that is, the words mean the same thing as what you are using the values for in your program
 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OHHHHHHHHH OK wow that was a silly mistake

so then;




I haven't yet learned about constants.. care to give me a brief summary about what it is ?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember how I said we like to see folks ShowSomeEffort? Well, that includes using search engines to find things. How to declare constants in Java

Now, about those poorly chosen names, ramd1 and ramd2, were you planning to do anything about those? You really should get into the habit of choosing good variable names in your programs. If you had done that in the first place, you probably wouldn't have had to spend hours trying to figure out your problem.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last nitpick: the method name Shuffle should be in lowercase. The convention in Java is to have class names start with an uppercase letter. Method names, variable names, field names, and others start with a lowercase letter. Read more and familiarize yourself with Java Naming Conventions
 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giving me some last advice ?

Could you send me in the right way on how to build some kind of for loops to build the objects I need ?


So instead of having all of that just making some for loops ? The thing is that I don't know how to make the for loop Select the specific attributes necessary for each different object. Since I have 14 Objects that have the attribute "Clubs" but then they branch off and get there Clubs of "Four" , 4; "Clubs" of "Five", 5 and so on. How do I set up a for loop to select the appropriate attributes.

I had the Idea of setting up 3 different kinds of static arrays like this with the different kind of attributes that are needed in order to instantiate the different kinds of Objects



Any idea on how to have the for loop select the appropriate attributes for each object ?
Hints ?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
search for nested for-loops in Java

And your value array should have elements whose values correspond to their matching elements in the rank array. There's no rule that says elements in an array have to be unique values.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an example using enumerated types in the Java® Language Specification. Might be in this section.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder, who closed this thread?

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread isn't locked,, Piet. However, I will mark it as resolved now per the OP's request.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!