Question: Implement a method called getGrade that accepts an integer representing a students grade in a course and returns that students numerical course grade. Write a
Implement a method called getGrade that accepts an integer representing a students grade in a course and returns that students numerical course grade. Write a main to test the implemented method. The grade can be between 0.0 (failing) and 4.0 (perfect). Assume that scores are in the range of 0 to 100 and grades are based on the following scale: For an added challenge, make your method throw an IllegalArgumentException if the user passes a grade lower than 0 or higher than 100
Score Grade <60 0.0
60-62 0.7
63 0.8
64 0.9
65 1.0
92 3.7
93 3.8
94 3.9
>=95 4
I had trouble with this question my answer keeps getting an error can you look and see and help me with it please
public class getGrade {
public static void main(String[] args) { //This is the main method statement Scanner input = new Scanner(System.in); //Scanner class to read input from user boolean repeat = true; int score; double grade; while(repeat) { System.out.print("Enter a score or (-1 to exit): "); //prompt and read a score score = input.nextInt(); if(score == -1) //set repeat to false if -1 is entered repeat = false; else { grade = getGrade(score); //get the grade by calling the getGrade method System.out.printf("Numerical course grade is: %.1f ", grade); //display the grade } } input.close(); } public static double getGrade(int score) { // This is the getGrade method statement and the open curly bracket opens the getGrade method if (score < 0 || score > 100) { // this is for if the score is < 0 or > 100 throw new IllegalArgumentException(); // this is for if the score is < 0 or > 100 } // closing curly bracket double grade = 0.0; if (score < 60) { // If this condition is satisfied it produces a specific answer grade = 0.0; // If the above condition is met then it will produce the grade 0.0 } // closing curly bracket else if (score <= 62) { // If this condition is satisfied it will produce a specific answer grade = 0.7; //If the above condition is met then it will produce the grade 0.7 } // closing curly bracket else if (score >= 95) { // If this condition is satisfied it will produce a specific answer grade = 4.0; // If the above condition is met it will produce the grade 4.0 } // closing curly bracket else { // code that should be executed if it is false grade = 0.8; for(int i = 63; i < score; i++){ //increment by 0.1 for scores between 63 and 94 grade += 0.1; } return grade; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
