Start learning free

LESSON 239 OF 285 · HELPER FUNCTIONS

Letter Grades in Java: A Helper Method and an ArrayList

Practice Read the full explanation

Given an array of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    JAVA · HELPER FUNCTIONS

    Letter Grades in Java, explained

    Problem overview

    Given an int[] of scores from 0 to 100, letterGrades returns a list with one letter per score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".

    The work is split in two methods. The helper letterGrade(int score) turns one score into its letter. The method letterGrades(int[] scores) goes over all the scores and collects the helper's answers.

    For scores = [95, 82, 71] the result is ["A", "B", "C"]. For [65, 40] it is ["D", "F"]. For [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short of it.

    Approach

    There are two separate questions here. "Which letter does this score get?" is a decision about one number. "Do that for every score and keep the answers" is a loop. Writing each as its own method keeps both short, and the loop never has to know anything about grade ranges.

    For the helper, the trick is to check the ranges from the top down. A score of 90 or more is an A. If the code gets past that check, the score is below 90, so "at least 80" already means 80 to 89, and no upper bound has to be written. Each if only needs one comparison, because every earlier one has filtered out the higher scores.

    Take the score 82. The test 82 >= 90 is false, so the method moves on. The test 82 >= 80 is true, so it returns "B" and the remaining checks never run. A score of 40 fails all four tests and falls through to "F".

    The cost is small: each score takes at most four comparisons, and the loop visits each score once, so the work grows in step with the number of scores.

    Step by step

    The helper, letterGrade

    1. If score >= 90, return "A" at once. A return ends the method, so the checks below only see scores under 90.
    2. If score >= 80, return "B". Scores of 90 and up have already left, so this means 80 to 89.
    3. If score >= 70, return "C".
    4. If score >= 60, return "D".
    5. After the last if, return "F". Only scores below 60 get this far, so the line needs no test and no else.

    The collector, letterGrades

    1. Create an empty List<String> named grades, backed by a new ArrayList. The letters will be added one at a time, so it starts empty.
    2. Write a for-each loop with an int named score over scores. It runs once per score, so each score produces exactly one letter.
    3. Inside the loop, call grades.add(letterGrade(score)). The inner call runs first and returns a String, which add then appends. Appending keeps the letters in the order of the scores.
    4. After the loop, return grades. The return goes after the loop's closing brace: inside the loop, only the first letter would ever come back.

    The solution in Java

    class Solution {
        public String letterGrade(int score) {
            if (score >= 90) {
                return "A";
            }
            if (score >= 80) {
                return "B";
            }
            if (score >= 70) {
                return "C";
            }
            if (score >= 60) {
                return "D";
            }
            return "F";
        }
    
        public List<String> letterGrades(int[] scores) {
            List<String> grades = new ArrayList<>();
            for (int score : scores) {
                grades.add(letterGrade(score));
            }
            return grades;
        }
    }
    

    Java details in this code:

    • Two methods, one class. letterGrades calls letterGrade by its name alone, because both belong to Solution. The helper's parameter is also called score, but each method has its own variables: the call hands the helper a copy of the loop's current score.
    • Array in, list out. The input is a fixed-size int[], but the result is a List<String> because the lesson builds it with add. An array's size is fixed when it is created, while an ArrayList grows as you add.
    • The declared type is the interface. List<String> grades = new ArrayList<>(); declares the variable as a List and creates an ArrayList. The <> tells Java to fill in the String type from the left side.
    • The imports. List and ArrayList live in java.util, so a standalone program needs import java.util.*;.
    • Printing a list. A List prints in square brackets without quotes around the strings, for example [A, B, C]. The values are still strings.

    Edge cases and common mistakes

    • Boundary scores. The values 59, 60, 69, 70, 79, 80, 89 and 90 are where off-by-one mistakes show up. Using > 90 instead of >= 90 would turn a 90 into a B. With the list [0, 59, 60, 69, 70, 79, 80, 89], the method returns [F, F, D, D, C, C, B, B].
    • Checking from the bottom up. If the first check were score >= 60, a 95 would return "D" immediately. Without upper bounds, the order of the checks matters: the highest range goes first.
    • Returning inside the loop. A return there ends letterGrades after the first score.
    • Comparing strings. If you later compare grades, use equals, as in grade.equals("A"). The == operator on strings compares references, not text.

    Testing

    These calls return the following lists:

    • letterGrades(new int[]{95, 82, 71}) returns [A, B, C].
    • letterGrades(new int[]{65, 40}) returns [D, F].
    • letterGrades(new int[]{100, 90, 89}) returns [A, A, B].
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free