Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter way to call common method on randomly selected object in java
    text
    copied!<p>Nice day to everybody.</p> <p>I have an abstract class with the method runRandomExercise(), and several classes that extends it to add different kind of exercise. </p> <p>I now want to chose a random type exercise, so I need to randomly choose one of the classes, and call runRandomExercise() on that.</p> <p>For now I am manually coding this, which is not the very best solution I think. However, I can’t store just the classes in the array since the class type is different, and if I use object[] I can’t call the runRandomExercise() method. Any smart way to handle this?</p> <p>Here is my code till now. It works, but it’s gonna be a pain to add other classes...</p> <pre><code>/*Specific classes that extend abstract class TrainingClass with the runRandomExercise() method*/ private MatheMagic mMathMag; private Mnemonics mMnemonics; private String[] mTrainingClasses; /*Initialize classes*/ mMathMag = new MatheMagic(); mMnemonics = new Mnemonics(); /*Manually store classe names*/ mTrainingClasses = new String[2]; mTrainingClasses[0] = "mMathMag"; mTrainingClasses[1] = "mMnemonics"; /*Return random exercise*/ public String[] RandomExercise() { Random aGenerator = new Random(); /*Get random class name*/ int rnd = aGenerator.nextInt(mTrainingClasses.length); String aChosen = mTrainingClasses[rnd]; String[] aRes = new String[2]; if (aChosen == "mMathMag") { aRes = mMathMag.runRandomExercise(); } else if (aChosen == "mMnemonics") { aRes = mMnemonics.runRandomExercise(); } return aRes; } </code></pre> <p>EDIT Here is how TrainingClass is defined: </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 mRandGen = new Random(); public ArrayList&lt;Exercise&gt; mExerciseTypes = new ArrayList&lt;Exercise&gt;(); /** Run a random exercise */ public String[] runRandomExercise() { int i = mRandGen.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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload