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
81 changes: 81 additions & 0 deletions src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
package io.zipcoder;


import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class Classroom {
Student[] students;
int maxNumberOfStudents;

public Classroom(int maxNumberOfStudent){
maxNumberOfStudents = maxNumberOfStudent;
}
public Classroom(Student[] allStudents){
this.students = allStudents;
}
public Classroom(){
this.students = new Student[30];
}
public Student[] getStudents() {
return students;
}
public Double getAverageExamScore(){
Double examSum = 0.0;
for(int i = 0; i < students.length; i++){
examSum += students[i].getAverageExamScore();
}
return examSum/students.length;
}
public void addStudent(Student student){
students = new Student[maxNumberOfStudents];
for (int i = 0; i < this.students.length; i++){
if(this.students[i] == null){
students[i] = student;
break;
}
}
}
public void removeStudent(String firstName, String lastName) {
for (int i = 0; i < students.length; i++) {
if ((students[i].firstName + students[i].lastName).equals(firstName + lastName)) {
System.arraycopy(students, i + 1, students, i, students.length - 1 - i);
}
students[students.length - 1] = null;
break;
}
}
public Student[] getStudentsByScore () {
Student[] scoreArr = new Student[students.length];
for (int i=0 ; i <students.length-1; i ++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].getAverageExamScore().compareTo(students[j].getAverageExamScore()) < 0) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
return scoreArr;
}

public Map getGradeBook() {
Map<String, String> gradeBook = new TreeMap<String, String>();
for (int i = 0; i < students.length; i++) {
if (students[i].getAverageExamScore() >= 90) {
gradeBook.put(students[i].getFirstName(), "A");
} else if (students[i].getAverageExamScore() >= 80) {
gradeBook.put(students[i].getFirstName(), "B");
} else if (students[i].getAverageExamScore() >= 70) {
gradeBook.put(students[i].getFirstName(), "C");
} else if (students[i].getAverageExamScore() >= 60) {
gradeBook.put(students[i].getFirstName(), "D");
} else {
gradeBook.put(students[i].getFirstName(), "F");
}

}
for (Map.Entry g : gradeBook.entrySet()) {
System.out.println(g);
}
return gradeBook;
}
}
59 changes: 59 additions & 0 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
package io.zipcoder;

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

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

public Student(String firstName, String lastName, ArrayList<Double> examScores) {
this.firstName = firstName;
this.lastName = lastName;
this.examScores = examScores;
}

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 String getExamScores(){
String output = "Exam Scores:\n";
int counter = 1;
for(int i = 0; i <= examScores.size() - 1; i++){
output +=" \tExam " + counter + " -> " + examScores.get(i) + "\n";
counter++;
}
return output;
}

public int getNumberOfExamsTaken() {
return examScores.size();
}

public void addExamScore(Double score){ examScores.add(score); }

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

public Double getAverageExamScore(){
Double average = 0.0;
for(int i = 0; i < examScores.size(); i++){
average += examScores.get(i);
}
return average/examScores.size();
}

@Override
public String toString(){
return "Student: " + getFirstName() + getLastName() + ", Average Scores: " + getAverageExamScore() + getExamScores();
}
}
90 changes: 90 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,94 @@
package io.zipcoder;

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

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


