package exercise.choice; import java.io.*; import java.util.*; import widget.*; // Uses widget.ChoicePane to provide model constants. public class ChoiceExercise { public static final int CHOOSE_ONE = ChoicePane.CHOOSE_ONE; public static final int CHOOSE_ALL = ChoicePane.CHOOSE_ALL; protected String exerciseID; protected String instructions; protected int type; protected String[] choices; public ChoiceExercise(String exerciseID, String instructions, int type, String[] choices) { this.exerciseID = exerciseID; this.type = type; this.instructions = instructions; this.choices = choices; } public String getExerciseID() { return exerciseID; } // For now this just returns the exercise ID. Ultimately it should // derive the exercise name from the id and return the derived name. public String getExerciseName() { return exerciseID; } public String getInstructions() { return instructions; } public int getType() { return type; } public String[] getChoices() { return choices; } public static ChoiceExercise loadExercise(String exerciseID) throws IOException { // Load the contents of the Exercise file. Class cl = ChoiceExercise.class; String resourceName = "exercises/" + exerciseID + ".txt"; InputStream is = cl.getResourceAsStream(resourceName); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); // Get the instructions and type. String instr = br.readLine(); int type = Integer.parseInt(br.readLine()); // Get the choices. Vector choiceLines = new Vector(); String line; while ((line = br.readLine()) != null) { choiceLines.add(line); } int numChoices = choiceLines.size(); String[] choices = new String[numChoices]; for (int i = 0; i < numChoices; i++) { choices[i] = (String)choiceLines.get(i); } // Close the reader. br.close(); // Return an Exercise object. return new ChoiceExercise(exerciseID, instr, type, choices); } }