0

I am trying to randomly grab integers from another class and cannot figure out how to get those values. The first class is my initial random number generator. The second class is the class I am trying to retrieve these numbers to. I have created a grab method but cannot figure out how to retrieve these random integers from class ArrayBag. Any help would be appreciated!

import java.util.Random;

public final class ArrayBag<T> implements BagInterface<T>
{
    private final T[] bag; 
    private int numberOfEntries;
    private boolean initialized = false;
    private static final int DEFAULT_CAPACITY = 6;
    private static final int MAX_CAPACITY = 10000;

    /** Creates an empty bag whose initial capacity is 6. */
    public ArrayBag() 
    {
        this(DEFAULT_CAPACITY);
        System.out.print("Generating 6 random integers 1 - 6 in   ArrayBag\n");      

        //Random loop to generate random numbers 1 to 6
        Random rand = new Random();
        for(int start=1; start <=6; start++)
        {
            int randInt = rand.nextInt(6);
            System.out.println("Generated : " + randInt);
        }
        System.out.println("Finished\n");

    } // end default constructor

This is my second class and method...

import java.util.Random;

public class PopulationBag
{
    public PopulationBag()
    {
        ArrayBag ab = new ArrayBag();
        LinkedBag lb = new LinkedBag();

    }

    /**
    * Grab Method
    */

    public int grab()
    {
        ArrayBag ab = new ArrayBag();
        Random grab = new Random();
        for(int start = 1; start <= 6; start++)
        {
            int randGrab = grab.nextInt(ab);
            System.out.println("Randomly grabbed values : " + randGrab);
        }
    }

}
DanM7
  • 2,203
  • 3
  • 28
  • 46
Joseph Kraemer
  • 111
  • 1
  • 2
  • 12
  • You're creating the ArrayBag class, and constructing one in the grab function (which just overwrites the one you already made in the constructor), and then you ignore it and use Random again. I don't understand what you're trying to do, but that probably isn't it. – Lee Daniel Crocker Jan 29 '15 at 17:59
  • First issue I see is he is trying to pass an ArrayBag() into the nextInt() which is expecting an int as the parameter. – Cody Anderson Jan 29 '15 at 18:27

1 Answers1

0

As far as I understant what you are trying to do is to build a class which genereates and holds a user-given number of random ints and second class which can access the values generated by the first class.

I've noticed a few issues with your approach. Firstly, you are generating the random numbers but never store them anyway. If the number of random ints is given by a user, you can try ArrayList approach, or use a new Java 8 approach with the method ints() of Random class (which I used in the code below). You would also need a getter method to make the access to your private variable possible.

Consider the code:

public class ArrayBag {
        private int[] randomBag;
        private boolean initialized = false;
        private static final int DEFAULT_CAPACITY = 6;
        private static final int MAX_CAPACITY = 10000;

        public ArrayBag() {
            Random random = new Random();

            randomBag = new int[DEFAULT_CAPACITY];

            // The method ints introduced in Java 8 accepts three params:
            // number of ints generated, upper and lower bound between which the ints will be generated.
            // toArray method passes the generated nums as an dynamically created array,
            // which you can assign to a variable.
            randomBag = random.ints(DEFAULT_CAPACITY, 1, DEFAULT_CAPACITY + 1).toArray();
        }

// the 'getter' method
        public int getRandomInt(int i) {
            return randomBag[i];

        }
    }
        /* Second class */
        public class PopulationBag {
        private ArrayBag ab = new ArrayBag();
        private Random grab = new Random();

       // Are the numbers form the ArrayBag going to be grabbed at random,
     //or simply one after another?

        public int[] grabInOrder(int numberOfGrabbedInts) {
    // Create an array to hold ints.
            int[] intBag = new int[numberOfGrabbedInts];
    // Fill in data to the intBag
            for (int i = 0; i < numberOfGrabbedInts; i++) {
    // Call getRandomInt() method form ArrayBag class
     intBag[i] = ab.getRandomInt(i);
                System.out.println("Randomly grabbed values : " + intBag[i]);
            }

            return intBag;
        }

    private int[] grabAtRandom(int numberOfGrabbedInts) {
            int[] intBag = new int[numberOfGrabbedInts];
            for (int i = 0; i < numberOfGrabbedInts; i++) {
                // This is kind of funky, basically it returns at random the ints saved in ArrayBag.
                intBag[i] = ab.getRandomInt(grab.ints(1, 1, numberOfGrabbedInts).findFirst().getAsInt());
                System.out.println("Randomly-randomly grabbed values : " + intBag[i]);
            }
            return intBag;
        }
ttarczynski
  • 949
  • 1
  • 10
  • 19