-1

I am having trouble getting a random generator instance from being recognized as an object and it won't allow for use within another .class file. The base code for the random integer generator is this:

package RandomInstanceGenerator;
import java.util.Random;

/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {

  public static final void main(String... aArgs){
    log("Generating 10 random integers in range 0..99.");

    //note a single Random object is reused here
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      int randomInt = randomGenerator.nextInt(100);
      log("Generated : " + randomInt);
    }

    log("Done.");
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

I am trying to have the code below run what is above as a new instance. I have tried several methods that were apparent to me from other learnings, but they have failed me and so I request the knowledge of others for help in understanding. I say that in understanding that i literally copied and pasted the base code from another source that has it run as it's own little .class. Here is the code that tries to create a new instance:

package RandomInstanceGenerator;
import java.util.Random;
class Inst {
  public static void main (String args[]) {
    RandomInteger rig=new RandomInteger();
    rig.main(args);
  }
}

I am certain both need editing, hope I can fix this out so it works for me.

List of attempted changes:

1) Tried importing RandomInteger.class. The error given back says it cannot find symbol "Random Integer".

I used the code import RandomInstanceGenerator.RandomInteger;.

2) Working on the next attempt later..

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
userLost
  • 3
  • 4
  • 2
    If you're seeing an error and need our help, you need to print the exact and full error text. Else you're forcing us to guess. – Hovercraft Full Of Eels Apr 10 '13 at 01:38
  • Note that you're trying to call a static method on an instance and not on the class. This can be done but is not a good practice. More important, I think that you don't want your RandomInteger class to have a main method, or any static methods, and in fact I'm willing to wager money on this. – Hovercraft Full Of Eels Apr 10 '13 at 01:39
  • It is saying it cannot find the symbols "RandomInteger" when I try to create a new instance. – userLost Apr 10 '13 at 01:40
  • 1
    You will have to try any changes and see if it works or not. We are a poor substitute for your Java compiler and JVM. – Hovercraft Full Of Eels Apr 10 '13 at 01:45
  • Then that isn't going to help then, so nevermind on that small question.. Hm. I will work some more and edit my post with more details if i cannot find a way to get it to work. thanks for the help. – userLost Apr 10 '13 at 01:48
  • You need to show us a specific set of headers (package/import/class lines) and the specific (copy/paste) error generated, plus describe your directory structure and give us the exact javac command line you're using. – Hot Licks Apr 10 '13 at 02:18

1 Answers1

0

When Java executes a program it looks for a main function; in this case in your second class. That class then instantiates your first class (via new RandomInteger()). You then attempt to call into that first class's main method.

Note, though, that the method is labeled static. Static methods are executable only on the class, not on a specific instantiated object. If you were to use RandomInteger.main() you could expect a different result:

class Inst {
  public static void main (String args[]) {
    RandomInteger.main(args);
  }
}

But note that this is equivalent to just running RandomInteger as it's own program. If, as you say, you want to run your program as a method on an object, this is what you want:

public final class RandomInteger {
  private Random randomGenerator = new Random(); //A single object can reuse this component

  //function prints out x random numbers between low and high
  //Note that your function should do ONE thing, therefore do not make it also interpret
  //your program arguments!
  public void logXRandomNumbers(int x, int low, int high){ 
    log("Generating " + x + " random integers in range " + low ".." + high);

    int range = high - low;
    //you really should do a sanity check here to ensure the range is valid.

    //note a single Random object is reused here
    for (int idx = 1; idx <= x; ++idx){
      int nextResult = this.randomGenerator.nextInt(range) + low;
      log("Generated : " + nextResult);
    }

    log("Done.");
  }

  //This function is probably overkill
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

Now all you have to do is call this from your main function:

public static void main (String args[]) {
  RandomInteger generator = new RandomInteger();
  generator.logXRandomNumbers(10, 0, 100);
}

As for your imports, both classes should have the same package. Lets say its Generator for simplicity:

package Generator;

Only your second class (the not very-well-named Inst) needs to import your generator/logging class:

import Generator.*;

or

import Generator.RandomInteger;

Be sure that both of these files are in the same directory named 'Generator' and that you are running javac from the directory above that.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • It worked! I thank you for your effort in explanation to a less than understanding individual. Thanks. :) – userLost Apr 10 '13 at 12:14