Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,116 @@
package io.zipcoder;

import java.text.DecimalFormat;
import java.util.*;

public class Classroom {

Student[] students;
int maxNumberOfStudents;


public Classroom(int maxNumberOfStudents) {
this.maxNumberOfStudents = maxNumberOfStudents;
}

public Classroom(Student... students) {
this.students = students;
}

public Classroom() {
this.students = new Student[30];
}

public Student[] getStudents() { return students; }

public double getAverageExamScores(Student[] students) {
double total = 0.0;
for (Student s : students) {
total += s.getAverageTestScore();

}
return total / students.length;
}

public void addStudent(Student student) {
for (int i = 0; i < students.length; i++) {
if(students[i] == null) {
students[i] = student;
break;
}
}
}

public int actualStudentCount() { //counts how many actual students there are in a classroom size of 30
int totalStudents = 0;
for(int i = 0; i < students.length; i++) {
if(students[i] != null) {
totalStudents++;
}
}
return totalStudents;
}

public void removeStudent(String firstName, String lastName) {
ArrayList<Student> newStudentList = new ArrayList<Student> (Arrays.asList(students));

for (int i = 0; i < newStudentList.size(); i++) {
Student student = newStudentList.get(i);
if(student == null) {
continue;
} else if(student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)){
newStudentList.remove(student);
newStudentList.add(null);
}
}
this.students = newStudentList.toArray(new Student[0]);
}

public Student[] getStudentsByScore() {
List<Student> studentList = new ArrayList<Student> (Arrays.asList(students));

Comparator<Student> byExamScores = Comparator.comparing(Student::getAverageTestScore);
Comparator<Student> byFullName = Comparator.comparing(Student::getFullName);

Collections.sort(studentList, byExamScores.reversed().thenComparing(byFullName));

Student[] studentsSortedByScore = studentList.toArray(new Student[0]);

return studentsSortedByScore;
}

public Map<Student, Character> getGradeBook() {
Student[] studentList = this.getStudents();
Map<Student, Character> gradeBookResult = new HashMap<>();
int length = actualStudentCount();
for(int i = 0; i < length; i++) {
gradeBookResult.put(studentList[i], getDeviation(studentList[i]));
}
return gradeBookResult;
}

public char getDeviation(Student student) {
//need average score of each person in class
Double averageClassExamScore = this.getAverageExamScores(students);
Double averageStudentExamScore = student.getAverageTestScore();
Double preDeviation = 0.0;
for(Student testScoreAverage : students) {
preDeviation += Math.pow(averageStudentExamScore - averageClassExamScore, 2);
}
Double standardDeviation = Math.sqrt((preDeviation / actualStudentCount() - 1));
// 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){
return 'C';
} else if(averageStudentExamScore >= (averageClassExamScore - standardDeviation)){
return 'D';
} else {
return 'F';
}
}
}
75 changes: 75 additions & 0 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,79 @@
package io.zipcoder;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.jar.JarEntry;

