@matfah/

Seating Chart FR 2014

Java

No description

fork
loading
Files
  • Main.java
  • SeatingChart.java
  • Student.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8007621468268336","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLnJlZmxlY3QuKjsKCnB1YmxpYyBjbGFzcyBNYWluIHsKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICB0ZXN0Q29uc3RydWN0b3IoKTsKICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQ29uc3RydWN0b3IgdGVzdHMgcGFzc2VkISIpOwogICAgdGVzdFJlbW92ZUFic2VudFN0dWRlbnRzKCk7CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oInJlbW92ZUFic2VudFN0dWRlbnRzKCkgdGVzdHMgcGFzc2VkISIpOwogICAgU3lzdGVtLm91dC5wcmludGxuKCJBbGwgdGVzdHMgcGFzc2VkISIpOwogICAgCiAgfQogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCB0ZXN0Q29uc3RydWN0b3IoKSB7CiAgICAKICAgIEZpZWxkIHNlYXRzRmllbGQgPSBudWxsOwogICAgdHJ5IHsKICAgICAgc2VhdHNGaWVsZCA9IFNlYXRpbmdDaGFydC5jbGFzcy5nZXREZWNsYXJlZEZpZWxkKCJzZWF0cyIpOwogICAgfQogICAgY2F0Y2goRXhjZXB0aW9uIGUpIHsKICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciB0cnlpbmcgdG8gYWNjZXNzIFNlYXRpbmdDaGFydCdzIHNlYXRzIGluc3RhbmNlIHZhcmlhYmxlIik7CiAgICAgIFN5c3RlbS5leGl0KDApOwogICAgfQogICAgc2VhdHNGaWVsZC5zZXRBY2Nlc3NpYmxlKHRydWUpOwogICAgCiAgICBmb3IoaW50IHJvd3MgPSAxOyByb3dzIDwgNjsgcm93cysrKSB7CiAgICAgIGZvcihpbnQgY29scyA9IDE7IGNvbHMgPCA2OyBjb2xzKyspIHsKICAgICAgICBmb3IoaW50IHN0dWRlbnRzID0gMDsgc3R1ZGVudHMgPCByb3dzKmNvbHM7IHN0dWRlbnRzKyspIHsKICAgICAgICAgIAogICAgICAgICAgQXJyYXlMaXN0PFN0dWRlbnQ+IHN0dWRlbnRzTGlzdCA9IG5ldyBBcnJheUxpc3Q8U3R1ZGVudD4oKTsKICAgICAgICAgIFN0dWRlbnRbXVtdIGdvYWwgPSBuZXcgU3R1ZGVudFtyb3dzXVtjb2xzXTsKICAgICAgICAgIAogICAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IHN0dWRlbnRzOyBpKyspIHsKICAgICAgICAgICAgU3RyaW5nIG5hbWUgPSAoY2hhcikoJ0EnICsgaSAlIDI2KSArICIiOwogICAgICAgICAgICBpbnQgYWJzID0gKGludCkoTWF0aC5yYW5kb20oKSoyMCk7CiAgICAgICAgICAgIHN0dWRlbnRzTGlzdC5hZGQobmV3IEN1c3RvbVN0dWRlbnQobmFtZSwgYWJzKSk7CiAgICAgICAgICAgIGdvYWxbaSAlIHJvd3NdW2kgLyByb3dzXSA9IHN0dWRlbnRzTGlzdC5nZXQoc3R1ZGVudHNMaXN0LnNpemUoKS0xKTsKICAgICAgICAgIH0KICAgICAgICAgIAogICAgICAgICAgU3RyaW5nIHN0dWRlbnRzTGlzdFN0ciA9ICIiOwogICAgICAgICAgZm9yKFN0dWRlbnQgeDogc3R1ZGVudHNMaXN0KQogICAgICAgICAgICBzdHVkZW50c0xpc3RTdHIgKz0geCArICIgIjsKICAgICAgICAgIAogICAgICAgICAgU2VhdGluZ0NoYXJ0IGNoYXJ0ID0gbnVsbDsKICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgIGNoYXJ0ID0gbmV3IFNlYXRpbmdDaGFydChzdHVkZW50c0xpc3QsIHJvd3MsIGNvbHMpOwogICAgICAgICAgfQogICAgICAgICAgY2F0Y2goRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciBjcmVhdGluZyBzZWF0aW5nIGNoYXJ0IHdpdGggIiArIHN0dWRlbnRzICsgIiBzdHVkZW50cywgIiArIHJvd3MgKyAiIHJvd3MsIGFuZCAiICsgY29scyArICIgY29sdW1ucyIpOwogICAgICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgICAgIH0KICAgICAgICAgIAogICAgICAgICAgU3RyaW5nIHN0dWRlbnRzTGlzdFN0cjIgPSAiIjsKICAgICAgICAgIGZvcihTdHVkZW50IHg6IHN0dWRlbnRzTGlzdCkKICAgICAgICAgICAgc3R1ZGVudHNMaXN0U3RyMiArPSB4ICsgIiAiOwogICAgICAgICAgICAKICAgICAgICAgIGlmKCFzdHVkZW50c0xpc3RTdHIuZXF1YWxzKHN0dWRlbnRzTGlzdFN0cjIpKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQ29uc3RydWN0aW5nIFNlYXRpbmdDaGFydCBzaG91bGQgTk9UIG1vZGlmeSB0aGUgcGFyYW1ldGVyIGxpc3QiKTsKICAgICAgICAgICAgU3lzdGVtLmV4aXQoMCk7CiAgICAgICAgICB9CiAgICAgICAgICAKICAgICAgICAgIFN0dWRlbnRbXVtdIHNlYXRzID0gbnVsbDsKICAgICAgICAgIAogICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgc2VhdHMgPSAoU3R1ZGVudFtdW10pc2VhdHNGaWVsZC5nZXQoY2hhcnQpOwogICAgICAgICAgfQogICAgICAgICAgY2F0Y2goRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciB0cnlpbmcgdG8gYWNjZXNzIFNlYXRpbmdDaGFydCdzIHNlYXRzIGluc3RhbmNlIHZhcmlhYmxlIik7CiAgICAgICAgICAgIFN5c3RlbS5leGl0KDApOwogICAgICAgICAgfQogICAg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3dzKyspIHsKICAgICAgZm9yKGludCBjb2xzID0gMTsgY29scyA8IDY7IGNvbHMrKykgewogICAgICAgIGZvcihpbnQgc3R1ZGVudHMgPSAwOyBzdHVkZW50cyA8IHJvd3MqY29sczsgc3R1ZGVudHMrKykgewogICAgICAgICAgCiAgICAgICAgICBBcnJheUxpc3Q8U3R1ZGVudD4gc3R1ZGVudHNMaXN0ID0gbmV3IEFycmF5TGlzdDxTdHVkZW50PigpOwogICAgICAgICAgU3R1ZGVudFtdW10gZ29hbCA9IG5ldyBTdHVkZW50W3Jvd3NdW2NvbHNdOwogICAgICAgICAgCiAgICAgICAgICBpbnQgdGhyZXNob2xkID0gKGludCkoTWF0aC5yYW5kb20oKSoyMCk7CiAgICAgICAgICBpbnQgY291bnQgPSAwOwogICAgICAgICAgCiAgICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgc3R1ZGVudHM7IGkrKykgewogICAgICAgICAgICBTdHJpbmcgbmFtZSA9IChjaGFyKSgnQScgKyBpICUgMjYpICsgIiI7CiAgICAgICAgICAgIGludCBhYnMgPSAoaW50KShNYXRoLnJhbmRvbSgpKjIwKTsKICAgICAgICAgICAgc3R1ZGVudHNMaXN0LmFkZChuZXcgQ3VzdG9tU3R1ZGVudChuYW1lLCBhYnMpKTsKICAgICAgICAgICAgaWYodGhyZXNob2xkID49IHN0dWRlbnRzTGlzdC5nZXQoc3R1ZGVudHNMaXN0LnNpemUoKS0xKS5nZXRBYnNlbmNlQ291bnQoKSkKICAgICAgICAgICAgICBnb2FsW2kgJSByb3dzXVtpIC8gcm93c10gPSBzdHVkZW50c0xpc3QuZ2V0KHN0dWRlbnRzTGlzdC5zaXplKCktMSk7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgfQogICAgICAgICAgCiAgICAgICAgICBTdHJpbmcgc3R1ZGVudHNMaXN0U3RyID0gIiI7CiAgICAgICAgICBmb3IoU3R1ZGVudCB4OiBzdHVkZW50c0xpc3QpCiAgICAgICAgICAgIHN0dWRlbnRzTGlzdFN0ciArPSB4ICsgIiAiOwogICAgICAgICAgCiAgICAgICAgICBTZWF0aW5nQ2hhcnQgY2hhcnQgPSBuZXcgU2VhdGluZ0NoYXJ0KHN0dWRlbnRzTGlzdCwgcm93cywgY29scyk7CiAgICAgICAgICAKICAgICAgICAgIGludCByZXN1bHRDb3VudCA9IDA7CiAgICAgICAgICAKICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgIHJlc3VsdENvdW50ID0gY2hhcnQucmVtb3ZlQWJzZW50U3R1ZGVudHModGhyZXNob2xkKTsKICAgICAgICAgIH0KICAgICAgICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRXJyb3IgY2FsbGluZyByZW1vdmVBYnNlbnRTdHVkZW50cygpIik7CiAgICAgICAgICAgIFN5c3RlbS5leGl0KDApOwogICAgICAgICAgfQogICAgICAgICAgCiAgICAgICAgICBTdHVkZW50W11bXSBzZWF0cyA9IG51bGw7CiAgICAgICAgICAKICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgIHNlYXRzID0gKFN0dWRlbnRbXVtdKXNlYXRzRmllbGQuZ2V0KGNoYXJ0KTsKICAgICAgICAgIH0KICAgICAgICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRXJyb3IgdHJ5aW5nIHRvIGFjY2VzcyBTZWF0aW5nQ2hhcnQncyBzZWF0cyBpbnN0YW5jZSB2YXJpYWJsZSIpOwogICAgICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgICAgIH0KICAgICAgICAgIAogICAgICAgICAgaWYoIWphdmEudXRpbC5BcnJheXMuZGVlcEVxdWFscyhnb2FsLCBzZWF0cykpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJXaGVuIGNhbGxpbmcgcmVtb3ZlQWJzZW50U3R1ZGVudHMoIiArIHRocmVzaG9sZCArICIpIG9uIGEgU2VhdGluZ0NoYXJ0IHRoYXQgaW5pdGlhbGx5IGhhZCBhIGxpc3Qgb2YgIiArIHN0dWRlbnRzTGlzdCArICIgYW5kICIgKyByb3dzICsgIiByb3dzICBhbmQgIiArIGNvbHMgKyAiIGNvbHMsIHRoZSByZXN1bHRhbnQgc2VhdGluZyBjaGFydCBpczpcblxuIiArIGphdmEudXRpbC5BcnJheXMuZGVlcFRvU3RyaW5nKHNlYXRzKSArICJcblxuYnV0IHNob3VsZCBiZTpcblxuIiArIGphdmEudXRpbC5BcnJheXMuZGVlcFRvU3RyaW5nKGdvYWwpKTsKICAgICAgICAgICAgU3lzdGVtLmV4aXQoMCk7CiAgICAgICAgICB9CiAgICAgICAgICAKICAgICAgICAgIGlmKGNvdW50ICE9IHJlc3VsdENvdW50KSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiV2hlbiBjYWxsaW5nIHJlbW92ZUFic2VudFN0dWRlbnRzKCIgKyB0aHJlc2hvbGQgKyAiKSBvbiBhIFNlYXRpbmdDaGFydCB0aGF0IGluaXRpYWxseSBoYWQgYSBsaXN0IG9mICIgKyBzdHVkZW50c0xpc3QgKyAiIGFuZCAiICsgcm93cyArICIgcm93cyAgYW5kICIgKyBjb2xzICsgIiBjb2xzLCB0aGUgcmV0dXJuZWQgdmFsdWUgaXMgIiArIHJlc3VsdENvdW50ICsgIiwgYnV0IHNob3VsZCBiZSAiICsgY291bnQpOwogICAgICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgICAgIH0KICAgICAgICAgIAogICAgICAgICAgCiAgICAgICAgfQogICAgICAgIAogICAgICB9CiAgICB9CiAgfQoKfQoKY2xhc3MgQ3VzdG9tU3R1ZGVudCBleHRlbmRzIFN0dWRlbnQgewogIAogIFN0cmluZyBuYW1lOwogIGludCBhYnNlbmNlczsKICAKICBwdWJsaWMgQ3VzdG9tU3R1ZGVudChTdHJpbmcgbiwgaW50IGEpIHsKICAgIG5hbWUgPSBuOwogICAgYWJzZW5jZXMgPSBhOwogIH0KICAKICBwdWJsaWMgU3RyaW5nIGdldE5hbWUoKSB7CiAgICByZXR1cm4gbmFtZTsKICB9CiAgCiAgcHVibGljIGludCBnZXRBYnNlbmNlQ291bnQoKSB7CiAgICByZXR1cm4gYWJzZW5jZXM7CiAgfQogIAogIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7CiAgICByZXR1cm4gbmFtZSArICIgIiArIGFic2VuY2VzOwogIH0KfQ=="},"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
