Home > other >  Initializing Methods and Objects Correctly - Java
Initializing Methods and Objects Correctly - Java

Time:01-14

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!

CodePudding user response:

You forgot a return type for the method:

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

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

ArrayList<Integer> testScores = new ArrayList<Integer> scores;

CodePudding user response:

// setLetterGrades Method
// Specifies the return type ## public static ArrayList<String> ##
public static setLetterGrades(ArrayList<Integer> scores){ 
    // The grade variable is of type String, so we need a list of Strings.
    ArrayList<Integer> grades = new ArrayList<Integer>();

    for (int i = 0; i < scores.size(); i  ){
        // Define the score variable
        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.";
    }
    // Here, when calling the grade variable, it is not initialized.
    grades.add(grade);
    }
    return grades;
}

my answer

// setLetterGrades Method
public static ArrayList<String> setLetterGrades(ArrayList<Integer> scores){ 
    ArrayList<String> grades = new ArrayList<String>();
    String grade = "";
    for (int i = 0; i < scores.size(); i  ){
        int score = scores.get(i);
        if (score >= 90 && score < 101){
            grade = "A";
        }
        else if (score >= 80 && score < 90){
            grade = "B";
        }
        else if (score >= 70 && score < 80){
            grade = "C";
        }
        else if (score >= 60 && score < 70){
            grade = "D";
        }
        else if(score > 60 && score <= 0){
            grade = "F";
        }
        else {
            grade = "Invalid test score.";
        }
        grades.add(grade);
    }
    return grades;
}

and my opinion on the second

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