0

I am trying to figure out an effective way of getting a random class from my project. There are nine different classes(ten including the startup), each one with a different behavior(which extend a declaratoid class that is used in the main function). I need to be able to have the function to run differently on each startup. What do I need to do? Edit:Thanks for the answer, but I am now running into another problem. I have to pass the result as the first parameter in another the function now.

2 Answers2

3

Create a factory method that gets a random number and creates an object based on that, in a switch:

public static YourInterfaceType createRandom() {
   Random r = new Random();
   switch(r.nextInt(10)) {
       case 1: return new FirstType();
       case 2: return new SecondType();
       // etc
       default: return new LastType();
   } 
}

Edit More exact definition of words. :)

Todd
  • 28,544
  • 10
  • 77
  • 83
0

you could generate a random number from 1 - 9 and use a switch statement where each case calls a different class

Lance
  • 19
  • 5