title: Call random method of random class in java
body: <p>I am working on a training program that has a Training class, with a function that returns a random method (one of the exercise types).</p>
  
  <p>This generic class is extended by some specific classes, wich contain exercise of different types. </p>
  
  <p>I then create an array of the specific classes, and want to choose a random one, then call the random exercise method.</p>
  
  <p>Here’s the generic class</p>
  
  <pre><code>public class TrainingClasses {
  Method[] mMethods;
  Random randomGenerator; 
   public void TraningClasses() {
      randomGenerator = new Random();
  
      /* Store methods */
      mMethods= getClass().getDeclaredMethods();
  
  
  }
  
   public void RandomExercise() {
      Random randGenerator = new Random();
      int rnd = randGenerator.nextInt(mMethods.length);
  
  mMethods[rnd].invoke(this);
  
  }
  </code></pre>
  
  <p>This is an example of specific training class</p>
  
  <pre><code> public class MatheMagic extends TrainingClasses{
  
  
  public MatheMagic() { 
  /*Something*/
   }
  
  
  
  public String[] SomeExercise() { 
      /*Some code */
   }
  
  public String[] SomeOtherExercise() { 
      /*Some code */
   }      
  }
  </code></pre>
  
  <p>At this point in the main activity I want to do this: </p>
  
  <pre><code>private Object[] mTrainingClasses; 
  private MatheMagic mMathMag;
  /*eventually other training classes*/
  mMathMag = new MatheMagic();
  mTrainingClasses[0] = mMathMag;
  
  Random randGenerator = new Random();
  int rnd = randGenerator.nextInt(mTrainingClasses.length);
  Object aTrain = mTrainingClasses[rnd];
  
  /*Return exercise*/
  String[] mRes = aTrain.RandomExercise();
  </code></pre>
  
  <p>This are the relevant part of the code, which is now still incomplete... However I can’t go on since I get type mismatch errors, when I try to store the result of the dynamically called method. 
  It’s likely that the project structure is incorrect, and that I should use some other architecture... However, I haven’t yet found out any better idea. </p>
  
  <p>Thanks to anybody able to help.</p>
  
  <p>–––––––––––––––––––––––––––––––––––––––––––––––</p>
  
  <p>Thank you guys for the answers.
  Here is the final working code for whoever might need this:</p>
  
  <pre><code>/** Common interface for all exercises */ 
  public interface Exercise {
    public String[] run();
  } 
  
  
  
  /** Common interface for all training classes */
  public abstract class TrainingClass {
  
    private Random rand = new Random();
    public ArrayList&lt;Exercise&gt; mExerciseTypes = new ArrayList&lt;Exercise&gt;();
  
    /** Run a random exercise */
    public String[] runRandomExercise() {
      int i = rand.nextInt(mExerciseTypes.size());
      return mExerciseTypes.get(i).run();
    }
  
  }
  
  
  /*Specific training class*/
  
  public class MatheMagic extends TrainingClass {
  
   public MatheMagic() {
  
          class SomeExercise implements Exercise {
  
              public String[] run() {
  
                   String[] mRes = new String[2];
                    mRes[0] = "Question type 1";
                    mRes[1] = "Answer type 1";
                  return mRes;
              }
  
          }
  
          class SomeOtherExercise implements Exercise {
  
              public String[] run() {
  
                   String[] mRes = new String[2];
                    mRes[0] = "Question type 2";
                    mRes[1] = "Answer type 2";
                  return mRes;
              }
  
          }
          SomeExercise mN = new SomeExercise();
  
          SomeOtherExercise mS = new SomeOtherExercise();
  
          mExerciseTypes.add(mN);
          mExerciseTypes.add(mS);
   }
  
  }
  </code></pre>