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

import java.util.*;

public class Classroom {
//declare the instance variables

Student[] students;
int maxNumberOfStudents;
ArrayList<Student> studentList;

//1st constructor
public Classroom(int max) {
this.maxNumberOfStudents = max;
students=new Student[max];
studentList=new ArrayList<>();
}


//2nd constructor
//here student is the object in Student[]
public Classroom(Student[] students) {
this.students = students;
}


//3rd constructor
public Classroom() {
studentList=new ArrayList<>();
students=new Student[30];


}

//define a method getStudent()
public Student[] getStudents() {
return students;
}

//get average exam score
public Double getAverageExamScore() {


double total = 0;
for (Student s : students) {
if (s != null)
total = total + s.averageExamScore();
}

Double average = total / students.length;
return average;
}

//define method add student

public void addStudent(Student student) {

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

@Override
public String toString() {
return "Classroom{" +
"students=" + Arrays.toString(students) +
", maxNumberOfStudents=" + maxNumberOfStudents +
'}';
}



public void removeStudent(String firstName ,String lastName) {
List<Student> studentsList = new ArrayList<>();
if(this.students !=null) {
studentsList = new ArrayList<>(Arrays.asList(this.students));
}
for(Student student : studentsList) {
if(student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
studentsList.remove(student);
break;
}
}
this.students = studentsList.toArray(new Student[]{});

}
public Student getStudentsByScore(Student[] students){
Arrays.sort(students, Collections.reverseOrder());


return null;
}
public Map<Student, String> getGradebook() {
Map<Student, String> gradebook = new HashMap<>();
String grade = "";
for (Student student : students) {
if (student.averageExamScore() >= 90)
grade = "A";
else if (student.averageExamScore() >= 80)
grade = "B";
else if (student.averageExamScore() >= 70)
grade = "C";
else if (student.averageExamScore() >= 60)
grade = "D";
else
grade = "F";
gradebook.put(student, grade);
}
return gradebook;
}





}



98 changes: 97 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,100 @@
package io.zipcoder;

public class Student {
import java.util.ArrayList;
import java.util.Arrays;

public class Student implements Comparable<Student>{
String firstName;
String lastName;
ArrayList<Double> examScore;



Student(String firstName , String lastName , Double[] testScores){
this.firstName=firstName;
this.lastName=lastName;
this.examScore= new ArrayList<Double>(Arrays.asList(testScores));

}
public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public ArrayList<Double> getExamScore() {
return examScore;
}

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

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


public Integer getNumberOfExamsTaken(){
if(this.examScore!= null){
return examScore.size();
}
else return 0;
}


public String getExamScores() {
StringBuilder output=new StringBuilder();
output.append("Exam Scores:\n");
for(int i=0;i<examScore.size();i++){
output.append(String.format("\tExam %s -> %s\n",i+1,Math.round(examScore.get(i))));
}
return output.toString();
}

/*
public String getExamScores(){
String output="Exam Scores:\n";
for(int i=0;i<examScore.size();i++){
output=output+String.format("Exam %s -> %s\n",i+1,Math.round(examScore.get(i)));
}
System.out.println(output);
return output;
}
*/

public void addExamScore(Double examScore){
this.examScore.add(examScore);

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


public Integer averageExamScore(){
Double sum=0.0;
for(int i=0;i<this.examScore.size();i++){
sum=(sum+this.examScore.get(i));

}
int avg=(int)Math.rint(sum/this.examScore.size());
return avg;
}
@Override
public String toString(){
return "Student Name: "+firstName+" "+lastName+"\n> Average Score: "+averageExamScore()+"\n> "+getExamScores();


}


ArrayList<Student>studentArrayList=new ArrayList<>();

@Override
public int compareTo(Student o) {
return 0;
}
}
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.Arrays;
import java.util.Map;

public class ClassroomTest {
@Test
public void ClassroomTest() {
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 = new Student[]{s1, s2};
Classroom classroom = new Classroom(students);
Student[] actual = classroom.getStudents();
Assert.assertEquals(students, actual);
}

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

Student s1 = new Student("Rose", "Petal", s1Scores);
Student s2 = new Student("Subhash", "Bose", s2Scores);
Student[] students = new Student[]{s1, s2};
Classroom classroom = new Classroom(students);
Double expected = 125.0;
Double actual = classroom.getAverageExamScore();


Assert.assertEquals(expected, actual);

}

@Test
public void test2getAverageExamScore() {
Double[] s1Scores = {100.0, 95.0};
Double[] s2Scores = {100.0, 96.0};
Student s1 = new Student("Student", "one", s1Scores);
Student s2 = new Student("Student", "two", s2Scores);
Student[] students = new Student[]{s1, s2};
Classroom classroom = new Classroom(students);
Double actual = classroom.getAverageExamScore();
System.out.println(actual);
Assert.assertNotEquals(100, classroom.getAverageExamScore(), 0.00009);

}

@Test
public void addStudentTest() {
// : 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();
System.out.println(classroom.toString());
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 removeStudentTest() {
Double[] s1Scores = {100.0, 150.0};
Double[] s2Scores = {225.0, 25.0};
Double[] s3Scores = {98.0, 112.0};
Double[] s4Scores = {220.0, 45.0};
Student s1 = new Student("Student", "one", s1Scores);
Student s2 = new Student("Rose", "Petal", s2Scores);
Student s3 = new Student("Student", "three", s3Scores);
Student s4 = new Student("Student", "four", s4Scores);

String firstName = "Rose";
String lastName = "Petal";
Classroom classroom = new Classroom();
classroom.addStudent(s1);
classroom.addStudent(s2);
classroom.addStudent(s3);
classroom.addStudent(s4);
Student[] students = new Student[]{s1, s2, s3, s4};


classroom.removeStudent("Rose", "Petal");
Student[] expected = classroom.getStudents();
Assert.assertEquals(s1, expected[0]);
Assert.assertEquals(s3, expected[1]);
Assert.assertEquals(s4, expected[2]);
Assert.assertEquals(expected[3], null);
// System.out.println(expected[3]);
//Assert.assertEquals(expected[3]);


}

/*
@Test
public void getStudentByScoreTest(){
Double[] s1Scores = { 100.0, 150.0 };
Double[] s2Scores = { 225.0, 25.0 };
Double[] s3Scores = { 98.0, 112.0 };
Double[] s4Scores = { 220.0, 45.0 };
Student s1=new Student("Student", "one", s1Scores);
Student s2=new Student("Student", "two", s2Scores);
Student s3=new Student("Student", "tree", s1Scores);
Student s4=new Student("Student", "four", s2Scores);
Student[] students=new Student[]{s1,s2,s3,s4};
Classroom classroom =new Classroom(students);
Student[] expected = {s4, s2,s1,s3};
Student[] actual=classroom.getStudentsByScore( Student[] Students);
Assert.assertArrayEquals(expected, actual);

}*/
@Test
public void getStudentsByScoreTest() {
Double[] scores1 = new Double[]{45.0, 50.0, 55.0};
Double[] scores2 = new Double[]{60.0, 65.0};
Double[] scores3 = new Double[]{70.0, 70.0};
Student student1 = new Student("Jimmy", "John", scores1);
Student student2 = new Student("Tammy", "Tohn", scores2);
Student student3 = new Student("Jack", "Tohn", scores3);
Student[] newStudents = {student1, student2, student3};
Classroom classroom = new Classroom(newStudents);

}
@Test
public void getGradebookTest() {
/* int n = 20;
Student[] students = new Student[n];
for (int i = 1; i<=n; i++) {
students[i-1] = new Student("","",new Double[] {100.0,5.0*i});
}
Classroom classroom = new Classroom(students);*/
Double[] scores1 = new Double[] {45.0, 50.0, 55.0};
Double[] scores2 = new Double[] {60.0, 65.0};
Double[] scores3 = new Double[] {70.0, 70.0};
Double[] scores4 = new Double[] {95.0, 90.0, 95.0};
Double[] scores5 = new Double[] {80.0, 85.0};
Student student1 = new Student("Jimmy", "John", scores1 );
Student student2 = new Student("Tammy", "Tohn", scores2 );
Student student3 = new Student("Jack", "Tohn", scores3 );
Student student4 = new Student("Rhett", "Ale", scores4 );
Student student5 = new Student("Amy", "Roberts", scores5 );
Student[] newStudents = {student1, student2, student3, student4, student5};
Classroom classroom = new Classroom(newStudents);

Map<Student,String> gradebook = classroom.getGradebook();

Assert.assertEquals(gradebook.get(student1),"F");
Assert.assertEquals(gradebook.get(student2),"D");
Assert.assertEquals(gradebook.get(student3),"C");
Assert.assertEquals(gradebook.get(student4),"A");
Assert.assertEquals(gradebook.get(student5),"B");

}
}
Loading