Grade Analyzer: methods for the lowest or highest class grade


#1

Hi everyone,
I am trying to improve my project according to the point 19 where it states: Optional: Write methods that determine both the lowest or highest class grade.

As far I’ve written this and I have no idea what is my mistake:

public int lowestGrade(ArrayList<Integer> grades){
    for (Integer grade : grades) {
    	for (int i=0;i<=grades.size(); i++){
      if (grade(i)<lowestGrade){
        lowestGrade = grade(i);
      }
     return grade;
    	}
    }
  }

It is between getAverage method and main method. On the right site it says:

GradeAnalyzer.java:24: error: cannot find symbol
if (grade(i)<lowestGrade){
^
symbol: method grade(int)
location: class GradeAnalyzer
GradeAnalyzer.java:24: error: cannot find symbol
if (grade(i)<lowestGrade){
^
symbol: variable lowestGrade
location: class GradeAnalyzer
GradeAnalyzer.java:25: error: cannot find symbol
lowestGrade = grade(i);
^
symbol: variable lowestGrade
location: class GradeAnalyzer
GradeAnalyzer.java:25: error: cannot find symbol
lowestGrade = grade(i);
^
symbol: method grade(int)
location: class GradeAnalyzer
4 errors

Thanks for any tips :wink:


#2

please post your full code


#3

Ok, here it is:

import java.util.ArrayList;

public class GradeAnalyzer { 
 
  public GradeAnalyzer(){
  } 
  public int getAverage(ArrayList<Integer> grades){
    if (grades.size() <1){
      System.out.println("Sorry, there are no grades on the list.");
      return 0;
    } else { 
      int sum = 0;
    for (Integer grade : grades) { 
      sum = sum+grade;
    } 
      int average = sum/grades.size();
      return average;
        }
  } 

  public int lowestGrade(ArrayList<Integer> grades){
    for (Integer grade : grades) { 
        for (int i=0;i<=grades.size(); i++){
      if (grade(i)<lowestGrade){
        lowestGrade = grade(i);
      } 
     return grade;
    } 
    } 
  } 
  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();
    System.out.println(myAnalyzer.getAverage(myClassroom));
System.out.println(myAnalyzer.lowestGrade(myClassroom));
  } 
} 


#4

why do you have a second loop?

look:

public int lowestGrade(ArrayList<Integer> grades){
    for (Integer grade : grades) { 
        System.out.println(grade);
    } 
    return 1;
  } 

one loop is enough to get the elements from the array, so all you need to do is set the lowestGrade to first element of array before the loop, then inside the loop use an if condition to check that grade is lower then lowestGrade, then inside the if clause update lowestGrade


#5

Ok, thank you so much for your help. Everything works perfectly. The final result is below:

import java.util.ArrayList;

public class GradeAnalyzer {
  
  public GradeAnalyzer(){
  }
  public int getAverage(ArrayList<Integer> grades){
    if (grades.size() <1){
      System.out.println("Sorry, there are no grades on the list.");
      return 0;
    } else {
      int sum = 0;
    for (Integer grade : grades) {
      sum = sum+grade;
    }
      int average = sum/grades.size();
      return average;
 		}
  }

public int lowestGrade(ArrayList<Integer> grades){
    int lowestGrade = grades.get(0);
  	for (Integer grade : grades) { 
        if (grade<lowestGrade){
        lowestGrade=grade;
      }
    } 
    return lowestGrade;
  } 
  
 public int highestGrade(ArrayList<Integer> grades){
    int highestGrade = grades.get(0);
  	for (Integer grade : grades) { 
      	if (grade>highestGrade){
        highestGrade=grade;
      }
    } 
    return highestGrade;
  } 
  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();
    System.out.println("The average of all grades is: "+myAnalyzer.getAverage(myClassroom));
		System.out.println("The lowest grade is: "+myAnalyzer.lowestGrade(myClassroom));
    System.out.println("The highest grade is: "+myAnalyzer.highestGrade(myClassroom));
  }
}

#6

Did i gave too much hints in a single go?

You might want to read this topic:

How do I format code in my posts?

on how to display your code nicely on the forum


#7

No, I think that was absolutely fine. It took me a moment to think about an answer :slight_smile:


#8

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