Create a new Project and Class called Exam2_ YourName . Please write your name i
ID: 3578840 • Letter: C
Question
Create a new Project and Class called Exam2_YourName. Please write your name in the class name.
Your program will have a main method and two other methods called gradeCalculator and calculateAverage. Submit all the .java file called Exam2_YourName. Also ,copy and paste your code into a .docx file and upload the file as well. Please use the exam2 dropbox to submit your code.
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.
Write the code for the method. The method has the following features:
This method will take in as a parameter/argument the array containing marks of the student (as in subjectMarks array).
This method returns a double value corresponding to the average marks of the student. You may declare a double variable called avgMark that will contain the average marks.
This method calculates the average marks by taking one element of the array at a time, adding them to find the total marks. You may use a variable called totalMark to hold the total mark. After adding all the elements (,or marks), divide the total marks with the length of the array ( which gives the number of subjects), to obtain the average marks. Return the average marks carried by the variable avgMarks.
Anastasia Declin
Explanation / Answer
Hi, Please find my code.
Please let me know in case of any issue.
import java.util.Scanner;
public class Exam2_YourName {
public static double calculateAverage(double[] subjectMarks){
double totalMark = 0;
for(int i=0; i<5; i++){
totalMark += subjectMarks[i];
}
double avgMarks = totalMark/subjectMarks.length;
return avgMarks;
}
public static String gradeCalculator(double avgMarks){
if(avgMarks >= 90)
return "A";
else if(avgMarks >= 80)
return "B";
else if(avgMarks >= 70)
return "C";
else if(avgMarks >= 60)
return "D";
else if(avgMarks >= 50)
return "E";
else
return "F";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] subjectMarks = new double[5];
System.out.println("Enter marks for 5 subjects ");
for(int i=0; i<subjectMarks.length; i++){
subjectMarks[i] = sc.nextDouble();
}
double avgMark = calculateAverage(subjectMarks);
System.out.println("Average Marks: "+avgMark);
String letterGrade = gradeCalculator(avgMark);
System.out.println("Grade: "+letterGrade);
}
}
/*
Sample run:
Enter marks for 5 subjects
78.56
98.7
88.5
79.8
90
Average Marks: 87.112
Grade: B
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.