3

Good day.

I have a class that I’m going to use to generate math exercises for training purposes. The class is made of a series of method, each one of them generates one type of exercises.

I’d like then to make a method that generates one random exercise of a random type. To do this I thought to store the methods name in an array an call a random entry.

So far so good.

Since in the future I’m going to add methods to generate new exercise types, I’d like to know if there is a way to generate dynamically the array of the methods: once the class is loaded, the constructor will check the methods available and store their name in an array.

Is that possible? And, if so, how?

2
  • Learn about the Reflection API. Commented Aug 29, 2012 at 11:07
  • Thanks everyone, a special thanks to those who have suggested alternative architectures. I’ll check it out as soon as I can. I chosen the most straightforward answer as accepted one. Kind regards Commented Aug 29, 2012 at 12:02

7 Answers 7

6

You can use reflection to discover class' methods.

However in my opinion, it's a bad architecture. Better way to handle different exercises is creating an interface IExercise that will be implemented by *Exercise classes. Then create those objects, put them into an array and pick one randomely. Then call specified method from interface or something...

Sign up to request clarification or add additional context in comments.

Comments

3

Instead of storing the names you can store Method proxies

Method[] methods = getClass().getDeclaredMethods();

You need to go through these are ignore any method you add which are not tests.

Comments

3

I highy recomend looking up a Strategy Pattern:

http://en.wikipedia.org/wiki/Strategy_pattern

How would you apply it to your problem? Just create objects that imlement common interfaces (one that gives you method for creating the exercise) and use List of this objects.

You will also practice a very useful pattern!

Comments

2

You can use Reflection API to check Available methods using

 Method[] methods= Class.forName("ClassTo test").getDeclaredMethods();

Having said that there are so many things can go wrong while invoking a method.

You can avoid it by just having

interface  IExercise
{
    void createExercise();
}
class Exercise1 implements IExercise
{
    @Override
    public void createExercise()
    {
    }
}

And then you can use IExercise[] to generate Exercise Randomly.

1 Comment

Probably because In Java you can not generate method at runtime is misleading, possibly because ClassName.class.getDeclaredMethods() is shorter and less prone to error. (disclaimer: wasn't me)
1

I think you are safer by using the Command pattern and storing each exercise as a command class implementing a marker interface (ICommand). Afterwards you can use reflection to detect at run-time all classes that implement ICommand on the classpath to have a list of exercises you can run. This would also save you from having to add every new type of exercise you design in a collection in your random selection code.

http://en.wikipedia.org/wiki/Command_pattern

This is somewhat similar to the Strategy pattern suggested below.

Comments

0

Create objects like:

MathExample math = new MathExample();

And add them to ArrayList<Objects>

Comments

0

I think, it is better to create chain of responsibility and stroe commands in array. Randomly choose command and put to the chain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.