This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleChoiceQuestion.java
More file actions
62 lines (46 loc) · 1.71 KB
/
MultipleChoiceQuestion.java
File metadata and controls
62 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cs1120;
import javax.swing.JOptionPane;
public class MultipleChoiceQuestion {
static int nQuestions = 0;
static int nCorrect = 0;
String question;
String correctAnswer;
// data members of the class.
MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) {
question = query+"\n";
question += "A. "+a+"\n";
question += "B. "+b+"\n";
question += "C. "+c+"\n";
question += "D. "+d+"\n";
question += "E. "+e+"\n";
correctAnswer= answer.toUpperCase();
}
// ask method that takes a question and return answer in upper case
String ask() {
while (true) {
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
boolean valid = (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E"));
if (valid) return answer;
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
}
}
// method check that take question and check the correct answer
void check() {
// increment questions each time
nQuestions ++;
//a method "ask" that asks the question
String answer = ask();
if (answer.equals(correctAnswer)) {
// increment correct answers for correct answers
nCorrect ++;
JOptionPane.showMessageDialog(null, "Correct!");
}else {
JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is "+ correctAnswer);
}
}
static void showResults() {
// show the number of questions and correct answers
JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + " questions ");
}
}