user3533863
user3533863

Reputation: 5

Seeking assistance for assignment to average an array of grades

I'll copy/paste the assignment, and then the progress I've made thus far. I'm truly not looking for someone to do the assignment, only some assistance to point me in the right direction as to where I'm going wrong? The program is running without error, however it's returning multiple results for a student when it should only be returning one. I can modify the grades and have it produce different results, so I know the math is being done, however I don't know why it's giving multiple (and different) results for a single student. I have been going over the code and cannot determine where I'm going wrong. My best guess is that it's somewhere in the OutputGrade method, I just can't find it.

Instructions:

Create a Java class called student with the following instance variables: • private String studentName; • private int [] grades; //an array of grades Include setter and getter methods to set and get these 2 properties. The setter method for the grades instance array variable — setGrades() — should take a single argument which is an array of int with the grades already filled in. setGrades() should be used if statement(s) to make sure that each grade value in the array parameter is valid (between 0 and 100) — you will need to use a loop in conjunction with the if-statement(s) to do this efficiently. If a grade is out of bounds, setGrades() should set that grade value to 0. The array that you pass to setGrades() must hold between 3 and 5 grades. Include a method called outputGrade() that averages up all the grades in the student grades array and then uses a switch statement to output grade conversions based on the following criteria: • For values from 0–59, your program should output: “Student has failed this class” • For values from 60–69, your program should output: “Student gets a D” • For values from 70–79, your program should output: “Student gets a C” • For values from 80–89, your program should output: “Student gets a B” • For values from 90–100, your program should output: “Student gets an A” Where “Student” is the actual name of the student. Create a test class called TestStudent that instantiates 3 students and sets their names and grades and then calls outputGrade() for each student. For your 3 students, 1 must have 3 grades, 1 must have 4 grades, and 1 must have 5 grades. This is so you will have 3 different array sizes to pass to setGrades(). Make sure that for 1 student, setGrades() is called with 1 grade value that is out of bounds (less than 0 or greater than 100).

Code:

public class TestStudent {

    public static void main(String[] args) {

        Student student1 = new Student();
        int[] grades1 = {15, 50, 5};
        student1.setStudentName("Ernest Craft");
        student1.setGrades(grades1);
        student1.outputGrades();

        Student student2 = new Student();
        int[] grades2 = {95, 95, 95, 95};
        student2.setStudentName("Steve Jones");
        student2.setGrades(grades2);
        student2.outputGrades();

        Student student3 = new Student();
        int[] grades3 = {105, -1, 72, 90, 88};
        student3.setStudentName("Mary Smith");
        student3.setGrades(grades3);
        student3.outputGrades();

    } // end method main
} // end class TestStudent

Student class:

public class Student {

    private String studentName;
    private int[] grades;

    //constructor
    public Student() {
    }

    public void setStudentName(String name) {
        studentName = name;
    } // end method setStudentName

    public String getStudentName() {
        return studentName;
    } // end method getStudentName

    public void setGrades(int gradeArray[]) {
        grades = gradeArray;

        for (int i = 0; i < gradeArray.length; i++) {
            if (gradeArray[i] < 0 || gradeArray[i] > 100) {
                gradeArray[i] = 0;
            } // end if
        } // end loop
    } // end method setGrades

    public int[] getGrades() {
        return grades;
    } // end method getGrades

    public void outputGrades() {

        int gradesTotal = 0;
        int gradesAverage;
        char letterGrade;

        for (int i : grades) {
            gradesTotal += i;
        } // end loop

        gradesAverage = gradesTotal / (grades.length);

        if (gradesAverage >= 0 && gradesAverage <= 59) {
            letterGrade = 'F';
        } else if (gradesAverage >= 60 && gradesAverage <= 69) {
            letterGrade = 'D';
        } else if (gradesAverage >= 70 && gradesAverage <= 79) {
            letterGrade = 'C';
        } else if (gradesAverage >= 80 && gradesAverage <= 89) {
            letterGrade = 'B';
        } else {
            letterGrade = 'A';
        } // end if statement

        switch (letterGrade) {

            case 'A':
                System.out.println(studentName + " gets an A.");
            case 'B':
                System.out.println(studentName + " gets an B.");
            case 'C':
                System.out.println(studentName + " gets an C.");
            case 'D':
                System.out.println(studentName + " gets an D.");
            case 'F':
                System.out.println(studentName + " gets an F.");

        } // end switch
    } // end method outputGrades
} // end class Student 

Thanks for taking a look!

Ben

Upvotes: 0

Views: 3101

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

You forgot the break statements from your switch. If you don't break, it'll just keep executing everything until the end of the block!

This somewhat confusing "feature" is a leftover from C, and in most cases you'll want to end all your cases with a break, return or throw.

Upvotes: 5

Related Questions