Classroom Grade Analyzer


#1

Hi, guys i wrote cod for exercise Classroom Grade Analyzer but i have some problems with sum variables.
Need help with understanding errors.

Here is my code
import java.util.ArrayList;

class GradeAnalyzer
{

public GradeAnalyzer () 
{

}

public int getAverage()

{
ArrayList < Integer > grades;

if ( grades.size() < 1) 
{
	System.out.println("Error");
	return 0;
	}
	else
{
 	int sum = 0;
}

for (Integer grade : grades) 
{
 sum = sum + grade;

}
int average = sum / grades.size();

System.out.println(average);

}

public static void main(String [ ] args)
{
ArrayList myClassroom = new ArrayList();
myClassroom.add(98);
myClassroom.add(92);
myClassroom.add(88);
myClassroom.add(75);
myClassroom.add(61);
myClassroom.add(89);
myClassroom.add(95);

GradeAnalyzer myAnalyzer = new GradeAnalyzer();
myAnalyzer.getAverage(myClassroom);

}
}

Program shows me this errors:

GradeAnalyzer.java:28: error: cannot find symbol
sum = sum + grade;
^
symbol: variable sum
location: class GradeAnalyzer
GradeAnalyzer.java:28: error: cannot find symbol
sum = sum + grade;
^
symbol: variable sum
location: class GradeAnalyzer
GradeAnalyzer.java:31: error: cannot find symbol
int average = sum / grades.size();
^
symbol: variable sum
location: class GradeAnalyzer
GradeAnalyzer.java:48: error: method getAverage in class GradeAnalyzer cannot be applied to given types;
myAnalyzer.getAverage(myClassroom);
^
required: no arguments
found: ArrayList
reason: actual and formal argument lists differ in length
4 errors


#2

This sum variable

is captured within this if block

Meaning it only exists in side that block, anything outside that block cannot see it. Perhaps you should change the scope of that variable.


Now according to the signature of your getAverage() method, it does not take in any arguments and it returns an int

but it has no return value

plus

grades has not been initialized you cant loop through it

and when you call it here

you are passing an argument. Perhaps you should re-evaluate that line

NB: start from beginning an re-evaluate your code logically and syntactically because you have a lot of errors