Brandon Woodruff
Brandon Woodruff

Reputation: 21

How do I call my method from another method I have within my class in Java?

I'm constructing a class that calculates grades of students from a text file. The methods and fields used HAVE to be named exactly as they are written, as this is a school project, and my teacher will take points off if it isn't written exactly as she wants it. Regardless, based on her criteria, she wants me to create a method that calculates the average of an array, and then retrieve that average in another method (seems kind of redundant personally, but like I said, it's what she wants). I've constructed the method, and I reference it in my getAverage(int studentNum) method, but it is giving me an error of ".class missing" when I compile it. I'm not sure why to be honest. The solutions that eclipse offers are not what I want to be done, as they suggest I change the parameters of the method, or create an entirely new method, neither of which is what my teacher will want. Here's what I'm working with:

public class GradeBook
{
    private final int NUM_STUDENT = 5;
    private final int NUM_TESTS = 4;
    private String[] names = new String[5];
    private char[] grades = new char[5];
    private double[] scores1 = new double[4];
    private double[] scores2 = new double[4];
    private double[] scores3 = new double[4];
    private double[] scores4 = new double[4];
    private double[] scores5 = new double[4];

    public void setName(int studentNum, String name)
    {
        names[studentNum - 1] = name;
    }

    public void setScores(int studentNum, double[] scores)
    {
        if (studentNum == 1)
        {
            copyArray(scores1, scores);
        }

        else if (studentNum == 2)
        {
            copyArray(scores2, scores);
        }

        else if (studentNum == 3)
        {
            copyArray(scores3, scores);
        }

        else if (studentNum == 4)
        {
            copyArray(scores4, scores);
        }

        else
        {
            copyArray(scores5, scores);
        }
    }

    public String getName(int studentNum)
    {
        return names[studentNum - 1];
    }

    public double getAverage(int studentNum)
    {
        double average;

        if (studentNum == 1)
        {
            average = calcAverage(scores1[]);

            return average;
        }

        else if (studentNum == 2)
        {
            return calcAverage(scores2[]);
        }

        else if (studentNum == 3)
        {
            return calcAverage(scores3[]);
        }

        else if (studentNum == 4)
        {
            return calcAverage(scores4[]);
        }

        else
        {
            return calcAverage(scores5[]);
        }
    }

    public char getLetterGrade(int studentNum)
    {
        return grades[studentNum - 1];
    }

    public void copyArray(double[] to, double[] from)
    {
        for (int i = 0 ; i < from.length ; i++)
        {
            from[i] = to[i];
        }
    }

    public double calcAverage(double[] scores)
    {
        double sum = 0;
        double average;

        for (int i = 0 ; i < scores.length ; i++)
            {
                sum = sum + scores[i];
            }

        average = sum / scores.length;

        return average;
    }

    public void assignGrade(int studentNum)
    {
            grades[studentNum - 1] = determineGrade(getAverage(studentNum));
    }

    public char determineGrade(double average)
    {
        if (average <= 100 && average >= 90)
        {
            return 'A';
        }

        else if (average <= 89 && average >= 80)
        {
            return 'B';
        }

        else if (average <= 79 && average >= 70)
        {
            return 'C';
        }

        else if (average <= 69 && average >= 60)
        {
            return 'D';
        }

        else 
        {
            return 'F';
        }
    }
} 

I'm sure it's something incredibly easy I'm missing, and I just don't see it. What do you guys think?

Upvotes: 0

Views: 56

Answers (1)

nicomp
nicomp

Reputation: 4647

Drop the [] in your method argument:

 average = calcAverage(scores1[]);

should be

 average = calcAverage(scores1);

Upvotes: 3

Related Questions