Skip to content

submitting #41

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

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

public class Classroom {
ArrayList<Student> students = new ArrayList<>();

//Constructors
public Classroom(int maxNumberOfStudents){
this.students = new ArrayList<>(maxNumberOfStudents);
}

public Classroom(Student[] classroom){
this.students.addAll(Arrays.asList(classroom));
}

public Classroom(){
this.students = new ArrayList<>(30);
}

//Get Methods
public ArrayList<Student> getStudents(){ return this.students; }

public String[] getStudentFirstNames(){
String[] names = new String[students.size()];
for(int i = 0; i < students.size(); i++){
names[i] = students.get(i).getFirstName();
}
return names;
}

public Double getAverageExamScore(){
Double sumScores = 0.0;
Double sumExams = 0.0;
for (int i = 0; i < students.size(); i++){
Student kid = students.get(i);
sumExams += kid.getNumberOfExamsTaken();
sumScores += (kid.getAverageExamScore() * kid.getNumberOfExamsTaken());
}
return sumScores/sumExams;
}

public void addStudent(Student guy){
students.add(guy);
}

public void removeStudent(String firstName, String lastName){
for (int i = 0; i < students.size(); i++){
if (students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)){
students.remove(i);
}
}
}

public Student[] getStudentsByScore(){
Student[] rankings = students.toArray(new Student[students.size()]);
Student placeholder;

for(int w = 1; w < students.size(); w++){
for(int i = 0; i < students.size() - w; i++){
if(rankings[i].getAverageExamScore() > rankings[i+1].getAverageExamScore()){
placeholder = rankings[i+1];
rankings[i+1] = rankings[i];
rankings[i] = placeholder;
}
}
}

return rankings;
}


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

import javax.xml.crypto.dom.DOMCryptoContext;
import java.util.ArrayList;
import java.util.Arrays;

public class Student {
String firstName;
String lastName;
ArrayList<Double> examScores = new ArrayList<>();

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

}

// Getters

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public ArrayList<Double> getExamScores() {
return examScores;
}

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

// Setters


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

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

// Other Methods

public String getExamsTaken() {
String ans = "Exam Scores:";
for (int i = 1; i <= examScores.size(); i++) {
ans += "\n Exam " + i + " -> " + Math.round(examScores.get(i - 1));
}
return ans;
}

public void takeExam(Double newScore) {
this.examScores.add(newScore);
}

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

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

return sum/examScores.size();
}

@Override
public String toString(){
return "Student Name: " + firstName + " " + lastName + "\n"
+"> Average Score: " + getAverageExamScore() + "\n"
+"> " + getExamsTaken();
}
}
167 changes: 167 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,171 @@
package io.zipcoder;

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

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

public class ClassroomTest {

@Test
public void testGetStudents(){
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[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

ArrayList<Student> expected = new ArrayList<>(Arrays.asList(cohort));
ArrayList<Student> actual = testClass.getStudents();

Assert.assertEquals(expected,actual);
}

@Test
public void testGetNotStudents(){
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[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

Student[] unexpected = {s1,s2};
ArrayList<Student> actual = testClass.getStudents();

Assert.assertNotEquals(unexpected,actual);
}

@Test
public void testGetAverageExamScore(){
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[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

Double expected = 125.0;
Double actual = testClass.getAverageExamScore();

Assert.assertEquals(expected, actual);
}

@Test
public void testGetNotAverageExamScore(){
Double[] s1Scores = { 100.0, 150.0 };
Double[] s2Scores = { 225.0, 26.0 };

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

Student[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

Double unexpected = 125.0;
Double actual = testClass.getAverageExamScore();

Assert.assertNotEquals(unexpected, actual);
}

@Test
public void addStudentPositiveTest(){
Classroom classroom = new Classroom(10);
Double[] examScores = { 100.0, 150.0, 250.0, 0.0 };
Student s1 = new Student("Leon", "Hunter", examScores);

Integer preEnrollmentsize = classroom.getStudents().size();
classroom.addStudent(s1);
ArrayList postEnrollment = classroom.getStudents();

Integer[] expected = {0,1};
Integer[] actual = {preEnrollmentsize, postEnrollment.size()};

Assert.assertEquals(expected,actual);

}

@Test
public void addStudentNegativeTest(){
Classroom classroom = new Classroom(10);
Double[] examScores = { 100.0, 150.0, 250.0, 0.0 };
Student s1 = new Student("Leon", "Hunter", examScores);

Integer preEnrollmentsize = classroom.getStudents().size();
classroom.addStudent(s1);
ArrayList postEnrollment = classroom.getStudents();

Integer[] expected = {0,0};
Integer[] actual = {preEnrollmentsize, postEnrollment.size()};

Assert.assertNotEquals(expected,actual);
}

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

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

Student[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

testClass.removeStudent("one","one");

Assert.assertEquals(1,testClass.students.size());

}

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

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

Student[] cohort = {s1,s2};
Classroom testClass = new Classroom(cohort);

testClass.removeStudent("one","one");

Assert.assertNotEquals(2,testClass.students.size());
}

@Test
public void getStudentsByScoreTest(){
Double[] scores1 = {1.0};
Double[] scores2 = {2.0};
Double[] scores3 = {3.0};
Double[] scores4 = {4.0};
Double[] scores5 = {5.0};
Double[] scores6 = {6.0};

Student s1 = new Student("1","1",scores1);
Student s2 = new Student("2","2",scores2);
Student s3 = new Student("3","3",scores3);
Student s4 = new Student("4","4",scores4);
Student s5 = new Student("5","5",scores5);
Student s6 = new Student("6","6",scores6);

Student[] cohort = {s4,s3,s2,s1,s6,s5};
Student[] expected = {s1,s2,s3,s4,s5,s6};
Classroom cohortSorted = new Classroom(cohort);
Student[] actual = cohortSorted.getStudentsByScore();

Assert.assertEquals(actual,expected);

}


}
Loading