0

thanks for looking. I'm stuck on why this is not handling cases for inputted grades. It handles A+ fine (returns 4.0) but 'A' gives an error, 'A-' gives 4.0 (should be 3.7), then 'B+' is correct, 'B' gives error, 'B-' gives 3.0 (should be 2.7), and this is the pattern. Any thoughts?

public class Grade {
    private String grade = "";
    public Grade(String grade) {
        this.grade = grade;
    public double getgrade() {
        double gpa = 0.0;
        char[] gradeArray = this.grade.toCharArray();
        if (gradeArray[0] == 'A') {
            gpa += 4.0;
        }
        if (gradeArray[0] == 'B') {
            gpa += 3.0;
        }
        if (gradeArray[0] == 'C') {
            gpa += 2.0;
        }
        if (gradeArray[0] == 'D') {
            gpa += 1.0;
        }        
        if (gradeArray[1] == '+') {
            if (gradeArray[0] != 'A') {
                gpa += 0.3;
            }
        }
        if (gradeArray[0] == '-') {
            gpa -= 0.3;
        }
        return gpa;
    }
}```
4
  • 1
    if (gradeArray[0] == '-') { explains the other issue. Commented Sep 30, 2020 at 19:44
  • 1
    Please format your code correctly, or correct it if it is exactly the same as you posted. Your methods don't end properly in the code you posted, as they lack the closing curly braces. Commented Sep 30, 2020 at 19:45
  • thanks for your help, I will do this Commented Sep 30, 2020 at 19:48
  • What if they pass something other than A-D, + or -? I'd do some validation in the constructor and throw an exception if necessary... Commented Sep 30, 2020 at 20:18

2 Answers 2

1

well if you call A or B, e.g.:

new Grade("A").getNumericGrade()

you get java.lang.IndexOutOfBoundsException on line

if (gradeArray[1] == '+') {

because gradeArray[1] does not exist, there is only 1 character (at index 0) it is 'A' or 'B'

Simple solution: check for length! E.g.:

if (gradeArray.length > 1 && gradeArray[1] == '+') {
Sign up to request clarification or add additional context in comments.

Comments

0

public class Grade {

private String grade = "";

public Grade(String grade) {
    this.grade = grade;
}

public String getLetterGrade() {
    return this.grade;
}

public double getNumericGrade() {
    double gpa = 0.0;
    char[] gradeArray = this.grade.toCharArray();

    // Good to have null/empty check always
    if ( gradeArray.length == 0  || gradeArray[0] == ' '){
        return 0;
    }

    // Code readability increases manifolds with switch. if clause can also be used
    // Considering only the cases you originally had. With this your code block at last becomes redundant
    switch ( gradeArray[0] ){
        case 'A': gpa += 4.0; break;
        case 'B': gpa += 3.0; break;
        case 'C': gpa += 2.0; break;
        case 'D': gpa += 1.0; break;
        default: break;
    }

    // First you need to check if there is a second character present or not
    // If present then it should be only +/-
    if( gradeArray.length == 2 && ( gradeArray[1] == '+' || gradeArray[1] == '-' )){
        if (gradeArray[1] == '+') {
            if (gradeArray[0] != 'A') {
                gpa += 0.3;
            }
        }
        // '-' would be second character, so index should be 1
        if (gradeArray[1] == '-') {
            gpa -= 0.3;
        }
    } //Include else to handle other scenarios

    return gpa;
}

}

1 Comment

Logic is mentioned in comments. Fixed a few typos!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.