-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.java
165 lines (162 loc) · 4.65 KB
/
Question.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//test
import java.lang.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
interface JQuestion {
void setQuestion2(String question);
void setAnswer(int i, String answer);
void setCorrectAnswer(int i);
void scrambleAnswers();
String toString2(int qNumber);
int getCorrectAnswer();
int incQNumber(int qNumber);
void addQuestion(JQuestion question);
String makeKey(int qNumber);
}
public class Question implements JQuestion
{
private String question;
private ArrayList <String> answers;
private int correctAnswer;
/*Question()
* constructor
* Olie Spohngellert*/
public Question()
{
//sets the question
question = "";
//creates the array list
answers = new ArrayList<String>(0);
for(int i = 0; i<5; i++)
answers.add(i, new String());
//creates the correctAnswer
correctAnswer = -1;
}
//getAnswers()
//gets the answer array list
public ArrayList<String> getAnswers()
{
return answers;
}
/*public void setAnswers
* Sets the answers*/
public void setAnswers(ArrayList<String> answers2)
{
answers = answers2;
stripEmpty();
}
/*public String getCorrectAnswer
* gets the correct answer*/
public int getCorrectAnswer()
{
return correctAnswer;
}
/*setCorrectAnswer
* sets the correct answer*/
public void setCorrectAnswer(int answer)
{
correctAnswer = answer;
}
/*getQuestion
* gets the question*/
public String getQuestion()
{
return question;
}
//Question(String question1, ArrayList<String> answers1, String answerIndex1)
//alternate constructor
public Question(String question1, ArrayList<String> answers1, int answerIndex1)
{
question = question1;
answers=answers1;
correctAnswer=answerIndex1;
}
/*setQuestion2
* sets the question*/
public void setQuestion2(String question2)
{
question = question2;
}
public void addQuestion(JQuestion question) {
throw new RuntimeException("Not right");
}
/*setAnswer(int i, String answer1
* sets a specific answer
* i-index of answer(-1), answer-answer to be set to*/
public void setAnswer(int i, String answer1)
{
answers.set(i, answer1);
}
/*toString
* makes the answer into a string*/
public String toString()
{
//strips out the empty answers from the arraylist
stripEmpty();
//Creates a string, adds the question and a new line
String returnable = question + "\r\n";
for(int i = 0; i<answers.size(); i++)
//adds all the answers and a new line each time
returnable+= i+1 + ")" + answers.get(i) + "\r\n";
//returns
return returnable + "\r\n";
}
/*toString2
* alternate toString formulator*/
public String toString2(int qNumber)
{
//strips out empty answers
stripEmpty();
//adds the question
String returnable = qNumber + ". " + question + "\r\n";
for(int i = 0; i<answers.size(); i++) {
//adds the answers
returnable+= "\t" + (i+1) + ")" + answers.get(i) + "\r\n";
}
//returns
return returnable + "\r\n";
}
public int incQNumber(int qNumber) {
return qNumber + 1;
}
public String makeKey(int qNumber) {
return qNumber + ". " + correctAnswer + "\r\n";
}
//scrambles the answers within the code
public void scrambleAnswers() {
//creates a random
Random random = new Random();
for(int i = 0; i<(answers.size()*2); i++)
{
//random indecies
int r1 = random.nextInt(10000)%answers.size();
int r2 = random.nextInt(10000)%answers.size();
int ans = correctAnswer - 1;
//checks if the correct answer is or isn't one of the others, if it is it resets it
if(ans == r1) {
correctAnswer = r2 + 1;
}
if(ans == r2) {
correctAnswer = r1 + 1;
}
//switches the answers
String temp = answers.get(r1);
answers.set(r1, answers.get(r2));
answers.set(r2, temp);
}
}
//strips out all empty answers
public void stripEmpty() {
ArrayList<String> temp = new ArrayList<String>(0);
for(int i = 0; i<answers.size(); i++) {
if(answers.get(i).equals("")) {
}
else {
temp.add(answers.get(i));
}
}
answers = temp;
}
}