1

I have this assignement for school for whitch i have to make a small program that will ask you a question, list the possible answers and you have to input the number of the answer. It would not be that much of a problem if not for two things:

-the answers have to show in different order every time the question is asked

-the teacher gave us a specific class/methode structure we have to follow

  • input/output
  • "logic" class with:
    • constructor (String question, String[] answers, int indexOfTheCorrectAnswer)
    • public String getQuestion
    • public String[] getAnswers
    • public String getCorrectAnswer
    • public boolean controlAnswer
    • private void randomize

Could you guys hint me a way to do the randomizing? The rest isnt that much of a problem to handle

4 Answers4

1

Collections have shuffle methods, so make an ArraList from your own Test class.

Laszlo Lugosi
  • 3,669
  • 1
  • 21
  • 17
1

templated Collections.shuffle is the method that you could use.

Actually you should go with this only as it was specifically designed for that. Underlying is the fairly optimized shuffle algorithm. Though you can customize the algorithm by overriding specific methods.

http://www.tutorialspoint.com/java/util/collections_shuffle.htm This is an example that you can refer which shuffles the list.

Hope that helps.

Sameer Sawla
  • 729
  • 6
  • 20
0

Look at java.util.Random for random number generation.

Random rnd = new Random(new Date().getTime());

The newDate() part seeds the random number generator so it won't have the exact same values every time you run your program.

I would then do the following:

  1. In my class, make a pointer to the right answer before randomizing.
  2. Copy the answers into an ArrayList.
  3. Then do a loop. While there are still contents in the array from #2, generate a random number 0 <= rnd < count and use that as an index in that array to copy the value into a new array.

You now have the answers randomized.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
0
String[] answers = new String[5];

//assume your array gets populated here...

//convert to a List so we can use Collections.shuffle()
List<String> answersList = Arrays.asList(answers);
Collections.shuffle(answersList);

//converting back to an array (although I'd avoid working with arrays alltogether if possible)
String[] shuffledAnswers = answersList.toArray(answers);
kwikness
  • 1,425
  • 4
  • 21
  • 37
  • Nice. good u are using shuffle as well. – Sameer Sawla Mar 12 '15 at 18:10
  • 1
    You could just use answersList.toArray(answers); instead of assign the result to a new array – dragon66 Mar 12 '15 at 18:26
  • Thanks, i tried to use your code and changed what i though i had to change to names of my own variables but i couldnt get it to work. But i snooped around the google and figured out how to make those array lists and stuff and this is what i ended up with ArrayList antwoordenList = new ArrayList(Arrays.asList(antwoorden)); Collections.shuffle(antwoordenList); shuffledAntwoorden = antwoordenList.toArray(shuffledAntwoorden); return; – Karol Rafalski Mar 13 '15 at 10:44