Search code examples
javaobjectmethods

Initializing Methods and Objects Correctly - Java


I haven't used java for a while and for some reason I can't figure out how to initialize my methods and objects without errors. These are the two I'm having troubles with:

// setLetterGrades Method
public static setLetterGrades(ArrayList<Integer> scores){ 
    ArrayList<Integer> grades = new ArrayList<Integer>();

    for (int i = 0; i < scores.size(); i++){
        if (score >= 90 && score < 101){
            String grade = "A";
        }
        else if (score >= 80 && score < 90){
            String grade = "B";
        }
        else if (score >= 70 && score < 80){
            String grade = "C";
        }
        else if (score >= 60 && score < 70){
            String grade = "D";
        }
        else if(score > 60 && score <= 0){
             String grade = "F";
        }
        else {
            String grade = "Invalid test score.";
        }
        grades.add(grade);
    }
    return grades;
}

I'm getting 'Syntax error, insert "EnumBody" to complete BlockStatement' on the first line: public static setLetterGrades(ArrayList<Integer> scores){

Similarly, I'm getting 'Syntax error on token "public", new expected' on the first line of this object:

// GradeBook Object
public GradeBook(ArrayList<Integer> scores, testName){
    String test = testName;
    ArrayList<Integer> testScores = ArrayList<Integer> scores;
    setLetterGrades();
}

How do I make this second one more "object-y" and correct the error? Thank you for the help!


Solution

  • You forgot a return type for the method:

    public static ArrayList<String> setLetterGrades(ArrayList<Integer> scores){
    

    And you forgot the new keyword for the ArrayList constructor. This ain't Kotlin!

    ArrayList<Integer> testScores = new ArrayList<>();
    

    But I think what you were trying to do is this:

    ArrayList<String> testScores = setLetterGrades(scores);