Classroom Grades Analyzer


#1

https://www.codecademy.com/en/courses/learn-java/projects/grades-analyzer

what went wrong? :frowning:

GradeAnalyzer.java:26: error: cannot find symbol
int average = sum / grade;
^
symbol: variable grade
location: class GradeAnalyzer
GradeAnalyzer.java:45: 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
2 errors



import java.util.ArrayList;

class GradeAnalyzer {

  public GradeAnalyzer() {
    
  }
  
  public int getAverage() {
   
    ArrayList<Integer> grades = new ArrayList<Integer>();
    
    if (grades.size() < 1) {
      
      System.out.println("ERROR the arraylist is emty");
      return 0;
    }
    
    else {
      int sum = 0;
      for (Integer grade : grades) {
        sum = sum + grade;
      }
      
      
      int average = sum / grade;
      System.out.println(average);
      return average;
      
    }
  }
  
public static void main(String[] args) {
  
  ArrayList<Integer> myClassroom = new ArrayList<Integer>();
  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);
}
  
  
}



#2

With
myAnalyzer.getAverage(myClassroom);
you are calling the =getAverage= Method using 1 argument
being an an Array with number Values, which you named =myClassroom=
Now read the article and look how they do it
https://stackoverflow.com/questions/1610757/pass-array-to-method-java
So you will have to make some adjustment in your getAverage-Method definition !!

===================================
If you want to create the =average=
you will want to divide the =sum= by the total of elements/items of the array…
and NOT as you do by using numbers which is basically the array itself …
please read
https://stackoverflow.com/questions/9297899/where-is-arrays-length-property-defined


#3

Thank you for replay. I managed to get the program to post the correct answer :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.