I want the solutions in net beans. Consider creating a method to calculate a stu
ID: 3906135 • Letter: I
Question
I want the solutions in net beans.
Consider creating a method to calculate a student's grade for the mid-term exam. The goal would be to have the instructor input a grade numeric value and have your logic return the letter grade equivalent. You may use the point-to-letter grade as listed in your syllabus as your guide. Even a null grade must receive a letter grade. If a negative point value is inadvertently entered then the method must attend to that situation and assure that only positive values can be entered. In no case can the grading be anonymous, identification of the student must be factored in. You do not have to write the actual code to accomplish this. But your logic has to provide a solution for all possible conditions.
Explanation / Answer
The below GradeCalculator class contains a method calculateGrade() which prompts the user to enter student name and score, validate the score thoroughly and calculate the letter grade based on score, then returns it. The below method is guaranteed to return a valid grade (A/B/C/D or F) in every possible cases. Thanks
// GradeCalculator.java
import java.util.Scanner;
public class GradeCalculator {
/**
* method to prompt the user to enter name and score, calculate the letter
* grade based on validated score
*
* @return A,B,C,D or F
*/
public static char calculateGrade() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String student = scanner.nextLine();
// apply the logic here to validate the student if you want.
double score = -1;
/**
* looping until the user enters a valid score between 0-100
*/
while (score < 0 || score > 100) {
System.out.print("Enter your numeric score (0-100): ");
try {
score = Double.parseDouble(scanner.nextLine());
} catch (Exception e) {
/**
* user entered some non numeric values
*/
System.out.println("Invalid grade, try again...");
}
if (score < 0 || score > 100) {
/**
* user entered a value not in the range 0-100
*/
System.out.println("Score must be between 0-100");
}
}
/**
* at this point, the score is validated, and we can proceed to
* calculate the grade letter
*/
char grade;
if (score >= 90) {
// 90-100 A
grade = 'A';
} else if (score >= 80) {
// 80-89 B
grade = 'B';
} else if (score >= 70) {
// 70-79 C
grade = 'C';
} else if (score >= 60) {
// 60-69 D
grade = 'D';
} else {
// under 60 F (failed)
grade = 'F';
}
// returning the grade letter
return grade;
}
public static void main(String[] args) {
/**
* getting and calculating the grade
*/
char grade = calculateGrade();
System.out.println("Your grade letter is " + grade);
}
}
/*OUTPUT*/
Enter student name: Alice
Enter your numeric score (0-100): -10
Score must be between 0-100
Enter your numeric score (0-100): 115
Score must be between 0-100
Enter your numeric score (0-100): xxy
Invalid grade, try again...
Score must be between 0-100
Enter your numeric score (0-100): 92
Your grade letter is A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.