-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingSimulator.java
More file actions
45 lines (36 loc) · 1.36 KB
/
VotingSimulator.java
File metadata and controls
45 lines (36 loc) · 1.36 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
import java.util.HashMap;
import java.util.ArrayList;
public class VotingSimulator {
public HashMap<String, ArrayList<Integer>> studentAnswers = new HashMap<>();
public Question question;
// default constructor
public VotingSimulator(Question question){
this.question = question;
}
// displays the question and choices
public void displayQuestion(){
System.out.println(question);
}
// records a students answer(s), stores in hashmap
public void recordAnswer(Student student){
studentAnswers.put(student.getID(), student.getAnswer());
}
// shows the statistics of each question using frequency map
public void showStats(){
HashMap<Integer, Integer> frequencyMap = new HashMap<>();
ArrayList<String> choices = new ArrayList<>();
choices.addAll(question.getChoices());
for(ArrayList<Integer> value : studentAnswers.values()){
for(int answer : value){
if(frequencyMap.containsKey(answer)) {
frequencyMap.put(answer, frequencyMap.get(answer) + 1);
} else{
frequencyMap.put(answer, 1);
}
}
}
for(int choice : frequencyMap.keySet()){
System.out.println(choices.get(choice - 1) + ": " + frequencyMap.get(choice));
}
}
}