Menu
Coddy logo textTech

Student Grade Calculator

Part of the Fundamentals section of Coddy's Java journey — lesson 73 of 73.

challenge icon

Challenge

Easy

Create a method named calculateAverageGrade that takes an array of integers (representing student grades) as input and returns the average grade as a double. The method should do the following:

  1. Calculate the sum of all grades in the array.
  2. Calculate the average grade by dividing the sum by the number of grades.
  3. Return the average grade
  4. Print the result using the following format: Average grade: [The result] and format the result to two decimal places.

Try it yourself

import java.util.Scanner;

public class Main {
    public static double calculateAverageGrade(int[] grades) {
        // Write your code here
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        String[] stringArr = text.split(",");
        int[] studentGrades = new int[stringArr.length];
        for (int i = 0; i < stringArr.length; i++) {
           studentGrades[i] = Integer.parseInt(stringArr[i]);
        }
        double averageGrade = calculateAverageGrade(studentGrades);
        System.out.printf("Average grade: %.2f", averageGrade);
    }
}

All lessons in Fundamentals