-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathClassroom.java
More file actions
151 lines (117 loc) · 5.37 KB
/
Classroom.java
File metadata and controls
151 lines (117 loc) · 5.37 KB
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
package io.zipcoder;
import java.util.*;
public class Classroom {
private Student[] students;
private int maxNumOfStudents;
public Classroom() {
this.students = new Student[30];
}
public Classroom(int maxNumOfStudents) {
this.students = new Student[maxNumOfStudents];
}
public Classroom(Student... students) {
this.students = students;
}
public Student[] getStudents() {
return students;
} //do I need to clone?
public double getAverageExamScore() {
double total = 0;
for (Student s : students) {
total += s.getAverageExamScore();
}
double average = total / correctForNull();
return average;
}
public void addStudent(Student student) {
for (int i = 0; i < students.length; i++) {
if (students[i] == null) {
students[i] = student;
break;
}
}
}
// ArrayList <Student> newEnrollment = new ArrayList<>(Arrays.asList(students));
// newEnrollment.add(student);
// this.students = newEnrollment.toArray(new Student[0]);
public void removeStudentbyName(String firstName, String lastName) {
ArrayList<Student> studentArray = new ArrayList<>(Arrays.asList(students));
for (int i = 0; i < studentArray.size(); i++) {
Student student = studentArray.get(i);
if (student == null) {
continue;
} else if (student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
studentArray.remove(student);
studentArray.add(null);
}
}
this.students = studentArray.toArray(new Student[0]);
}
// ArrayList<Student> studentArray = new ArrayList<>(Arrays.asList(students));
// for (int i = 0; i < studentArray.size() ; i++) {
// Student student = studentArray.get(i);
// if (student.getLastName().equalsIgnoreCase(lastName) && student.getFirstName().equalsIgnoreCase(firstName)) {
// studentArray.set(student);
// }
// } studentArray.add(null);
public Student[] getStudentByScore() {
List<Student> studentbyScore = new ArrayList<>(Arrays.asList(students));
Comparator<Student> byExamScores = Comparator.comparing(Student::getAverageExamScore);
Comparator<Student> byFullName = Comparator.comparing(Student::getFullName);
studentbyScore.sort(byExamScores.reversed().thenComparing(byFullName));
return studentbyScore.toArray(new Student[0]);
}
public Map <Student, Character> getGradeBookMap() {
Student[] studentArray = students;
Map<Student, Character> gradeBookAssigned = new HashMap<>();
for (int i = 0; i < correctForNull(); i++) {
gradeBookAssigned.put(studentArray[i], assignGrade(studentArray[i]));
}
return gradeBookAssigned;
}
public void getGradeBook(){
for (int i = 0; i < correctForNull(); i++) {
char grade = assignGrade(students[i]);
String name = students[i].getFullName();
System.out.println(name + " -> " + grade);
}
}
public char assignGrade(Student student){
double studentAvg = student.getAverageExamScore();
double classAvg = getAverageExamScore();
double deviation = getDeviation();
if (studentAvg >= classAvg + (2 * deviation)){
return 'A';
} else if (studentAvg >= classAvg + deviation) {
return 'B';
} else if (studentAvg >= classAvg){
return 'C';
}else if (studentAvg >= classAvg - deviation){
return 'D';
}
return 'F';
}
public double getDeviation() {
double deviation = 0;
double classAverage = getAverageExamScore();
for (Student s : students) {
if (s == null) {
deviation += 0;
} else {
double diff = classAverage - s.getAverageExamScore();
deviation += Math.pow(diff, 2);
}
}
double squaredDev = deviation / correctForNull();
return Math.sqrt(squaredDev);
}
public int correctForNull() {
int studentCount = 0;
for (Student student : students) {
if (student != null) {
studentCount++;
}
}
return studentCount;
}
}