Can you guys solve this for me : JAVA Classroom Grades Analyser

import java.util.ArrayList;
class GRadeAnalyser{
  public GradeAnalyser(){
    }

public int getAverage(grades){
  ArrayList<Integer> grades=new ArrayList<Integer>();
  if(grades.size<1){
    System.out.println("Error! The ArrayList is not empty.");
    return 0;
  }else{
    int sum=0;
  }
    for(Integer grade:grades){
      sum=sum+grade;
    }
  int average= sum/grades.size;
  System.out.println(grades.get(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);
    GradeAnalyser myAnalysor=new GradeAnalyser();
    myAnalyser.getAverage(myClassroom);
    System.out.println(myClassroom.get(0));
    System.out.println(myClassroom.get(6));
    
  }
}


and here is the error I got…

GradeAnalyzer.java:3: error: invalid method declaration; return type required
public GradeAnalyser(){
^
GradeAnalyzer.java:6: error: expected
public int getAverage(grades){
^
2 errors

Hello? Anyone pls…

your constructor (which is also empty) doesn’t have the same name as the class, this will throw an error.

its a forum, getting a reply might take time.

Can u show me how should I correct it pls… Thanks

correct the typo of your GradeAnalyser class

But… still can’t debug

you can’t change an R into r because you made a typo in a class name?

I changed it , but it still doesn’t work

can i then see an updated version of your code? Maybe the new error gives you new clues on how to solve it?

import java.util.ArrayList;
class GradeAnalyser{
public GradeAnalyser(){
}

public int getAverage(grades){
ArrayList grades=new ArrayList();
if(grades.size<1){
System.out.println(“Error! The ArrayList is not empty.”);
return 0;
}else{
int sum=0;
}
for(Integer grade:grades){
sum=sum+grade;
}
int average= sum/grades.size;
System.out.println(grades.get(average));
return 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);
GradeAnalyser myAnalysor=new GradeAnalyser();
myAnalyser.getAverage(myClassroom);
System.out.println(myClassroom.get(0));
System.out.println(myClassroom.get(6));

}
}

The error:
GradeAnalyzer.java:6: error: expected
public int getAverage(grades){
^
1 error

your constructor is empty, maybe it should have content?

still cannot eh… I don’t know

I am having the same error in the next project too: personal library…

import java.util.HashMap;
class Library{
public Library(){
}

public void getFinishedBooks(library){
HashMap<String, Boolean> library=new HashMap<String, Boolean>();
if(library.size<1){
System.out.println(“The HashMap is empty.”);
}else{

  for(String book : getFinishedBooks.keyset()){
    
    if(library.get(book)==true){
      System.out.println(book);
    }
  }
}

}

public static void main(String[]args){
HashMap<String,Boolean> myBooks=new HashMap<String,Boolean>();
myBooks.put(“Road Down The Funnel”, true);
myBooks.put(“Rat: A Biology”, false);
myBooks.put(“TimeIn”,true);
myBooks.put(“3D Food Printing”, false);
Library myLibrary=new Library();
myLibrary.getFinishedBooks(myBooks);
incompleteBooks=books-getFinishedBooks;
System.out.println(incompleteBooks);
}
}

The error is still Java 6:
Library.java:6: error: expected
public void getFinishedBooks(library){
^
1 error

parameters have types

Can u state in details with the answer… Thanks

your parameter lacks a type.

You may want to write a far more basic method, perhaps one that adds two numbers together.

class Main {
    // create a static method called add that returns the sum of two Integers

    public static void main(String[] args) {
        System.out.println(add(3, 4));  // this should print 7
    }
}
1 Like

Can u kindly send me the full answer for this code…

if you want the code, you can use the solution button (which appears after several failed attempts). The forum is for explanation.

If you write something far more basic which will be much easier to debug and google examples for

Then, when you have that working, either you’ll have found what you did differently, or you can slowly turn this into the one you’re debugging and see when it stops compiling