@matfah/

Checker FR 2008

Java

No description

fork
loading
Files
  • Main.java
  • AndChecker.java
  • Checker.java
  • PartC.java
  • SubstringChecker.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      testSubstringChecker();
      
    for(int i = 0; i < 10; i++)
      testAndChecker();
      
    testPartC();
      
    System.out.println("All tests passed!");
  }
  
  public static void checkAllInstanceVariablesArePrivate(Class c) {
    Field[] fields = c.getDeclaredFields();
    for(Field f: fields) {
      if((f.getModifiers() & Modifier.PRIVATE) != Modifier.PRIVATE)
      {
        System.out.println("Not all fields of " + c.getName() + " are private!");
        System.exit(0);
      }
    }
  }
  
  public static void testSubstringChecker() {
    checkAllInstanceVariablesArePrivate(SubstringChecker.class);
    
    Constructor<SubstringChecker> cons = null;
    
    try {
      cons = SubstringChecker.class.getConstructor(String.class);
    }
    catch(Exception e) {
      System.out.println("SubstringChecker does not have a one parameter String constructor");
      System.exit(0);
    }
    
    SubstringChecker check = null;
    String[] toMatch = {"the", "pizza", "apple", "city"};
    String[] phrases = {"it's the end of the world as we know it", 
                        "did someone say pizza?",
                        "an apple a day keeps doctor away",
                        "we built this city on rock and roll!"};
    
    int matchPos = (int)(Math.random()*toMatch.length);
    int phrasePos = (int)(Math.random()*phrases.length);
    boolean switchAroo = Math.random() < 0.5;
    
    try {
      if(!switchAroo)
        check = cons.newInstance(toMatch[matchPos]);
      else
        check = cons.newInstance(phrases[phrasePos]);
    }
    catch(Exception e) {
      System.out.println("Error creating an instance of a SubstringChecker");
      System.exit(0);
    }
    
    
    //now, there isn't a way to see if they've initializes their fields correctly,
    //but let's make sure they initialize at least one of them!
    Field[] fields = SubstringChecker.class.getDeclaredFields();
    boolean didOne = false;
    for(Field f: fields) {
      f.setAccessible(true);
      Object value = null;

      //I'm not sure how this would generate an error...
      try {
        value = f.get(check);
      }
      catch(Exception e) {}
      
      if(value != null)
        didOne = true;
    }
    
    //this doesn't check if they initialized it correctly... 
    //that's almost impossible to tell...
    if(!didOne) {
      System.out.println("SubstringChecker constructor does not initialize an instance variable!");
      System.exit(0);
    }
    
    boolean result = false;
    
    try {
      if(!switchAroo)
        result = check.accept(phrases[phrasePos]);
      else
        result = check.accept(toMatch[matchPos]);
    }
    catch(Exception e) {
      System.out.println("Error calling accept() method of SubstringChecker class");
      System.exit(0);
    }
    
    if((switchAroo && result) || (!switchAroo && result != (matchPos == phrasePos))) {
      System.out.println("Wrong result returned by accept() method of SubstringChecker class");
      System.out.println(switchAroo + " " + result + " " + matchPos + " " + phrasePos);
      System.exit(0);
    }
    
  }
  
  
  public static void testAndChecker() {
    checkAllInstanceVariablesArePrivate(AndChecker.class);
    
    Constructor<AndChecker> cons = null;
    
    try {
      cons = AndChecker.class.getConstructor(Checker.class, Checker.class);
    }
    catch(Exception e) {
      System.out.println("AndChecker does not have a two parameter Checker constructor");
      System.exit(0);
    }
    
    AndChecker check = null;
    String[] toMatch = {"the", "pizza", "apple", "city"};
    String[] phrases = {"it's the end of the world as we know it", 
                        "did someone say pizza?",
                        "an apple a day keeps the doctor away",
                        "we built this city on rock and roll!"};
    
    int matchPosOne = (int)(Math.random()*toMatch.length);
    int matchPosTwo = (int)(Math.random()*toMatch.length);
    int phrasePos = (int)(Math.random()*phrases.length);

    NotChecker a = null;
    NotChecker b = null;

    try {
      a = new NotChecker(new SubstringChecker(toMatch[matchPosOne]));
      b = new NotChecker(new SubstringChecker(toMatch[matchPosTwo]));
      check = cons.newInstance(a, b);
    }
    catch(Exception e) {
      System.out.println("Error creating an instance of an AndChecker");
      System.exit(0);
    }
    
    
    //now, there isn't a way to see if they've initializes their fields correctly,
    //but let's make sure they initialize at least one of them!
    Field[] fields = AndChecker.class.getDeclaredFields();
    boolean didOne = false;
    for(Field f: fields) {
      f.setAccessible(true);
      Object value = null;

      //I'm not sure how this would generate an error...
      try {
        value = f.get(check);
      }
      catch(Exception e) {}
      
      if(value != null)
        didOne = true;
    }
    
    //this doesn't check if they initialized it correctly... 
    //that's almost impossible to tell...
    if(!didOne) {
      System.out.println("AndChecker constructor does not initialize an instance variable!");
      System.exit(0);
    }
    
    boolean result = false;
    
    try {
      result = check.accept(phrases[phrasePos]);
    }
    catch(Exception e) {
      System.out.println("Error calling accept() method of AndChecker class");
      System.exit(0);
    }
    
    //I do an and because you actually don't need to call both if the result should be false
    if(!a.called && !b.called) {
      System.out.println("AndChecker class does not call accept() method for each of the Checker objects it contains");
      System.exit(0);
    }
    
    if(result != (phrases[phrasePos].indexOf(toMatch[matchPosOne]) < 0 && phrases[phrasePos].indexOf(toMatch[matchPosTwo]) < 0)) {
      System.out.println("Wrong result returned by accept() method of AndChecker class");
      System.exit(0);
    }
    
  }
  
  
  public static void testPartC() {
    PartC c = new PartC();
    
    try {
      c.writeYourCodeForPartCHere();
    }
    catch(Exception e) {
      System.out.println("Error initializing the yummyChecker");
      System.exit(0);
    }
    
    if(c.yummyChecker == null) {
      System.out.println("yummyChecker has not been initialized");
      System.exit(0);
    }
    
    String[] toCheck = {"this is the song that never ends",
                        "kale is the most pretentious vegitable",
                        "artichokes hearts are delicious",
                        "would you rather have kale soup or artichokes soup"};
    
    boolean[] desiredResults = {true, false, false, false};
    
    for(int i = 0; i < toCheck.length; i++) {
      if(c.yummyChecker.accept(toCheck[i]) != desiredResults[i]) {
        System.out.println("yummyChecker's accept method is not returning the correct value");
        System.exit(0);
      }
    }
    
    c = new PartC();
    c.aChecker = new SubstringChecker("song");
    c.kChecker = new SubstringChecker("delicious");
    
    c.writeYourCodeForPartCHere();
    desiredResults = new boolean[]{false, true, false, true};
    
    for(int i = 0; i < toCheck.length; i++) {
      if(c.yummyChecker.accept(toCheck[i]) != desiredResults[i]) {
        System.out.println("It looks like you are not initializing your yummyChecker using the variables aChecker and kChecker!");
        System.exit(0);
      }
    }
  }
  
}

class NotChecker implements Checker {
  private Checker a;
  boolean called = false;
  
  public NotChecker(Checker a) {
    this.a = a;
  }
  
  public boolean accept(String text) {
    called = true;
    return !a.accept(text);
  }
}

class OrChecker implements Checker {
  private Checker[] a;
  
  public OrChecker(Checker... x) {
    a = x;
  }
  
  public boolean accept(String text) {
    for(Checker c: a) {
      if(c.accept(text))
        return true;
    }
    return false;
  }
}