-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathClassroom.java
More file actions
139 lines (118 loc) · 4.68 KB
/
Classroom.java
File metadata and controls
139 lines (118 loc) · 4.68 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
package io.zipcoder;
import java.util.*;
public class Classroom {
private Student[] students;
private int maxNumberOfStudents;
public Classroom(){
this.students = new Student[30];
this.maxNumberOfStudents = 30;
this.initializeStudentArray();
}
public Classroom(int maxNumberOfStudents){
this.maxNumberOfStudents = maxNumberOfStudents;
this.students = new Student[maxNumberOfStudents];
this.initializeStudentArray();
}
public Classroom(Student[] students){
this.students = students;
}
private void initializeStudentArray() {
for(int i = 0; i < students.length; i++){
}
}
public Student[] getStudents() {
List<Student> studentList = new ArrayList<Student>(Arrays.asList(students));
while(studentList.remove(null)){}
return studentList.toArray(new Student[0]);
}
public double getAverageExamScore(){
double sum = 0.00;
Student[] students = this.getStudents();
for(Student element: students){
sum += element.getAverageExamScore();
}
return sum / actualStudentCount();
}
public int actualStudentCount(){
Integer studentCount = 0;
Student[] students = this.getStudents();
Integer length = students.length;
for(int i = 0; i < length; i++){
if(students[i] != null)
studentCount++;
}
return studentCount;
}
public void addStudent(Student student){
List<Student> studentArrayList;
if(this.getStudents().length > 0){
studentArrayList = new ArrayList<Student>(Arrays.asList(this.getStudents()));
} else {
studentArrayList = new ArrayList<Student>();
}
studentArrayList.add(student);
this.students = studentArrayList.toArray(new Student[0]);
}
public void removeStudent(String firstName, String lastName){
boolean adjust = false;
Student[] studentArray = this.getStudents();
for(int i = 0; i < studentArray.length; i++){
if(studentArray[i].getFirstName() == firstName){
if(studentArray[i].getLastName() == lastName){
studentArray[i] = null;
adjust = true;
}
}
}
if(adjust)
this.adjustForNull();
}
public void adjustForNull(){
Student[] studentArray = this.getStudents();
Student[] adjustedStudentArray = new Student[this.maxNumberOfStudents];
int lastIndex = 0;
for(int i = 0; i < studentArray.length; i++){
if(studentArray[i] != null){
adjustedStudentArray[lastIndex] = studentArray[i];
lastIndex++;
}
}
this.students = adjustedStudentArray;
}
public Student[] getStudentsByScore(){
List<Student> students = new ArrayList<>(Arrays.asList(this.getStudents()));
Comparator<Student> byScore = Comparator.comparing(Student::getAverageExamScore);
Comparator<Student> byFirstName = Comparator.comparing(Student::getLastName);
Comparator<Student> byLastName = Comparator.comparing(Student::getFirstName);
Collections.sort(students, byScore.thenComparing(byLastName).thenComparing(byFirstName));
Collections.reverse(students);
return students.toArray(new Student[0]);
}
public char getDeviationScore(Student student){
Double averageClassExamScore = this.getAverageExamScore();
Double averageStudentExamScore = student.getAverageExamScore();
Double preDeviation = Math.pow(averageStudentExamScore - averageClassExamScore, 2);
Double standardDeviation = Math.sqrt(preDeviation/(actualStudentCount() - 1));
if(averageStudentExamScore >= (averageClassExamScore + (standardDeviation * 2))){
return 'A';
} else if(averageStudentExamScore >= (averageClassExamScore + standardDeviation)){
return 'B';
} else if(averageStudentExamScore < (averageClassExamScore + standardDeviation) &&
averageStudentExamScore > averageClassExamScore){
return 'C';
} else if(averageStudentExamScore <= (averageClassExamScore + standardDeviation)){
return 'D';
} else {
return 'F';
}
}
public Map<Student, Character> getGradeBook(){
Student[] studentlist = this.getStudents();
Map<Student, Character> gradeMap = new HashMap<>();
int length = actualStudentCount();
for(int i = 0; i < length; i++){
gradeMap.put(studentlist[i], getDeviationScore(studentlist[i]));
}
return gradeMap;
}
}