forked from Harshil1823/Learning-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
114 lines (100 loc) · 2.72 KB
/
Question.java
File metadata and controls
114 lines (100 loc) · 2.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author JavaDoc
* Represents question on test.
*/
public class Question {
private String questionText;
private ArrayList<String> choices;
private int correctChoiceIndex;
private boolean completed;
private static Scanner keyboard = new Scanner(System.in);
/**
* Constructor to initialize a Question.
*
* @param questionText String of question.
* @param choices ArrayList<String> of choices.
* @param correctChoiceIndex String of correct choice.
*/
public Question(String questionText, ArrayList<String> choices, int correctChoiceIndex) {
this.questionText = questionText;
this.choices = choices;
this.correctChoiceIndex = correctChoiceIndex;
this.completed = false;
}
/**
* Returns the question text.
*
* @return String of question.
*/
public String getQuestionText() {
return questionText;
}
/**
* Returns choices of question.
*
* @return ArrayList<String> of choices.
*/
public ArrayList<String> getChoices() {
return choices;
}
/**
* Returns correct choice.
*
* @return String of correct choice.
*/
public int getCorrectChoice() {
return correctChoiceIndex;
}
public boolean getCompleted(){
return completed;
}
/**
* Set the question.
*
* @param questionText String of question.
*/
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
/**
* Set choices for question.
*
* @param choices ArrayList<String> of choices.
*/
public void setChoices(ArrayList<String> choices) {
this.choices = choices;
}
/**
* Set of correct choices.
*
* @param correctChoice String of correct choice.
*/
public void setCorrectChoice(int choice) {
this.correctChoiceIndex = choice;
}
/**
* Allows user to answer a question.
*
* @return true if correct answer, false otherwise.
*/
public boolean answerQuestion() {
System.out.print("Question: ");
System.out.println(questionText);
System.out.println("Choose from the following choices by number: ");
for (int i = 1; i <= choices.size(); i++) {
System.out.print(i + ". ");
System.out.println(choices.get(i - 1));
}
int choice = keyboard.nextInt();
keyboard.nextLine();
if (correctChoiceIndex == choice - 1) {
completed = true;
return true;
} else {
completed = true;
return false;
}
}
}