@matfah/

AP Score FR 2007

Java

No description

fork
loading
Files
  • Main.java
  • StudentAnswerSheet.java
  • TestResults.java
Main.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
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
import java.util.ArrayList;
import java.lang.reflect.*;

public class Main {
  
  public static String[] names = {"Alice", "Bob", "Cathy", "David", "Erin", "Frank", "Gina"};
  
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      testGetScore();
    System.out.println("Finished testing getScore() method - all tests passed!");
      
    for(int i = 0; i < 10; i++)
      testHighestScoringStudent();
    System.out.println("Finished testing highestScoringStudent() method - all tests passed!");
      
    System.out.println("Passed All Tests!");
  }
  
  public static void testGetScore() {
    
    CustomStudentAnswerSheet sheet = new CustomStudentAnswerSheet("Bob");
    
    Field answersField = null;
    try {
      answersField = StudentAnswerSheet.class.getDeclaredField("answers");
    }
    catch(Exception e) {
      System.out.println("Did you delete the answers instance variable...???");
      System.exit(0);
    }
    answersField.setAccessible(true);
    
    ArrayList<String> key = new ArrayList<String>();
    randomSheet(key, true);
    
    ArrayList<String> sheetList = new ArrayList<String>();
    double goal = randomScoredSheet(sheetList, key);
    
    try {
      answersField.set(sheet, sheetList);
    }
    catch(Exception e) {
      System.out.println("Did you change the answers instance variable...???");
      System.exit(0);
    }
    
    String keyString = key.toString();
    String sheetString = sheetList.toString();
    
    double result = 0.0;
    try {
      result = sheet.getScore(key);
    }
    catch(Exception e) {
      System.out.println("Error calling getScore()");
      System.exit(0);
    }
    
    if(!key.toString().equals(keyString) || !sheetList.toString().equals(sheetString)) {
      System.out.println("getScore() should not modify the parameters!");
      System.exit(0);
    }
    
    if(Math.abs(result - goal) > 0.0001) {
      System.out.println("getScore() is returning the wrong value!");
      System.exit(0);
    }
  }
  
  public static void randomSheet(ArrayList<String> sheet, boolean key) {
    
    int size = (int)(Math.random()*10) + 5;
    for(int i = 0; i < size; i++) {
      int rand = (int)(Math.random()*6);
      
      switch(rand) {
        case 0: sheet.add("A"); break;
        case 1: sheet.add("B"); break;
        case 2: sheet.add("C"); break;
        case 3: sheet.add("D"); break;
        case 4: sheet.add("E"); break;
        case 5: if(!key) sheet.add("?");
                else i--; 
                break;
      }
    }
    
  }
  
  static String options = "ABCDEF?";
  public static double randomScoredSheet(ArrayList<String> sheet, ArrayList<String> key) {
    
    double score = 0.0;
    
    for(int i = 0; i < key.size(); i++) {
      
      String answer = key.get(i);
      
      if(Math.random() < 1/3.0) {
        sheet.add(answer);
        score += 1.0;
      }
      else if(Math.random() < 1/2.0) {
        sheet.add("?");
      }
      else {
        
        String choice = answer;
        while(choice.equals(answer) || choice.equals("?")) {
          int spot = (int)(Math.random()*options.length());
          choice = options.substring(spot, spot+1);
        }
        sheet.add(choice);
        
        score -= 0.25;
        
      }
    }
    return score;
    
  }
  
  public static void testHighestScoringStudent() {
    Field sheetsField = null;
    
    TestResults tResults = new TestResults();
    
    try {
      sheetsField =TestResults.class.getDeclaredField("sheets");
    }
    catch(Exception e) {
      System.out.println("Did you delete the sheets instance variable...???");
      System.exit(0);
    }
    sheetsField.setAccessible(true);
    
    Field answersField = null;
    try {
      answersField = StudentAnswerSheet.class.getDeclaredField("answers");
    }
    catch(Exception e) {
      System.out.println("Did you delete the answers instance variable...???");
      System.exit(0);
    }
    answersField.setAccessible(true);
    
    ArrayList<StudentAnswerSheet> studentSheets = new ArrayList<StudentAnswerSheet>();
    ArrayList<String> key = new ArrayList<String>();
    randomSheet(key, true);
    
    String bestStud = null;
    double bestScore = 0.0;
    
    for(int i = (int)(Math.random()*10)+5; i >= 0; i--) {
      ArrayList<String> newAnswers = new ArrayList<String>();
      double value = randomScoredSheet(newAnswers, key);
      String name = names[(int)(Math.random()*names.length)];
      
      CustomStudentAnswerSheet sheet = new CustomStudentAnswerSheet(name);
      
      try {
        answersField.set(sheet, newAnswers);
      }
      catch(Exception e) {
        System.out.println("Did you change the answers instance variable...???");
        System.exit(0);
      }
      studentSheets.add(sheet);
      
      if(value > bestScore) {
        bestScore = value;
        bestStud = name;
      }
    }
    
    try {
      sheetsField.set(tResults, studentSheets);
    }
    catch(Exception e) {
      System.out.println("Did you change the sheets instance variable...???");
      System.exit(0);
    }
    
    //to make sure they didn't muck around with the lists
    String ssString = "";
    for(StudentAnswerSheet sheet: studentSheets) {
      try {
        ssString += sheet.getName() + answersField.get(sheet);
      }
      catch(Exception e) {
        System.out.println("Why can't I access the sheets field?");
        System.exit(0);
      }
    }
    for(String x : key) {
      ssString += x;
    }
    
    String chosen = "";
    try {
      chosen = tResults.highestScoringStudent(key);
    }
    catch(Exception e) {
      System.out.println("Error calling highestScoringStudent()");
      System.exit(0);
    }
    
    if(!bestStud.equals(chosen)) {
      System.out.println("highestScoringStudent() is not returning the right name");
      System.exit(0);
    }
    
    String postssString = "";
    for(StudentAnswerSheet sheet: studentSheets) {
      try {
        postssString += sheet.getName() + answersField.get(sheet);
      }
      catch(Exception e) {
        System.out.println("Why can't I access the sheets field?");
        System.exit(0);
      }
    }
    for(String x : key) {
      postssString += x;
    }
    
    if(!postssString.equals(ssString)) {
      System.out.println("highestScoringStudent() should not modify the parameters!");
      System.exit(0);
    }
    
  }
  
}


class CustomStudentAnswerSheet extends StudentAnswerSheet {

  String name;

  public CustomStudentAnswerSheet(String n) {
    name = n;
  }

  //no implementation was given in the original problem.  Amazingly, this method 
  //will not return null when my tester calls it!  It's magic...
  public String getName() {
    return name;
  }

}