-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashCard.java
More file actions
254 lines (222 loc) · 7.1 KB
/
FlashCard.java
File metadata and controls
254 lines (222 loc) · 7.1 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package flashcards.model;
/**
* A class which models a flash card.
*
* @author Daniel Nedrow
*/
public class FlashCard {
private static final String DELIM = " @DL ";
private static final int QUESTION = 0, ANSWER = 1, NUM_ATTEMPTS = 2,
NUM_CORRECT = 3, NUM_INCORRECT = 4, LAST_5_ATTEMPTS = 5;
private String question, answer;
private int numAttempts, numCorrect, numIncorrect;
private int[] last5Attempts; // "1" for correct, "0" for incorrect
/**
* Create new FlashCard with given question and answer.
*
* @param question flash card question
* @param answer flash card answer
*/
public FlashCard(String question, String answer) {
this.question = question;
this.answer = answer;
numAttempts = numCorrect = numIncorrect = 0;
last5Attempts = new int[5];
}
/**
* @return the flash card's question
*/
public String getQuestion() {
return question;
}
/**
* @return the flash card's answer
*/
public String getAnswer() {
return answer;
}
/**
* Set the flash card's question.
*
* @param question the question to assign to this flash card
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* Set the flash card's answer
*
* @param answer the answer to assign to this flash card
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return the number of times user has attempted to answer this flash card
*/
public int getNumAttempts() {
return numAttempts;
}
/**
* @return the number of times user has answered this flash card correctly
*/
public int getNumCorrect() {
return numCorrect;
}
/**
* @return the number of times user has answered this flash card incorrectly
*/
public int getNumIncorrect() {
return numIncorrect;
}
/**
* Set the number of times the user has attempted this flash card.
*
* @param numAttempts number of times user has attempted this flash card
*/
public void setNumAttempts(int numAttempts) {
this.numAttempts = numAttempts;
}
/**
* Set number of times the user has answered this flash card correctly.
*
* @param numCorrect number of user's correct answers for flash card
*/
public void setNumCorrect(int numCorrect) {
this.numCorrect = numCorrect;
}
/**
* Set number of times user has answered this flash card incorrectly.
*
* @param numIncorrect number of user's incorrect answers for flash card
*/
public void setNumIncorrect(int numIncorrect) {
this.numIncorrect = numIncorrect;
}
/**
* @return right/wrong history of user's last 5 attempts
*/
public int[] getLast5Attempts() {
return last5Attempts;
}
/**
* Set user's right/wrong history of last 5 attempts on flash card.
*
* @param last5 array with right/wrong history of last 5 attempts
*/
public void setLast5Attempts(int[] last5) {
last5Attempts = last5;
}
/**
* Checks if user's answer is correct with String equality, and updates
* user's right/wrong history.
*
* @param attempt the user's answer
* @return true if correct, false otherwise
*/
public boolean autoCheckCorrect(String attempt) {
boolean correct;
if (answer.equalsIgnoreCase(attempt)) {
numCorrect++;
last5Attempts[numAttempts % 5] = 1;
correct = true;
} else {
numIncorrect++;
last5Attempts[numAttempts % 5] = 0;
correct = false;
}
numAttempts++;
return correct;
}
/**
* Accepts a boolean representing user's judgment on whether they got
* question right or wrong. For answers too complicated for String match.
* Updates user's right/wrong history.
*
* @param correct user's judgment whether question was correct or not
*/
public void userChecksAnswer(boolean correct) {
if (correct) {
numCorrect++;
last5Attempts[numAttempts % 5] = 1;
} else {
numIncorrect++;
last5Attempts[numAttempts % 5] = 0;
}
numAttempts++;
}
/**
* @return lifetime percentage of correct answers for this flash card
*/
public double getPercentCorrect() {
if (numAttempts == 0) {
return 0;
}
return (numCorrect * 1.0 / numAttempts) * 100;
}
/**
* @return percentage of correct answers over last 5 attempts
*/
public double getPercentCorrectLast5() {
if (numAttempts == 0) {
return 0;
}
int sum = 0;
for (int i = 0; i < 5 && i < numAttempts; i++) {
sum += last5Attempts[i];
}
int divisor = Math.min(numAttempts, 5); // user may not have 5 attempts
return (sum * 1.0 / divisor) * 100;
}
/**
* When this method is called it will reset the history of number of
* attempts, number correct, number incorrect, and last 5 attempts. clears
* the current card's score
*/
public void resetScore() {
numAttempts = 0;
numCorrect = 0;
numIncorrect = 0;
last5Attempts = new int[5];
}
/**
* Gives a String with all the data of a FlashCard, suitable for save file.
* This method is the inverse of the fromString method in that it produces a
* String format of a FlashCard which fromString can use to create the
* FlashCard.
*
* @return a String representing the FlashCard
*/
@Override
public String toString() {
StringBuilder flashcardString = new StringBuilder(question + DELIM
+ answer + DELIM + numAttempts + DELIM + numCorrect + DELIM
+ numIncorrect + DELIM);
for (int i = 0; i < 5; i++) {
flashcardString.append(last5Attempts[i]).append(" ");
}
flashcardString.append("\n");
return flashcardString.toString();
}
/**
* This method is the inverse of the toString method in that it takes the
* toString format of a FlashCard and returns the FlashCard object.
*
* @param flashcardString a String with all the data for a FlashCard
* @return the FlashCard with its data loaded from flashcardString
*/
public static FlashCard fromString(String flashcardString) {
String[] data = flashcardString.split(DELIM);
FlashCard flashcard = new FlashCard(data[QUESTION], data[ANSWER]);
flashcard.setNumAttempts(Integer.parseInt(data[NUM_ATTEMPTS]));
flashcard.setNumCorrect(Integer.parseInt(data[NUM_CORRECT]));
flashcard.setNumIncorrect(Integer.parseInt(data[NUM_INCORRECT]));
String[] last5 = data[LAST_5_ATTEMPTS].split(" ");
int[] last5Atmpts = new int[5];
for (int i = 0; i < 5; i++) {
last5Atmpts[i] = Integer.parseInt(last5[i]);
}
flashcard.setLast5Attempts(last5Atmpts);
return flashcard;
}
}