diff options
author | Camil Staps | 2015-04-18 13:44:44 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-18 13:44:44 +0200 |
commit | 6a44b074f0169a1b0f9e92347af929c5e471746e (patch) | |
tree | ae5663fe7c69881bf4ecfedbef99c2505f8ec964 /Week5 Quiz/src/week5/MCQuestion.java | |
parent | Added copyright to docs (diff) |
Reorganised projects
Diffstat (limited to 'Week5 Quiz/src/week5/MCQuestion.java')
-rw-r--r-- | Week5 Quiz/src/week5/MCQuestion.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Week5 Quiz/src/week5/MCQuestion.java b/Week5 Quiz/src/week5/MCQuestion.java new file mode 100644 index 0000000..86869c5 --- /dev/null +++ b/Week5 Quiz/src/week5/MCQuestion.java @@ -0,0 +1,81 @@ +package week5; + +import java.util.Random; + +/** + * Class for multiple choice questions + * + * Note: for 2-choice questions the TCQuestion class should be used. + * + * @author Camil Staps, s4498062 + */ +public class MCQuestion extends Question { + + protected final String[] answers; + protected final int correctAnswer; + + /** + * Construct a new instance + * + * @param question the question + * @param answers array of the answer + * @param correctAnswer index for answers pointing to the correct answer + * @param weight weight of the question + */ + public MCQuestion (String question, String[] answers, int correctAnswer, int weight) { + this.question = question; + this.answers = answers; + this.correctAnswer = correctAnswer; + setWeight(weight); + } + + /** See the javadoc on the constructor above */ + public MCQuestion (String question, String[] answers, int correctAnswer) { + this.question = question; + this.answers = answers; + this.correctAnswer = correctAnswer; + } + + @Override + public boolean isCorrect(String answer) { + return correctAnswer == (answer.charAt(0) - 'A'); + } + + @Override + public String juisteAntwoord() { + return answers[correctAnswer]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()); + sb.append("\n"); + int i = 0; + for (String a : answers) { + sb.append((char) ('A' + i++)).append(": ").append(a).append("\n"); + } + sb.deleteCharAt(sb.length() - 1); + return sb.toString(); + } + + /** + * For this type of questions, duplicating randomizes the order of the answers. + * + * @return A new instance of the MCQuestion class with the same parameters, just another order of the answers. + */ + @Override + public Question duplicate() { + Random r = new Random(); + int shift = r.nextInt(answers.length - 1) + 1; + + String[] new_answers = new String[answers.length]; + for (String a : answers) { + new_answers[shift++ % answers.length] = a; + } + + int new_correctAnswer = (correctAnswer + shift) % answers.length; + + return new MCQuestion(question, new_answers, new_correctAnswer, weight); + } + +} |