Your main method does the following : Create a 5-element array called subjectMar
ID: 3580844 • Letter: Y
Question
Your main method does the following :
Create a 5-element array called subjectMarks that will carry numeric marks (double data type) of a student in five different subjects. Each mark can be a real number such as 40.5, 35.6 etc. Declare the array appropriately.
Fill the array with values: Prompt the user to enter the student’s mark in each subject, one by one and use those values to fill the elements of the subjectMarks array one by one.
Call the calculateAverage method and pass on the subjectMarks array as a parameter. This method will return the average mark of the student. Print out the average mark of the student on the console with proper comments. Use any required variable (for example- name it avgMark) to store the average mark value returned by the method.
Call the gradeCalculator method and pass on to this method as a parameter, the variable that contains the average mark (avgMark as obtained in step 3). This method (gradeCalculator ) calculates the letter grade based on the student’s average and returns a String that will be the letter grade of the student. Print out this letter grade into the console. You may use a variable name letterGrade to hold the letter grade returned by the method
Explanation / Answer
SubjectMarksTest.java
import java.util.Scanner;
public class SubjectMarksTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double subjectMarks[] = new double[5];
for(int i=0; i<subjectMarks.length; i++){
System.out.println("Enter the subject marks: ");
subjectMarks[i] = scan.nextDouble();
}
double average = calculateAverage(subjectMarks);
char letterGrade = gradeCalculator(average);
System.out.println("Letter grade is "+letterGrade);
}
public static double calculateAverage(double subjectMarks[]){
double sum = 0;
for(int i=0; i<subjectMarks.length; i++){
sum = sum + subjectMarks[i];
}
double average = sum/(double)subjectMarks.length;
return average;
}
public static char gradeCalculator(double avgMark ){
char letterGrade = '';
if(avgMark>= 90){
letterGrade = 'A';
}
else if(avgMark>= 80 && avgMark < 90){
letterGrade = 'B';
}
else if(avgMark>= 70 && avgMark < 80){
letterGrade = 'C';
}
else if(avgMark>= 60 && avgMark < 70){
letterGrade = 'D';
}
else{
letterGrade = 'F';
}
return letterGrade;
}
}
Output:
Enter the subject marks:
50
Enter the subject marks:
60
Enter the subject marks:
70
Enter the subject marks:
80
Enter the subject marks:
90
Letter grade is C
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.