@matfah/

Pet FR 2004

Java

No description

fork
loading
Files
  • Main.java
  • Cat.java
  • Kennel.java
  • LoudDog.java
  • Pet.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7562074886313879","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEubGFuZy5yZWZsZWN0Lio7CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlMaXN0OwppbXBvcnQgamF2YS5pby4qOwoKcHVibGljIGNsYXNzIE1haW4gewogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIHRlc3RDYXQoKTsKICAgIHRlc3RMb3VkRG9nKCk7CiAgICB0ZXN0S2VubmVsKCk7CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oIllvdSBwYXNzZWQgYWxsIHRoZSB0ZXN0cyEiKTsKICB9CiAgCiAgcHVibGljIHN0YXRpYyB2b2lkIHRlc3RDYXQoKSB7CiAgICAKICAgIGNoZWNrQWxsSW5zdGFuY2VWYXJpYWJsZXNBcmVQcml2YXRlKENhdC5jbGFzcyk7CiAgICAKICAgIENvbnN0cnVjdG9yPENhdD4gY2F0Q29uc3QgPSBudWxsOwogICAgdHJ5IHsKICAgICAgY2F0Q29uc3QgPSBDYXQuY2xhc3MuZ2V0RGVjbGFyZWRDb25zdHJ1Y3RvcihTdHJpbmcuY2xhc3MpOwogICAgfQogICAgY2F0Y2goRXhjZXB0aW9uIGUpIHsKICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJZb3VyIGNhdCBjbGFzcyBkb2VzIG5vdCBoYXZlIGEgMSBwYXJhbWV0ZXIgY29uc3RydWN0b3IhIik7CiAgICAgIFN5c3RlbS5leGl0KDApOwogICAgfQogICAgCiAgICBDYXQgYUNhdCA9IG51bGw7CiAgICB0cnkgewogICAgICBhQ2F0ID0gY2F0Q29uc3QubmV3SW5zdGFuY2UoImZsdWZmeSIpOwogICAgfQogICAgY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sYXN0SW5kZXhPZihzcGVhayk7CiAgICAgICAgCiAgICAgICAgaWYocG9zMCA9PSAtMSB8fCBwb3MxID09IC0xKSB7CiAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIktlbm5lbCdzIGFsbFNwZWFrKCkgaGFzIGluY29ycmVjdCBvdXRwdXQhIik7CiAgICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgICB9CiAgICAgIH0KICAgIH0KICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgIGUucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgIFN5c3RlbS5leGl0KDApOwogICAgfQogIH0KICAKICBwdWJsaWMgc3RhdGljIHZvaWQgY2hlY2tBbGxJbnN0YW5jZVZhcmlhYmxlc0FyZVByaXZhdGUoQ2xhc3MgYykgewogICAgRmllbGRbXSBmaWVsZHMgPSBjLmdldERlY2xhcmVkRmllbGRzKCk7CiAgICBmb3IoRmllbGQgZjogZmllbGRzKSB7CiAgICAgIGlmKChmLmdldE1vZGlmaWVycygpICYgTW9kaWZpZXIuUFJJVkFURSkgIT0gTW9kaWZpZXIuUFJJVkFURSkKICAgICAgewogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTm90IGFsbCBmaWVsZHMgb2YgIiArIGMuZ2V0TmFtZSgpICsgIiBhcmUgcHJpdmF0ZSEiKTsKICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgfQogICAgfQogIH0KfQoKY2xhc3MgRG9nIGV4dGVuZHMgUGV0IHsgcHVibGljIERvZyhTdHJpbmcgbmFtZSkgeyBzdXBlcihuYW1lKTsgfSBwdWJsaWMgU3RyaW5nIHNwZWFrKCkgeyByZXR1cm4gImFyZiBhcmYiOyB9IH0="},"asBuffer":null},"loaded":true}}
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
import java.lang.reflect.*;
import java.util.ArrayList;
import java.io.*;

public class Main {
  public static void main(String[] args) {
    testCat();
    testLoudDog();
    testKennel();
    System.out.println("You passed all the tests!");
  }
  