public class Student {

private String firstName;
private String lastName;
ArrayList<Double> examScores;
Double[] testScores;

public Student(String firstName, String lastName, Double[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
this.testScores = testScores;
this.examScores = new ArrayList(Arrays.asList(testScores));
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getNumberOfExamsTake() {
return testScores.length;
}

public String getExamScores() {
StringBuilder testScoreString = new StringBuilder();
for (int i = 0; i < testScores.length; i++) {
testScoreString.append(testScores[i] + " | ");
}
return testScoreString.toString();
}

public void addExamScore(double testScores) {
this.examScores.add(testScores);
}

public void setExamScore(int examNumber, double newScore) {
this.examScores.set(examNumber, newScore);
}

public double getAverageTestScore() {
double sum = 0.0;
for (int i = 0; i < testScores.length; i++) {
sum += testScores[i];
}
DecimalFormat format = new DecimalFormat("#.#");
return sum / testScores.length;
}

@Override
public String toString() {
return String.format("Student Name: %s %s\n" + "Average Score: %.1f\n" + "Exam Scores: %s", firstName, lastName, getAverageTestScore(), getExamScores());
//Student Name: Jen
//Average Score: 80.0
//Exam Score 1: 90.0 | 80.0 | 70.0 |
}

public String getFullName() {
String fullName = getFirstName() + " " + getLastName();
return fullName;
}
}
114 changes: 114 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,118 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Test;

public class ClassroomTest {

@Test
public void testGetStudents() {
//given
Double[] testScores = {90.0, 80.0, 70.0};
Student student = new Student("Tohru", "Honda", testScores);
//when
Classroom classroom = new Classroom();
classroom.addStudent(student);
Student actual = classroom.getStudents()[0];
//then
Assert.assertEquals(student, actual);
}

@Test
public void testGetAverageExamScore() {
//given
Double[] examScores1 = {90.0, 95.0, 100.0}; //95
Double[] examScores2 = {85.0, 90.0, 87.0}; //87.3
Double[] examScores3 = {70.0, 65.0, 55.0}; //63.3
Student student1 = new Student("Blossom", "Ultonium",examScores1);
Student student2 = new Student("Buttercup", "Ultonium", examScores2);
Student student3 = new Student("Bubbles", "Ultonium", examScores3);
Student[] testStudentArr = {student1, student2, student3};
double expected = 81.888;
//when
Classroom classroom = new Classroom();
double actual = classroom.getAverageExamScores(testStudentArr);
//then
Assert.assertEquals(expected, actual, 0.001);
}

@Test
public void testAddStudents() {
//given
Double[] examScores1 = {90.0, 95.0, 100.0}; //95
Double[] examScores2 = {85.0, 90.0, 87.0}; //87.3
Double[] examScores3 = {70.0, 65.0, 55.0}; //63.3
Student student1 = new Student("Blossom", "Ultonium",examScores1);
Student student2 = new Student("Buttercup", "Ultonium", examScores2);
Student student3 = new Student("Bubbles", "Ultonium", examScores3);
Student[] expected = {student1, student2, student3, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null};
//when
Classroom testClassroom = new Classroom();
testClassroom.addStudent(student1);
testClassroom.addStudent(student2);
testClassroom.addStudent(student3);
Student[] actual = testClassroom.getStudents();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void testRemoveStudents() {
//given
Double[] examScores1 = {90.0, 95.0, 100.0};
Double[] examScores2 = {85.0, 90.0, 87.0};
Double[] examScores3 = {70.0, 65.0, 55.0};
Student student1 = new Student("Erwin", "Smith",examScores1);
Student student2 = new Student("Hanji", "Zoe", examScores2);
Student student3 = new Student("Eren", "Yeager", examScores3);
Student[] expected = {student2, student3, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null};
//when
Classroom testClassroom = new Classroom();
testClassroom.addStudent(student1);
testClassroom.addStudent(student2);
testClassroom.addStudent(student3);
testClassroom.removeStudent("Erwin", "Smith");
Student[] actual = testClassroom.getStudents();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void testGetStudentsByScore() {
//given
Double[] examScores1 = {85.0, 90.0, 87.0};
Double[] examScores2 = {90.0, 95.0, 100.0};
Double[] examScores3 = {85.0, 90.0, 87.0};
Student student1 = new Student("Craig", "Boone",examScores1);
Student student2 = new Student("Veronica", "Santangelo", examScores2);
Student student3 = new Student("Cass", "Sharon", examScores3);
Student[] expected = {student2, student3, student1};
//when
Classroom testClassroom = new Classroom(student1, student2, student3);
Student[] actual = testClassroom.getStudentsByScore();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void testGetGradeBook() {
//given
Double[] examScores1 = {85.0, 90.0, 87.0};
Double[] examScores2 = {90.0, 95.0, 100.0};
Double[] examScores3 = {85.0, 90.0, 87.0};
Double[] examScores4 = {65.0, 50.0, 57.0};
Double[] examScores5 = {75.0, 60.0, 67.0};
Double[] examScores6 = {85.0, 80.0, 77.0};
Student student1 = new Student("Craig", "Boone", examScores1);
Student student2 = new Student("Veronica", "Santangelo", examScores2);
Student student3 = new Student("Cass", "Sharon", examScores3);
Student student4 = new Student("Kyo", "Sohma", examScores4);
Student student5 = new Student("Tohru", "Honda", examScores5);
Student student6 = new Student("Hatsuharu", "Sohma", examScores6);
Student[] expected = {student1, student2, student3, student4, student5, student6};
//when
Classroom testClassroom = new Classroom(student1, student2, student3, student4, student5, student6);
//then
System.out.println(testClassroom.getGradeBook());
}
}
Loading