-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulationDriver.java
93 lines (83 loc) · 3.55 KB
/
SimulationDriver.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
import java.util.ArrayList;
import java.util.Random;
/**
* @author jmcgr
*SimulationDriver facilitates a simulation of a random number of students answers both multiple and single choice questions
*/
public class SimulationDriver {
public static void main(String[] args) {
//sets the min/max number of students to be randomly created, and the total questions that can be asked
final int MIN_NUM_OF_STUDENTS = 6;
final int MAX_NUM_OF_STUDENTS = 11;
final int TOTAL_QUESTIONS = 10;
//variable to identify which question type has been randomly chosen by the driver
int questionChosen;
//create objects and list for the students to be created
Question sChoice = new SingleChoiceQuestion();
Question mChoice = new MultipleChoiceQuestion();
Random randomNum = new Random();
ArrayList<Student> listOfStudents = new ArrayList<Student>();
//randomly choose the number of students to be created
int numOfStudents = randomNum.nextInt(MAX_NUM_OF_STUDENTS - MIN_NUM_OF_STUDENTS) + MIN_NUM_OF_STUDENTS;
//create VotingService object from the amount of students created
VotingService service = new VotingService(numOfStudents);
//populates a list of students
generateList(numOfStudents, listOfStudents);
//prints basic info to the screen before asking questions
System.out.println("There are " + service.getTotalStudents() + " total students");
System.out.println("All unique student ID's:\n");
for(int i = 0; i < listOfStudents.size(); i++) {
System.out.println(listOfStudents.get(i).getStudentID());
}
System.out.println();
//prints the question randomly chosen to the screen and simulates each student making a choice
for(int i = TOTAL_QUESTIONS; i > 0; --i) {
int presentQ = 0;
questionChosen = passQtoStudent(chooseQuestion(sChoice, mChoice, service));
for(int j = 0; j < listOfStudents.size(); j++) {
listOfStudents.get(j).makeChoice(questionChosen);
}
//prints which students made which choices for each question
service.printQResults(listOfStudents, presentQ, questionChosen);
}
//prints total results once all questions have been asked and answered
service.printTotalResults(listOfStudents);
}
//populates the list of students based on random number of students chosen to be created
public static void generateList(int numOfStudents, ArrayList<Student> listOfStudents) {
for(int i = 1; i <= numOfStudents; i++) {
Student studentToBeAdded = new Student();
listOfStudents.add(studentToBeAdded);
}
}
//randomly chooses which question will be asked next
public static int chooseQuestion(Question questionType0, Question questionType1, VotingService service) {
Random randNum = new Random();
int randQuestion;
if(service.getmultQsLeft() > 0 && service.getSingQsLeft() > 0) {
randQuestion = randNum.nextInt(2);
}
else if(service.getSingQsLeft() == 0) {
randQuestion = 1;
}
else if(service.getmultQsLeft() == 0) {
randQuestion = 0;
}
else
randQuestion = -1;
if(randQuestion == 0 && service.getSingQsLeft() != 0) {
questionType0.getQuestion(service.getSingQsLeft());
service.decSingQsLeft();
}
if(randQuestion == 1 && service.getmultQsLeft() != 0) {
questionType1.getQuestion(service.getmultQsLeft());
service.decMultQsLeft();
}
return randQuestion;
}
//returns the value(0/1) to be passed to each student in order to identify which set of answers are possible
public static int passQtoStudent(int choice) {
int questionChosen = choice;
return questionChosen;
}
}