0

Assuming I am working on creating a deck of cards game, I have two methods called shuffle and randomInt in my class.

private static void shuffle(Card [] cardArray) {
    for (int i = 1; i <= 20; s++) {
        Card temp = cardArray[a];
        cardArray[a] = cardArray[b];
        cardArray[b] = temp;
    }
}

private static void randomInt(int a, int b) {   
    a = (int)(Math.random() * 12);
    b = (int)(Math.random() * 12);
}

Question here is how can I pass the variable a and b from method randomInt() into the shuffle() method? I understand that I can simply put in this randomInt() inside shuffle() and it will work fine, but I will like to know if there is any way to do it this way.

Will appreciate for someone to explain on the concept too as I am fairly new to OOP. Thank you.

amq
  • 160
  • 4
  • 17
SunnyBoiz
  • 514
  • 1
  • 5
  • 14
  • Your `swap` method doesn't actually swap anything, it just assigns values to two variables that are local to the method – UnholySheep Oct 12 '19 at 11:02
  • If you want to pass something to a method you usually include it as a parameter, what's the problem with doing that here? – UnholySheep Oct 12 '19 at 11:03
  • Edited my post by renaming the swap. I am trying to figure out what is the correct input for cardArray[?] as putting in cardArray[a] will not work. – SunnyBoiz Oct 12 '19 at 11:05

5 Answers5

2

Let your randomInt() return the number and call the function inside of shuffle().

private static void shuffle(Card[] cardArray) {
    for (int i = 1; i <= 20; i++) {
        int a = randomInt();
        int b = randomInt();
        Card temp = cardArray[a];
        cardArray[a] = cardArray[b];
        cardArray[b] = temp;
    }
}

private static int randomInt() {   
    return (int)(Math.random() * 12);
}

This will shuffle your card deck according to the way randomInt() generates the indices

amq
  • 160
  • 4
  • 17
  • Thank you for the reply. Is there no way to do the variable pass with my given code above? – SunnyBoiz Oct 12 '19 at 11:19
  • since you override `a` and `b` there is no reason to pass them into the function – amq Oct 12 '19 at 11:24
  • Thank you. I will mark this as the answer since this method works well and did not deviate too far from my original code. – SunnyBoiz Oct 13 '19 at 04:51
0

u can create a Deck class and put ur logic and data in this class

public class Deck {
    private Card[] deck = new Card[52];
    public Deck(){
        initDeck();
    }
    public void shuffle() {
        for (int i = 1; i <= 20; i++)
        {
            int a = (int)(Math.random() * 12);
            int b = (int)(Math.random() * (52 - 12));
            swap(a,b);
        }
    }
    private void swap(int a,int b){
        Card temp = deck[a];
        deck[a] = deck[b];
        deck[b] = temp;
    }
    private void print() {
       ...
    }

}

and in ur main method doing somthing like that

Deck d = new Deck();
deck.shuffle();
deck.print();
hossein rasekhi
  • 209
  • 1
  • 13
  • Hey, thanks. I have this class and the methods are all in a similar class like this. I am trying to know how to pass the variable a and b from swap to shuffle so I can do the shuffling of deck. – SunnyBoiz Oct 12 '19 at 11:18
  • ur method is static and i think u have to share Card[] data between methods. u have to model ur world correctly – hossein rasekhi Oct 12 '19 at 11:21
0

If you want your application to return two values a and b from your method randomInt, you cannot just declare a and b as parameters. Method parameters in java are "ByValue" parameters. The method does not change the value on the caller's a and b values.

Preferred option:

let randomInt return an array with two elements. After invocation of randomInt you can assign the values from the array to your variables a and b in the caller method.

Alternative option, pseudo by-references via an array:

instead of passing a and b, pass an array with just one element to your method:

private static void randomInt(int[] a, int[] b)
{ 
  //assuming a[] and b[] both are an array with just one element
  //set a[0] and b[0] here like you already set a and b
}

and on the caller side,

...
int[] a = new int[1];
int[] b = new int[1];
randomInt(a, b);

//now you have your values in a[0] and b[0].
Christoph Bimminger
  • 1,006
  • 7
  • 25
0

In java, the method doesn't work as you supposed to (https://www.google.com/?q=java+call+by+value ... https://stackoverflow.com/a/40523/592355), but you can work around:

  • You can encapsulate your output variables inside an object (which modification will persist after the method exits):

    with

    class TwoInts {
       int a,b;
    }
    

    and:

    private static void randomInt(TwoInts container) {
      assert(conatiner != null);   
      container.a = (Math.random() * 12);
      container.b = (Math.random() * 12);
    }
    
  • The straight forward approach would be to (write a method with one return value):

     private static int rand(int offset, int max) {
        return (int) (Math.random() * max) + offset;
     }
    

    ..and to invoke it twice:

     a = rand(0, 12);
     b = rand(0, 12);
    
  • ...

Please also have a look at java.util.Random...

xerx593
  • 12,237
  • 5
  • 33
  • 64
-3

You can declare the a and b variable global

int a,b;

private static void shuffle(Card [] cardArray)
{
    for (int i = 1; i <= 20; s++)
    { 
       randomint();
       Card temp = cardArray[a];
       cardArray[a] = a;
       cardArray[b] = b;
    }
}

private static void randomInt()
{   
    a = (Math.random() * 12);
    b = (Math.random() * 12);
}
salvatore
  • 1
  • 1
  • 1
    No, you can't - you could make them `static` members of the class. Also why would you then have input parameters on `randomInt`? – UnholySheep Oct 12 '19 at 11:16
  • 1
    Indeed, currently as you've got them as method parameters as well, the meaning of `a` in the `randomInt` method is the parameter, not the variable. (And the call to `randomint` would fail as it doesn't provide any arguments - and there's a capitalization issue.) – Jon Skeet Oct 12 '19 at 11:17