-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.java
More file actions
52 lines (42 loc) · 1.73 KB
/
Executor.java
File metadata and controls
52 lines (42 loc) · 1.73 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
// Wenlu Cheng, CSE 373
// assignment #1, ID:1336340
// This file is assigning questions to oracles in random order and print out their
// answers
public class Executor {
public static void main(String[] args) {
Utility.init(); // initializes file readers
String[] questions = Utility.readQuestions(); //reads question.txt file into questions array
String[] answers = Utility.readAnswers(); // reads answers.txt file into answers array
int numOracles = answers.length; //finds the number of oracles
// 1. Initialize one ArrayQueue per oracle
ArrayQueue[] oracles = new ArrayQueue[numOracles];
for(int i = 0; i < numOracles; i++){
oracles[i] = new ArrayQueue();
}
// 2. Put the questions into the queues,assigning each one
//to the queue of the oracle in random order
for(int i= 0; i < questions.length; i++){
String temp = questions[i];
int number = Utility.random(numOracles);
oracles[number].enqueue(temp);
}
// 3. Loop through the oracles, having each one remove a question from
//its queue (if empty do nothing) and answer it with its unique
//answer repeatedly until all queues are empty
//find the longest element in the array(that is how many time
// it has to loop for every arrayQueue in the element)
int max = 0;
for(int i = 0; i < oracles.length; i++ ){
max = Math.max(oracles[i].getSize(), max);
}
// loop max time to print every question inside the arrayQueues
// if it is empty just skip it.
for(int i = 0; i < questions.length * max; i++){
if(!oracles[i % numOracles].isEmpty()){
String tempQuestion = oracles[i % numOracles].dequeue();
String tempAnswer = answers[i % numOracles];
System.out.println(tempQuestion + ": " + tempAnswer);
}
}
}
}