Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:11:24+00:00 2026-06-10T16:11:24+00:00

I am working on a training program that has a Training class, with a

  • 0

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).

This generic class is extended by some specific classes, wich contain exercise of different types.

I then create an array of the specific classes, and want to choose a random one, then call the random exercise method.

Here’s the generic class

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);

}

This is an example of specific training class

 public class MatheMagic extends TrainingClasses{


public MatheMagic() { 
/*Something*/
 }



public String[] SomeExercise() { 
    /*Some code */
 }

public String[] SomeOtherExercise() { 
    /*Some code */
 }      
}

At this point in the main activity I want to do this:

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();

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.

Thanks to anybody able to help.

–––––––––––––––––––––––––––––––––––––––––––––––

Thank you guys for the answers.
Here is the final working code for whoever might need this:

/** 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<Exercise> mExerciseTypes = new ArrayList<Exercise>();

  /** 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);
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:11:26+00:00Added an answer on June 10, 2026 at 4:11 pm

    I think you should probably re-think your design. Using reflection should usually be avoided in most situations. The only times you really need reflection are when you are dynamically loading and executing code (e.g. the JUnit framework dynamically loads and executes tests in a class whose name is passed as a command-line argument).

    Instead of randomly executing a method in a class I would suggest creating a collection of Exercise objects with a common interface, choosing one of them at random and then running it. Here’s some skeleton code for what I’m thinking:

    import java.util.ArrayList;
    import java.util.Random;
    
    /** Common interface for all training classes */
    public abstract class TrainingClass {
    
      private Random rand = new Random();
      protected ArrayList<Exercise> exercises = new ArrayList<Exercise>();
    
      /** Run a random exercise */
      public String[] runRandomExercise() {
        int i = rand.nextInt(exercises.size());
        return exercises.get(i).run();
      }
    
    }
    
    /** Common interface for all exercises */ 
    public interface Exercise {
      String[] run();
    }
    
    public class MatheMagic extends TrainingClass {
    
      /** Constructor creates all the exercises */
      public MatheMagic() {
        // Some exercise
        exercises.add(new Exercise {
          public String[] run() {
            // Code for some exercise ...
          }
        });
        // Some other exercise
        exercises.add(new Exercise {
          public String[] run() {
            // Code for some other exercise ...
          }
        });
        // etc ...
      }
    
    }
    

    Since all the exercises are of type Exercise and have a common run() method there is no need to use reflection. The declaration is a bit more verbose since you need to create an anonymous inner class for each exercise, but the extra bit of verbosity is well worth it for avoiding reflection!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's the requirement. While in the Gym working out, they play this training program
I have a one method interface and a class mocking that interface. The method
Working with an undisclosed API, I found a function that can set the number
So I have a library I'm working on that has, or will have, optimised
I am working with a Drupal 6.x system to create exercise / personal training
I'm working on a training tracker program and I'm at a point where I
I am working on Galaxy Nexus(Android 4.0.3). I refer this to link https://developer.android.com/training/camera/photobasics.html to
I've been working on training myself in the ways of using nAnt over the
Working with H2 I get this error when I try to write a row
I finally have this working now but would like to know how I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.