Skip to content

Instantly share code, notes, and snippets.

@skooltch84
Created February 14, 2017 03:00
Show Gist options
  • Save skooltch84/4dfc4a07553564297686ed4a0ac0e56c to your computer and use it in GitHub Desktop.
Save skooltch84/4dfc4a07553564297686ed4a0ac0e56c to your computer and use it in GitHub Desktop.
Shuffle objects in an array
package objects;
import java.util.Arrays;
import java.util.Collections;
public class Quizbook {
String mQuestion;
boolean mAnswer;
public Quizbook(String question, boolean answer){
this.mQuestion = question;
this.mAnswer = answer;
}
public String getQuestion(){
return mQuestion;
}
public boolean isAnswer(){
return mAnswer;
}
//Create the objects
private static Quizbook m01 = new Quizbook("Magnets have two poles", true);
private static Quizbook m02 = new Quizbook("Magnets have an East pole and a West pole", false);
private static Quizbook m03 = new Quizbook("The earth is not magnetic", false);
//add Objects to an array
public static Quizbook [] magnetism = new Quizbook [] {
m01, m02, m03
};
public static void shuffleQuestions(){
Collections.shuffle(Arrays.asList(magnetism));
}
public static void main(String[] args) {
shuffleQuestions();
System.out.println(magnetism[0].getQuestion());
System.out.println(magnetism[0].isAnswer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment