Student Grade Calculator
Part of the Fundamentals section of Coddy's Java journey — lesson 73 of 73.
Challenge
EasyCreate 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:
- Calculate the sum of all grades in the array.
- Calculate the average grade by dividing the sum by the number of grades.
- Return the average grade
- 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else