1.11M Members

Help making a tester class

 
0
 

Edit: Oh, nvm. I just redid the code and figured out how to write the tester class. Sorry guys for the trouble:( This thread can be deleted.

So I have this code (not all was written by me).
But I don't know how to start writing a tester class for it.
It asks: first, ask the user for all student names. Then read in the scores for all quizzes, prompting for the score of each student. Finally, print the names of all students and their final scores.

Here's the GradeBook.java:

import java.util.*;
import java.lang.*;
/**
    this class makes a gradebook you can modify the gradebook however you like and you can ask for individual's grade or a class as a collective.
*/
public class GradeBook
{
    //instance variables
    private ArrayList<ArrayList> gradeBook;
    private ArrayList<String> students;
    /**
        this method creates a new object that holds student grades
    */
    public GradeBook()
    {
        gradeBook = new ArrayList<ArrayList>();
        students = new ArrayList<String>();
    }
    /**
        This adds a new score to a student's name or array
        @param name is the students name
        @param score is the score that you add to the student's name
    */
    public void addScore(String name, double score)
    {
        if(!students.contains(name))
        {
            ArrayList<Double> currentScore = new ArrayList<Double>();
            students.add(name);
            currentScore.add(score);
            gradeBook.add(currentScore);
        }
        else
        {
            int indexOfStudent = 0;
            indexOfStudent = students.indexOf(name);
            ArrayList<Double> currentScores = new ArrayList<Double>();
            currentScores = gradeBook.get(indexOfStudent);
            currentScores.add(score);
            gradeBook.set(indexOfStudent, currentScores);
        }
    }
    /**
        finds the sum of an individual's scores
        @param name name of the student
    */
    public double individualSum(String name)
    {
        int indexOfStudent = 0;
        indexOfStudent = students.indexOf(name);
        double sum = 0.0;
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(indexOfStudent);
        for(int i = 0; i < studentScores.size(); i++)
        {
            sum += studentScores.get(i);
        }
        return sum;
    }
    /**
        Using a neutral indexing of the sum of all student
        @param j is the index of all the students
    */
    public double sum(int j)
    {
        double sum = 0.0;
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(j);
        for(int i = 0; i < studentScores.size(); i++)
        {
            sum += studentScores.get(j);
        }
        return sum;
    }
    /**
        finding the minimum of the student's scores
        @param name the students name of which is being refered to
        
    */
    public double individualMinimum(String name)
    {
        int indexOfStudent = 0;
        indexOfStudent = students.indexOf(name);
        double minNum = 0.0;
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(indexOfStudent);
        for(int i = 0; i < studentScores.size() - 1; i++)
        {
            minNum = Math.min(studentScores.get(i), studentScores.get(i + 1));
        }
        return minNum;
    }
    public double minimum(int j)
    {
        double minNum = 0.0;
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(j);
        for(int i = 0; i < studentScores.size() - 1; i++)
        {
            minNum = Math.min(studentScores.get(i), studentScores.get(i + 1));
        }
        return minNum;
    }
    public double finalScore(String name)
    {
        int indexOfStudent = 0;
        indexOfStudent = students.indexOf(name);
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(indexOfStudent);
        if(studentScores.size() == 0)
            return 0;
        else if (studentScores.size() == 1)
            return studentScores.get(0);
        else
            return individualSum(name) - individualMinimum(name);
    }
    public double allfinalScores(int i)
    {
        ArrayList<Double> studentScores = new ArrayList<Double>();
        studentScores = gradeBook.get(i);
        if(studentScores.size() == 0)
            return 0;
        else if (studentScores.size() == 1)
            return studentScores.get(0);
        else
            return sum(i) - minimum(i);
    }
    public String toString()
    {
        String allOfGradeBook = "";
        for(int i = 0; i < students.size(); i++)
        {
            allOfGradeBook += students.get(i) + " " + allfinalScores(i);
            if(i + 1 != students.size())
                allOfGradeBook += "\n";
        }
        return allOfGradeBook;
    }
}
Question Self-Answered as of 3 Years Ago
 
0
 

One small advice. You do this in a lot of places in your program:

ArrayList<Double> studentScores = new ArrayList<Double>();
studentScores = gradeBook.get(indexOfStudent);

It is not necessary. What you actually do is create a new ArrayList<Double>. A new instance of ArrayList<Double> in memory. And then you never use it. You simply replace the reference studentScores with the one inside the gradeBook. The first ArrayList<Double> created goes unused.
It is simpler to do this:

ArrayList<Double> studentScores = gradeBook.get(indexOfStudent);

Of course if you want to add a new student you correctly create a new ArrayList<Double>, but when you have found the indexOfStudent then can take it from the gradeBook. The addScore method is ok.

But for the other methods it would be safer to add this:

public double individualSum(String name)
    {
        int indexOfStudent = 0;
        indexOfStudent = students.indexOf(name);

        if (indexOfStudent==-1) {
          System.out.println("Student: "+name+" not found");
          return -1;
        }
  
        double sum = 0.0;
        ArrayList<Double> studentScores = gradeBook.get(indexOfStudent);

        for(int i = 0; i < studentScores.size(); i++)
        {
            sum += studentScores.get(i);
        }

        return sum;
    }
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article