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

//import com.google.common.collect;
import java.util.*;

public class Classroom {

//instance variables
private ArrayList<Student> students;
private Integer maxNumberOfStudents;
private static Map<Double, String> limits;



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

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

//Constructor with the students <list> representative of the collection of student objects this classroom will store.
public Classroom(ArrayList<Student> students) {
this.students = students;
}


//Getters
public ArrayList<Student> getStudents() {
return students;
}

public Integer getMaxNumOfStudents() {
return maxNumberOfStudents;
}

public Double getAverageExamScore() {
Double totalExamScores = 0.0;
int numOfStudents = students.size();

for (int i = 0; i < students.size(); i++) {
totalExamScores += students.get(i).getAverageExamScore();
}
return totalExamScores / numOfStudents;
}

public void addStudent (Student studentToAdd){
//StringBuilder sb = new StringBuilder();
students.add(studentToAdd);
// sb.append("\n========================================\n");
// sb.append(studentToAdd.printToString());
// sb.append("========================================");
//return sb.toString();
}

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 ArrayList <Student> getStudentsByScore () {
Collections.sort(students);
return students;
}
//Tried creating some maps and things to sort the student data. Ended up using a custom comparator to sor the student list

// for (int i = 0; i < students.size(); i++) {
// students.get(i).getAverageExamScore();
// students.sort(Comparator.naturalOrder());
// HashMap<Student, Double> studentDoubleHashMap = new HashMap<>();
// for (int i = 0; i < students.size(); i++) {
// studentDoubleHashMap.put(students.get(i), students.get(i).getAverageExamScore());
// }

public Map getGradeBook (){
//Integer numOfStudentsInClass = students.size();
String letterGrade = "I";
//orders students by score
getStudentsByScore();
//Limits for the tree map
// static {
// limits = new TreeMap<>();
// limits.put(0.0 , "F");
// limits.put(students.size()*0.11 , "D");
// limits.put(students.size()*0.31, "C");
// limits.put(students.size()*0.51, "B");
// limits.put(students.size()*0.71, "B");
// limits.put(students.size()* 0.9, "A");

TreeMap<Student, String> gradeMap = new TreeMap<>();

// gradeMap.put(90.0, "A");
// gradeMap.put(71.0, "B");
// gradeMap.put(51.0, "C");
// gradeMap.put(11.0, "D");
// gradeMap.put( 0.0, "F");

for (int i = 0; i < students.size(); i++ ){

if ( students.get(i).getAverageExamScore() >= 90){
letterGrade = " A";}
else if (students.get(i).getAverageExamScore() <= 90 && students.get(i).getAverageExamScore() >= 71){
letterGrade = " B";}
else if (students.get(i).getAverageExamScore() <= 70 && students.get(i).getAverageExamScore() >= 50){
letterGrade= " C";}
else if (students.get(i).getAverageExamScore() <= 90 && students.get(i).getAverageExamScore() >= 71){
letterGrade = " D";}
else {
letterGrade = " F";}
gradeMap.put(students.get(i), letterGrade);
}
return gradeMap;
}

}

//
//+ " " + letterGrade

// students.get(i).getFirstName() + " " + students.get(i).getLastName());
// RangeMap<Integer, String> gradeLetter = ImmutableRangeMap.builder()
// .put(Range.closed(90, 100), "A")
// .put(Range.closed(60, 89), "B")
// // ...
// .build();
//
// public String getGrade(String studentName) {
// int averageScore = getAverageExamGrade(studentName);
// return gradeLetter.get(averageScore);
// }




125 changes: 123 additions & 2 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,125 @@
package io.zipcoder;

public class Student {
}
//import com.sun.javafx.binding.StringFormatter;



import java.lang.instrument.ClassDefinition;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

public class Student implements Comparable <Student>{

//***Instance Fields***
private String firstName;
private String lastName;
private ArrayList<Double> examScores;

//constructor method

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

//ArrayList<Double> examScores = new ArrayList<>();
// Collections.addAll(examScores, testScores);
}

//Getters and setters

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

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


public String getFirstName() {
return this.firstName;
}

public String getLastName() {
return this.lastName;
}

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

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

//get exam scores method

public String getExamScores() {

StringBuilder sb = new StringBuilder();
sb.append("Exam Scores:\n");
for (int i = 0; i < examScores.size(); i++){
sb.append(String.format("\tExam %d -> %.1f\n" , i+1, examScores.get(i)));
}

return sb.toString();
}

public String addExamScore(Double examScore)
{
examScores.add(examScore);
//StringBuilder sb = new StringBuilder();
return getExamScores();
}

public String setExamScore(Integer examNumber, Double newScore){
examScores.set(examNumber-1, newScore);
return getExamScores();
}

public Double getAverageExamScore (){
Double sum = 0.0;
Double result = 0.0;
Double numOfScores = getNumOfExamsTaken()*1.0;

// double sum = 0;
// double numOfScores = examScores.size();
// double result = 0;

for (int i = 0; i < examScores.size(); i++) {
sum += examScores.get(i);
}
result = (sum / numOfScores);
DecimalFormat df = new DecimalFormat("#.#");

return Double.parseDouble(df.format(result));

}
public String printToString(){
StringBuilder sb= new StringBuilder();
sb.append(String.format("Student Name: %s %s\n", firstName, lastName));
sb.append(String.format("Average Score: %.1f\n", getAverageExamScore()));
sb.append(getExamScores());
return sb.toString();
}


@Override
public int compareTo(Student other) {
//Sort by score in reverse order (high to low)
if(this.getAverageExamScore().compareTo(other.getAverageExamScore()) != 0) {return -1 * this.getAverageExamScore().compareTo(other.getAverageExamScore());}
//if scores are the same, use last name from a to z as the next check
else if (lastName.compareTo(other.lastName) != 0) {return lastName.compareTo(other.lastName);}
//if the last names are the same, use first name from a to z
else {return firstName.compareTo(other.firstName);}
}



}
Loading