Created
February 14, 2017 03:00
-
-
Save skooltch84/4dfc4a07553564297686ed4a0ac0e56c to your computer and use it in GitHub Desktop.
Shuffle objects in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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