• Post Reply Bookmark Topic Watch Topic
  • New Topic

need to create random numbers and store it in two paratmeter int  RSS feed

 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another newb here.

I need my code checked. It compiles, but I get runtime error and can't figure it out. To tell you the truth, I don't even know if I'm doing it correctly.

Thanks in advance.

Here's the instruction and my code:

a constant called TIMES_TO_SHUFFLE with the value 10.

a method called swap() that takes two int parameters. This method checks to make sure the parameters are valid index positions in the deck. If they are not valid it displays an error message. If they are valid it switches the positions in the deck of the two cards at those index positions.

a method called shuffle()that randomly selects two numbers between 0 (inclusive) and the size of the deck (exclusive), and passes those numbers as parameters to swap(). This must be within a loop, so that the swap() method is called TIMES_TO_SHUFFLE times.


 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the swap() method,
If invalid positions, it would print error message but still do the swap.
I am not quite understand your swap logic and index1 (method local variable) is different to this.index1 (class instance variable).
If you want to swap two variable, you could use a temp variable to hold first value, set first variable to second value, set second variable to the temp value.

For the shuffle() method,
You didn't call the swap() method.
What would be the usage for counter? keep track the number of execution, but it is not used
RunTimeException you have got may due to the while loop condition, 0 <= TIMES_TO_SHUFFLE, in what condition it would end the while loop?
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raymond. This is what I got. In swap() method, I put everything in one if statement and got rid of this. In shuffle() method, I got rid of runtime error by replacing 0 with counter...newb mistake.

I am still not getting swap result, the numbers are not swapping. Any suggestion?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, what are lines 5 and 6 supposed to do? They do nothing, just assign variables to themselves.

Second, variables in Java are passed by value. You don't pass the variable itself, just its value, which is copied into the argument variables whenever you call a method.

Since the argument variables are not the original variables you passed into the method, nothing happens to the original variables when you assign anything to the argument variables. To illustrate:

This means that assigning to index1 and index2 in your swap() method is not going to work. The swap() method will do nothing to the variables that you passed in.
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!