Skip to content

finished lab #49

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

import java.util.*;
import java.util.stream.Collectors;

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

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

public Classroom(Student... students) {

this.students = students;
}

public Classroom() {

this.students = new Student[30];
}

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

public Double getAverageExamScore() {
Double sum =0.0;
for(Student student : students){
sum += student.getAverageExamScore();
}
return sum / students.length;
}

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

public void removeStudent(String firstName, String lastName) {
// Student[] studentArray = new Student[students.length];
// for(int i = 0; i < students.length; i++) {
// if(students[i].getFirstName().equals(firstName) && students[i].getLastName().equals(lastName));
// this.students[i] = null;
ArrayList<Student> list = new ArrayList<>(Arrays.asList(students));
for(int i =0; i < list.size(); i++){
Student student = list.get(i);
if(student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
list.remove(student);
list.add(null);
}

}
this.students = list.toArray(new Student[0]);
}



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



}
71 changes: 70 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
package io.zipcoder;

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

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

public Student(String firstName, String lastName, Double ... examScores) {
this.firstName = firstName;
this.lastName = lastName;
this.examScores = new ArrayList<Double>(Arrays.asList(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 Integer getNumberOfExamsTaken() {
return examScores.size();
}

public String getExamScores() {
String result = "Exam Scores: \n";

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

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

public void setExamScore(Integer examNumber, Double newScore) {
examScores.set(examNumber, newScore);
}

public Double getAverageExamScore() {
Double result = 0.0;
for (int i = 0; i < examScores.size(); i++) {
result = result + examScores.get(i);
}
return result / examScores.size();
}
@Override
public String toString() {
String result = "Student: " + firstName + " " + lastName + "\n";
result = result + "> Average Score: " + this.getAverageExamScore() + "\n";
result = result + "> " + getExamScores();


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

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

import java.util.Arrays;


public class ClassroomTest {

@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("John", "Song", 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 getAverageExamScoreTest() {
// : 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 expected = 125.0;
Double actual = classroom.getAverageExamScore();

// Then
Assert.assertEquals(expected,actual);

}
@Test
public void removeStudentTest() {
// : Given
Double[] examScores = {100.0, 150.0, 250.0, 0.0};
Double[] examScores1 = {90.0, 50.0, 70.0, 100.0};
Double[] examScores2 = {50.0, 60.0, 30.0, 40.0};
Student student = new Student("John", "Song", examScores);
Student student1 = new Student ("Billy", "Bob", examScores1);
Student student2 = new Student ("Water", "Bottle", examScores2);
Student[] students = {student, student1, student2};


// When
Classroom classRoom = new Classroom(students);
classRoom.removeStudent("Water", "Bottle");

// Then
Student actual = students[2];
Assert.assertNull(actual);
}

@Test
public void getStudentsByScoreTest() {
// : Given
Classroom classroom = new Classroom();
Double[] examScores = {100.0, 60.0, 80.0, 70.0};
Double[] examScores1 = {90.0, 50.0, 70.0, 90.0};
Double[] examScores2 = {70.0, 80.0, 50.0, 30.0};

// : When
Student student = new Student("John", "Song", examScores);
Student student1 = new Student("Leon", "Hunter", examScores1);
Student student2 = new Student("Billy", "Bob", examScores2);


Student[] expected = {student, student1, student2};
Student[] actual = classroom.getStudentsByScore();


// : Then
Assert.assertArrayEquals(expected, actual);

}

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

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

import java.util.ArrayList;

public class StudentTest {

@Test
public void studentConstructorTest() {
// : 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 expectedFirstNaME = student.getFirstName();
String expectedLastName = student.getLastName();




// Then
Assert.assertEquals(expectedFirstNaME, firstName);
Assert.assertEquals(expectedLastName, lastName);



}

@Test
public void studentSetConstructorTest() {
// : 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
student.setFirstName("John");
student.setLastName("Song");
String expectedFirstNaME = "John";
String expectedLastName = "Song";
String actual = student.getFirstName();
String actual2 = student.getLastName();

// Then
Assert.assertEquals(expectedFirstNaME, actual);
Assert.assertEquals(expectedLastName, actual2);
}

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

// When
String expectedExamScore = "Exam Scores: \n" + "\tExam 1 -> 100.0\n" + "\tExam 2 -> 95.0\n" +
"\tExam 3 -> 123.0\n";

String actual = student.getExamScores();

// Then
Assert.assertEquals(expectedExamScore, actual);


}

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

// When
student.setExamScore(1, 95.0);
String expectedExamScore = "Exam Scores: \n" + "\tExam 1 -> 100.0\n" + "\tExam 2 -> 95.0\n" +
"\tExam 3 -> 123.0\n";

String actual = student.getExamScores();

// Then
Assert.assertEquals(expectedExamScore, actual);
}

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

// When
String expected = "Student: Leon Hunter\n" +
"> Average Score: 90.0\n" + "> Exam Scores: \n" + "\tExam 1 -> 90.0\n" + "\tExam 2 -> 90.0\n" + "\tExam 3 -> 90.0\n";
String actual = student.toString();



// Then
Assert.assertEquals(expected, actual);
}

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

// When
Integer expected = 4;
Integer actual = student.getNumberOfExamsTaken();


// Then
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 not shown.