import java.util.*;
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    testConstructor();
    System.out.println("Constructor tests passed!");
    testRemoveAbsentStudents();
    System.out.println("removeAbsentStudents() tests passed!");
    System.out.println("All tests passed!");
    
  }
  
  public static void testConstructor() {
    
    Field seatsField = null;
    try {
      seatsField = SeatingChart.class.getDeclaredField("seats");
    }
    catch(Exception e) {
      System.out.println("Error trying to access SeatingChart's seats instance variable");
      System.exit(0);
    }
    seatsField.setAccessible(true);
    
    for(int rows = 1; rows < 6; rows++) {
      for(int cols = 1; cols < 6; cols++) {
        for(int students = 0; students < rows*cols; students++) {
          
          ArrayList<Student> studentsList = new ArrayList<Student>();
          Student[][] goal = new Student[rows][cols];
          
          for(int i = 0; i < students; i++) {
            String name = (char)('A' + i % 26) + "";
            int abs = (int)(Math.random()*20);
            studentsList.add(new CustomStudent(name, abs));
            goal[i % rows][i / rows] = studentsList.get(studentsList.size()-1);
          }
          
          String studentsListStr = "";
          for(Student x: studentsList)
            studentsListStr += x + " ";
          
          SeatingChart chart = null;
          try {
            chart = new SeatingChart(studentsList, rows, cols);
          }
          catch(Exception e) {
            System.out.println("Error creating seating chart with " + students + " students, " + rows + " rows, and " + cols + " columns");
            System.exit(0);
          }
          
          String studentsListStr2 = "";
          for(Student x: studentsList)
            studentsListStr2 += x + " ";
            
          if(!studentsListStr.equals(studentsListStr2)) {
            System.out.println("Constructing SeatingChart should NOT modify the parameter list");
            System.exit(0);
          }
          
          Student[][] seats = null;
          
          try {
            seats = (Student[][])seatsField.get(chart);
          }
          catch(Exception e) {
            System.out.println("Error trying to access SeatingChart's seats instance variable");
            System.exit(0);
          }
          
          if(!java.util.Arrays.deepEquals(goal, seats)) {
            System.out.println("When creating a SeatingChart with students named " + studentsList + " and " + rows + " rows  and " + cols + " cols, the created seats array is:\n\n" + java.util.Arrays.deepToString(seats) + "\n\nbut should be:\n\n" + java.util.Arrays.deepToString(goal));
            System.exit(0);
          }
          
          
        }
        
      }
    }
    
  }
  
  public static void testRemoveAbsentStudents() {
    Field seatsField = null;
    try {
      seatsField = SeatingChart.class.getDeclaredField("seats");
    }
    catch(Exception e) {
      System.out.println("Error trying to access SeatingChart's seats instance variable");
      System.exit(0);
    }
    seatsField.setAccessible(true);
    
    for(int rows = 1; rows < 6; rows++) {
      for(int cols = 1; cols < 6; cols++) {
        for(int students = 0; students < rows*cols; students++) {
          
          ArrayList<Student> studentsList = new ArrayList<Student>();
          Student[][] goal = new Student[rows][cols];
          
          int threshold = (int)(Math.random()*20);
          int count = 0;
          
          for(int i = 0; i < students; i++) {
            String name = (char)('A' + i % 26) + "";
            int abs = (int)(Math.random()*20);
            studentsList.add(new CustomStudent(name, abs));
            if(threshold >= studentsList.get(studentsList.size()-1).getAbsenceCount())
              goal[i % rows][i / rows] = studentsList.get(studentsList.size()-1);
            else
              count++;
          }
          
          String studentsListStr = "";
          for(Student x: studentsList)
            studentsListStr += x + " ";
          
          SeatingChart chart = new SeatingChart(studentsList, rows, cols);
          
          int resultCount = 0;
          
          try {
            resultCount = chart.removeAbsentStudents(threshold);
          }
          catch(Exception e) {
            System.out.println("Error calling removeAbsentStudents()");
            System.exit(0);
          }
          
          Student[][] seats = null;
          
          try {
            seats = (Student[][])seatsField.get(chart);
          }
          catch(Exception e) {
            System.out.println("Error trying to access SeatingChart's seats instance variable");
            System.exit(0);
          }
          
          if(!java.util.Arrays.deepEquals(goal, seats)) {
            System.out.println("When calling removeAbsentStudents(" + threshold + ") on a SeatingChart that initially had a list of " + studentsList + " and " + rows + " rows  and " + cols + " cols, the resultant seating chart is:\n\n" + java.util.Arrays.deepToString(seats) + "\n\nbut should be:\n\n" + java.util.Arrays.deepToString(goal));
            System.exit(0);
          }
          
          if(count != resultCount) {
            System.out.println("When calling removeAbsentStudents(" + threshold + ") on a SeatingChart that initially had a list of " + studentsList + " and " + rows + " rows  and " + cols + " cols, the returned value is " + resultCount + ", but should be " + count);
            System.exit(0);
          }
          
          
        }
        
      }
    }
  }

}

class CustomStudent extends Student {
  
  String name;
  int absences;
  
  public CustomStudent(String n, int a) {
    name = n;
    absences = a;
  }
  
  public String getName() {
    return name;
  }
  
  public int getAbsenceCount() {
    return absences;
  }
  
  public String toString() {
    return name + " " + absences;
  }
}