public class ClassroomTest {
@Test
public void testGetAvergeExamScore() {
ArrayList<Double> s1Scores = new ArrayList<Double>(Arrays.asList(84.00, 92.00));
ArrayList<Double> s2Scores = new ArrayList<Double>(Arrays.asList(96.0, 100.00));

Student s1 = new Student("Kai", "Shields", s1Scores);
Student s2 = new Student("Kendra", "Ng", s2Scores);

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

Double actual = classroom.getAverageExamScore();

Assert.assertEquals(expectedAverage, actual);
}

@Test
public void testAddStudent() {
int maxNumberOfStudents = 1;
Classroom classroom = new Classroom(maxNumberOfStudents);
ArrayList<Double> examScores = new ArrayList<>(Arrays.asList(100.0, 85.0, 76.0, 0.0));
Student student = new Student("Kai", "Shields", examScores);

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

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

@Test
public void testRemoveStudent() {
Integer maxNumberOfStudents = 1;
Classroom classroom = new Classroom(maxNumberOfStudents);
ArrayList<Double> s2Scores = new ArrayList<>(Arrays.asList(89.0, 100.0, 50.0));
Student s1 = new Student("Kai", "Shields", s2Scores);
classroom.addStudent(s1);

Integer actual = classroom.getStudents().length;

classroom.removeStudent("Kai", "Shields");

Assert.assertEquals(maxNumberOfStudents, actual);
}

@Test
public void testGetStudentsByScore() {
ArrayList<Double> s1Scores = new ArrayList<Double>(Arrays.asList(100.0, 93.0));
ArrayList<Double> s2Scores = new ArrayList<Double>(Arrays.asList(78.0, 85.0));
Student s1 = new Student("Kendra", "Ng", s2Scores);
Student s2 = new Student("Kai", "Shields", s1Scores);
Student[] students = {s1, s2};
Classroom classroom = new Classroom(students);
classroom.addStudent(s1);
classroom.addStudent(s2);

Student[] preEnrollment = classroom.getStudents();
String preEnrollmentAsString = Arrays.toString(preEnrollment);
classroom.getStudentsByScore();
Student[] postEnrollment = classroom.getStudents();

String postEnrollmentAsString = Arrays.toString(postEnrollment);
}

@Test
public void testGetGradeBook() {
ArrayList<Double> s1Scores = new ArrayList<Double>(Arrays.asList(70.0, 100.0));
ArrayList<Double> s2Scores = new ArrayList<Double>(Arrays.asList(94.0, 85.0));
ArrayList<Double> s3Scores = new ArrayList<Double>(Arrays.asList(62.0, 96.0));

Student s1 = new Student("Kai", "Shields", s1Scores);
Student s2 = new Student("Kendra", "Ng", s2Scores);
Student s3 = new Student("Val", "Fraiger", s3Scores);

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

Map gradeBook = classroom.getGradeBook();
}

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

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReferenceArray;

public class StudentTest {
@Test
public void testGetFirstName(){
Student s = new Student("Bob", "Bobert", null);
String expected = "Bob";
String actual = s.getFirstName();
Assert.assertEquals(expected, actual);
}
@Test
public void testGetLastName(){
Student s = new Student("Bob", "Bobert", null);
String expected = "Bobert";
String actual = s.getLastName();
Assert.assertEquals(expected, actual);
}
@Test
public void testGetExamScores(){
String firstName = "Kai";
String lastName = "Shields";
ArrayList<Double> examScores = new ArrayList<Double>(Arrays.asList(80.0, 87.0, 100.0));
Student s = new Student(firstName, lastName, examScores);

String expected ="Exam Scores:\n" +
" \tExam 1 -> 80.0\n" +
" \tExam 2 -> 87.0\n" +
" \tExam 3 -> 100.0\n";
String actual = s.getExamScores();

Assert.assertEquals(expected, actual);
}

@Test
public void testAddExamScore(){
String firstName = "Kai";
String lastName = "Shields";
ArrayList<Double> examScores = new ArrayList<Double>(Arrays.asList(80.0, 87.0, 100.0));
Student student = new Student(firstName, lastName, examScores);
String expected ="Exam Scores:\n" +
" \tExam 1 -> 80.0\n" +
" \tExam 2 -> 87.0\n" +
" \tExam 3 -> 100.0\n" +
" \tExam 4 -> 90.0\n";

student.addExamScore(90.0);
String actual = student.getExamScores();

Assert.assertEquals(expected, actual);
}

@Test
public void testSetExamScore(){
String firstName = "Kai";
String lastName = "Shields";
ArrayList<Double> examScores = new ArrayList<Double>(Arrays.asList(80.0, 87.0, 100.0, 90.0));
Student student = new Student(firstName, lastName, examScores);
String expected ="Exam Scores:\n" +
" \tExam 1 -> 70.0\n" +
" \tExam 2 -> 87.0\n" +
" \tExam 3 -> 100.0\n" +
" \tExam 4 -> 90.0\n";

student.setExamScores(0, 70.0);
String actual = student.getExamScores();

Assert.assertEquals(expected, actual);
}

@Test
public void testAverageExamScore(){
String firstName = "Kai";
String lastName = "Shields";
ArrayList<Double> examScores = new ArrayList<Double>(Arrays.asList(80.0, 87.0, 100.0, 90.0));
Student student = new Student(firstName, lastName, examScores);
Double expected = 89.25;

Double actual = student.getAverageExamScore();

Assert.assertEquals(expected, actual);

}

@Test
public void testToString(){
String firstName = "Kai";
String lastName = "Shields";
ArrayList<Double> examScores = new ArrayList<Double>(Arrays.asList(80.0, 87.0, 100.0, 90.0));
Student student = new Student(firstName, lastName, examScores);
String expected ="Student: Kai Shields" +
", Average Scores: 89.25" +
"Exam Scores:\n" +
" \tExam 1 -> 80.0\n" +
" \tExam 2 -> 87.0\n" +
" \tExam 3 -> 100.0\n" +
" \tExam 4 -> 90.0\n";
String actual = student.toString();
Assert.assertEquals(expected, actual);
}

}
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.