@matfah/

HorseBarn FR 2012

Java

No description

fork
loading
Files
  • Main.java
  • Horse.java
  • HorseBarn.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
class Main {
    
    //test code to make sure findHorseSpace and consildate work properly
    public static void main(String[] args) {
    
        int score = 0;
        
        score += testFindHorseSpace(1, 1);
        score += testFindHorseSpace(2, 2);
        score += testFindHorseSpace(1, 2);
        score += testFindHorseSpace(4, 10);
        score += testFindHorseSpace(7, 100);
        score += testFindHorseSpace(12, 1000);
        
        score /= 3;
        
        if(testConsolidate(1, 1)) score++;
        if(testConsolidate(3, 5)) score++;
        if(testConsolidate(4, 10)) score++;
        if(testConsolidate(5, 25)) score++;
        if(testConsolidate(14, 1000)) score++;
        if(testConsolidate(105, 10000)) score++;
        
        System.out.println("Final Score: " + score + " / 12");
    
    }
    
    //tests whether the consolidate method works properly
    private static boolean testConsolidate(int n, int length) {
        Horse[] spots = new Horse[length];
        fillWithNHorses(n, spots);
        System.out.println("Testing consolidate() with array length " + length + " and " + n + " horses");
        Horse[] copy = copyArray(spots);
        
        HorseBarn barn = new HorseBarn(spots);
        barn.consolidate();
        
        int barnPos = 0;
        int originalPos = 0;
        boolean alreadyFoundNull = false;
        
        while(barnPos < spots.length) {
            if(spots[barnPos] != null) {
                if(alreadyFoundNull) { //BAD!  can't have any horses after null
                    System.out.println("  FAILURE: a horse was found after a null");
                    System.exit(0);
                    return false;
                }
                while(originalPos < copy.length && copy[originalPos] == null) {
                    originalPos++;
                }
                if(originalPos == copy.length) { //we didn't find the matching horse
                    System.out.println("  FAILURE: a new horse was added to the barn... somehow...");
                    System.exit(0);
                    return false;
                }
                else if(spots[barnPos] != copy[originalPos]) { //the next horse doesn't match, so the order must be wrong
                    System.out.println("  FAILURE: the horses are not in the same order as in the beginning");
                    System.exit(0);
                    return false;
                }
                originalPos++;
            }
            else {
                alreadyFoundNull = true;
            }
            barnPos++;
        }
        
        while(originalPos < copy.length) {
            if(copy[originalPos] != null) { //missed some of the original horses
                System.out.println("  FAILURE: a horse has escaped the barn!");
                System.exit(0);
                return false;
            }
            originalPos++;
        }
        
        System.out.println("  Success in consildate()");
        return true;
    }
    
    //tests whether the findHorseSpace method works properly
    private static int testFindHorseSpace(int n, int length) {
        
        Horse[] spots = new Horse[length];
        fillWithNHorses(n, spots);
        System.out.println("Testing findHorseSpace() with array length " + length + " and " + n + " horses");
        Horse[] copy = copyArray(spots);
        int score = 0;
        
        HorseBarn barn = new HorseBarn(spots);
        
        int pos;
        pos = barn.findHorseSpace('a' + "");
        if(pos == -1) {
            System.out.println("  FAILURE: result was -1, but the horse is in the barn!");
            System.exit(0);
        }
        else if(copy[pos] == null) {
            System.out.println("  FAILURE: result was a position that holds a null value!");
            System.exit(0);
        }
        else if(!copy[pos].getName().equals("a")) {
            System.out.println("  FAILURE: result was a position that holds a Horse that has the wrong name");
            System.exit(0);
        }
        else {
            System.out.println("  Passed findHorseSpace() part I");
            score++;
        }
        
        pos = barn.findHorseSpace((char)('a' + n/2) + "");
        String horseName = (char)('a' + n/2) + "";
        if(pos == -1) {
            System.out.println("  FAILURE: result was -1, but the horse is in the barn!");
            System.exit(0);
        }
        else if(copy[pos] == null) {
            System.out.println("  FAILURE: result was a position that holds a null value!");
            System.exit(0);
        }
        else if(!copy[pos].getName().equals((char)('a' + n/2) + "")) {
            System.out.println("  FAILURE: result was a position that holds a Horse with the wrong name!");
            System.exit(0);
        }
        else {
            System.out.println("  Passed findHorseSpace() Part II");
            score++;
        }
        
        pos = barn.findHorseSpace((char)('a' + n) + "");
        horseName = (char)('a' + n) + "";
        if(pos == -1) {
            System.out.println("  Passed findHorseSpace() Part III");
            score++;
        }
        else {
            System.out.println("  FAILURE: found position but result should have been -1");
            System.exit(0);
        }
        return score;
    }
    
    //fills an array of Horses with n Horses, naming Horses from the alphabet (a, b, c...).  Horse positions are randomized (as are the weights)
    private static void fillWithNHorses(int n, Horse[] spots) {
        char next = 'a';
        while(n > 0) {
            int rand = (int)(Math.random()*spots.length);
            if(spots[rand] == null) {
                int randWeight = (int)(Math.random()*10)+10;
                spots[rand] = new MyHorse(next + "", randWeight);
                next++;
                n--;
            }
        }
    }
    
    //makes a shallow copy of the array of Horses
    private static Horse[] copyArray(Horse[] spots) {
        Horse[] copy = new Horse[spots.length];
        for(int i = 0; i < spots.length; i++)
            copy[i] = spots[i];
        return copy;
    }
    
    //returns a String representation of the horse array
    private static String horsesToStrings(Horse[] spots) {
        String answer = "";
        
        for(Horse h: spots) {
            if(h == null)
                answer += "null ";
            else
                answer += h.getName() + " ";
        }
        
        return answer;
    }
}

class MyHorse implements Horse {
  
  private String name;
  private int weight;
  
  public MyHorse(String n, int w) {
    name= n;
    weight = w;
  }
  
  public String getName() {
    return name;
  }
  
  public int getWeight() {
    return weight;
  }
  
}