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);
}
}
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
Exerciseobjects with a common interface, choosing one of them at random and then running it. Here’s some skeleton code for what I’m thinking:Since all the exercises are of type
Exerciseand have a commonrun()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!