Skip to content

Completed Lab as per instructions #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
135 changes: 135 additions & 0 deletions src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,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;
}

}
78 changes: 78 additions & 0 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,82 @@
package io.zipcoder;

import java.util.ArrayList;
import java.util.Arrays;

public class Student {
private String firstName;
private String lastName;
private ArrayList<Double> testScores;

public Student(String firstName, String lastName, Double[] testScores){
this.firstName = firstName;
this.lastName = lastName;
this.testScores = new ArrayList<Double>(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 ArrayList<Double> getTestScores(){
return this.testScores;
}

public Integer getNumberOfExamsTaken(){
return this.getTestScores().size();
}

public void addExamScore(double examScore){
this.getTestScores().add(examScore);
}

public String getExamScores(){
String printOut = "";
ArrayList<Double> examScores = this.getTestScores();

for(int i = 0; i < examScores.size(); i++){
printOut += " Exam " + (i + 1) + " -> ";
printOut += examScores.get(i);
printOut += "\n";
}
return printOut;
}


public void setExamScore(int examNumber, double newScore){
ArrayList<Double> testScores = this.getTestScores();
testScores.set(examNumber - 1, newScore);
}

public double getAverageExamScore(){
double sum = this.getTestScores().stream().mapToDouble(Double::doubleValue).sum();
return sum / this.getTestScores().size();
}

@Override
public String toString(){
String readout = "";
readout += "Student Name: " + this.firstName + " " + this.lastName;
readout += "\n";
readout += "> Average Score: " + this.getAverageExamScore();
readout += "\n";
readout += "> Exam Scores:";
readout += "\n";
readout += this.getExamScores();

return readout;
}

}
69 changes: 69 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
package io.zipcoder;

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

import java.util.Arrays;
import java.util.Map;

public class ClassroomTest {

@Test
public void testAverageExamScores(){
// : Given
Double[] s1Scores = { 100.0, 150.0 };
Double[] s2Scores = { 225.0, 25.0 };

Student s1 = new Student("student", "one", s1Scores);
Student s2 = new Student("student", "two", s2Scores);

Student[] students = {s1,s2};
Classroom classroom = new Classroom(students);

// When
double output = classroom.getAverageExamScore();

// Then
System.out.println(output);
}

@Test
public void testAddStudent(){
// : Given
int maxNumberOfStudents = 1;
Classroom classroom = new Classroom(maxNumberOfStudents);
Double[] examScores = { 100.0, 150.0, 250.0, 0.0 };
Student student = new Student("Leon", "Hunter", examScores);

// When
Student[] preEnrollment = classroom.getStudents();
classroom.addStudent(student);
Student[] postEnrollment = classroom.getStudents();

// Then
String preEnrollmentAsString = Arrays.toString(preEnrollment);
String postEnrollmentAsString = Arrays.toString(postEnrollment);

System.out.println("===========================");
System.out.println(preEnrollmentAsString);
System.out.println("===========================");
System.out.println(postEnrollmentAsString);
}

@Test
public void testGetStudentsByExamScore(){
// : Given
Double[] s1Scores = { 100.0, 150.0, 123.0 };
Double[] s2Scores = { 100.0, 150.0, 123.0 };
Double[] s3Scores = { 145.3, 55.2, 400.1, 300.5};

Student s1 = new Student("student", "Yep", s1Scores);
Student s2 = new Student("student", "Two", s2Scores);
Student s3 = new Student("student", "three", s3Scores);

Student[] students = {s1,s2,s3};
Student[] expected = {s3, s1, s2};
Classroom classroom = new Classroom(students);

Student[] actual = classroom.getStudentsByScore();
Map<Student, Character> gradeBook = classroom.getGradeBook();
Assert.assertArrayEquals(actual, expected);

}
}
79 changes: 79 additions & 0 deletions src/test/java/io/zipcoder/StudentTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
package io.zipcoder;

import org.junit.Test;

public class StudentTest {
@Test
public void testStudentConstructor(){
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { 100.0, 95.0, 123.0, 96.0 };
Student student = new Student(firstName, lastName, examScores);

// When
String output = student.getExamScores();

// Then
System.out.println(output);
}


@Test
public void testAddExamScore() {
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = {};
Student student = new Student(firstName, lastName, examScores);

// When
student.addExamScore(100.0);
String output = student.getExamScores();

// Then
System.out.println(output);
}


@Test
public void testSetExamScore(){
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { 100.0 };
Student student = new Student(firstName, lastName, examScores);

// When
student.setExamScore(1, 150.0);
String output = student.getExamScores();

// Then
System.out.println(output);
}

@Test
public void testGetAverageScore(){
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { 100.0, 150.0, 250.0, 0.0 };
Student student = new Student(firstName, lastName, examScores);

// When
Double output = student.getAverageExamScore();

// Then
System.out.println(output);
}

@Test
public void testToString(){
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { 100.0, 150.0, 250.0, 0.0 };
Student student = new Student(firstName, lastName, examScores);

// When
String output = student.toString();

// Then
System.out.println(output);
}
}
Binary file added target/classes/io/zipcoder/Classroom.class
Binary file not shown.
Binary file added target/classes/io/zipcoder/Student.class
Binary file not shown.
Binary file not shown.
Binary file added target/test-classes/io/zipcoder/StudentTest.class
Binary file not shown.