  public static void testCat() {
    
    checkAllInstanceVariablesArePrivate(Cat.class);
    
    Constructor<Cat> catConst = null;
    try {
      catConst = Cat.class.getDeclaredConstructor(String.class);
    }
    catch(Exception e) {
      System.out.println("Your cat class does not have a 1 parameter constructor!");
      System.exit(0);
    }
    
    Cat aCat = null;
    try {
      aCat = catConst.newInstance("fluffy");
    }
    catch(Exception e) {
      System.out.println("Error trying to construct a Cat object!");
      System.exit(0);
    }
    
    try {
      Field myName = Pet.class.getDeclaredField("myName");
      myName.setAccessible(true);
      String name = (String)myName.get(aCat);
      if(name == null || !name.equals("fluffy")) {
        System.out.println("Cat Constructor does not properly call superclass constructor!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Did you change the Pet class...?!?!");
      System.exit(0);
    }
    
    Method getName = null;
    Method speak = null;
    try {
     getName = Pet.class.getDeclaredMethod("getName");
    }
    catch(Exception e) {
      System.out.println("Did you change the Pet class...?!?!");
      System.exit(0);
    }
    
    try {
     speak = Cat.class.getDeclaredMethod("speak");
    }
    catch(Exception e) {
      System.out.println("Your cat class does not have a speak() method!");
      System.exit(0);
    }
    
    if(!Pet.class.isAssignableFrom(Cat.class)) {
      System.out.println("Your cat class is not a subclass of Pet!");
      System.exit(0);
    }
    
    try {
      String result = (String)getName.invoke(aCat);
      if(result == null || !result.equals("fluffy")) {
        System.out.println("Result of calling getName() from a Cat object is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getName() from a Cat object!");
      System.exit(0);
    }
    
    try {
      String result = (String)speak.invoke(aCat);
      if(result == null || !result.equals("meow")) {
        System.out.println("Result of calling speak() from a Cat object is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling speak() from a Cat object!");
      System.exit(0);
    }
  }
  
  public static void testLoudDog() {
    
    checkAllInstanceVariablesArePrivate(LoudDog.class);
    
    Constructor<LoudDog> dogConst = null;
    try {
      dogConst = LoudDog.class.getDeclaredConstructor(String.class);
    }
    catch(Exception e) {
      System.out.println("Your LoudDog class does not have a 1 parameter constructor!");
      System.exit(0);
    }
    
    LoudDog aDog = null;
    try {
      aDog = dogConst.newInstance("fido");
    }
    catch(Exception e) {
      System.out.println("Error trying to construct a LoudDog object!");
      System.exit(0);
    }
    
    try {
      Field myName = Pet.class.getDeclaredField("myName");
      myName.setAccessible(true);
      String name = (String)myName.get(aDog);
      if(name == null || !name.equals("fido")) {
        System.out.println("LoudDog Constructor does not properly call superclass constructor!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Did you change the Pet class...?!?!");
      System.exit(0);
    }
    
    Method getName = null;
    Method speak = null;
    try {
     getName = Pet.class.getDeclaredMethod("getName");
    }
    catch(Exception e) {
      System.out.println("Did you change the Pet class...?!?!");
      System.exit(0);
    }
    
    try {
     speak = LoudDog.class.getDeclaredMethod("speak");
    }
    catch(Exception e) {
      System.out.println("Your LoudDog class does not have an overriden speak() method!");
      System.exit(0);
    }
    
    if(!Dog.class.isAssignableFrom(LoudDog.class)) {
      System.out.println("Your LoudDog class is not a subclass of Dog!");
      System.exit(0);
    }
    
    try {
      String result = (String)getName.invoke(aDog);
      if(result == null || !result.equals("fido")) {
        System.out.println("Result of calling getName() from a LoudDog object is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getName() from a LoutDog object!");
      System.exit(0);
    }
    
    try {
      String result = (String)speak.invoke(aDog);
      if(result == null || result.indexOf("arf arf") == result.lastIndexOf("arf arf")) {
        System.out.println("Result of calling speak() from a LoudDog object is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling speak() from a LoudDog object!");
      System.exit(0);
    }
  }
  
  public static void testKennel() {
    Kennel k = new Kennel();
    
    try {
      Field petListField = Kennel.class.getDeclaredField("petList");
      petListField.setAccessible(true);
      ArrayList<Pet> petList = new ArrayList<Pet>();
      petListField.set(k, petList);
      
      for(int i = (int)(Math.random()*10)+5; i >= 0; i--) {
        double r = Math.random();
        if(r < 1/3.0) petList.add(new Cat("blossom"));
        else if(r < 2/3.0) petList.add(new Dog("ralph"));
        else petList.add(new LoudDog("clifford"));
      }
      
      //System.out.println() redirection: 
      //http://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PrintStream ps = new PrintStream(baos);
      PrintStream old = System.out;
      System.setOut(ps);
      
      k.allSpeak();
      
      System.out.flush();
      System.setOut(old);

      String[] result = baos.toString().split("\n");
      
      if(result.length != petList.size()) {
            System.out.println("Kennel's allSpeak() outputs a different number of lines than there are pets in the list!");
            System.exit(0);
      }

      for(int i = 0; i < result.length; i++) {
        String name = petList.get(i).getName();
        String speak = petList.get(i).speak();
        
        int pos0 = result[i].indexOf(name);
        int pos1 = result[i].lastIndexOf(speak);
        
        if(pos0 == -1 || pos1 == -1) {
          System.out.println("Kennel's allSpeak() has incorrect output!");
          System.exit(0);
        }
      }
    }
    catch(Exception e) {
      e.printStackTrace();
      System.exit(0);
    }
  }
  
  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);
      }
    }
  }
}

class Dog extends Pet { public Dog(String name) { super(name); } public String speak() { return "arf arf"; } }