Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that accepts a test score, that is, a positive integer in the ra

ID: 3579825 • Letter: W

Question

Write a program that accepts a test score, that is, a positive integer in the range 0 through 100 inclusive, and displays an equivalent letter grade: A (90+), B (80-89), C (70-79), D (60-69), F (under 60). Throw an exception if the input is in the wrong format or if it is out of range, print an error message, and halt gracefully.

Edit ot so that it takes an integer from the user and displays the appropriate letter grade so that it throws a custom Exception called OutOfBoundsException when the user inputs a number out of the 0-100 range (i.e., a number less than 0 or greater than 100). Thus, you must write your own custom Exception class and throw it in the appropriate section of your grade input program.

using java

Explanation / Answer

ScoreTest.java

import java.util.Scanner;


public class ScoreTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
      
       System.out.println("Enter the test score: ");
       int score = scan.nextInt();
       try{
       if(score < 0 || score > 100){
           throw new OutOfBoundsException("Invalid test score. Test score must be between 0 and 100.");
       }
       else{
           if(score >= 90 && score <=100){
               System.out.println("Letter grade: A");
           }
           else if(score >= 80 && score <90){
               System.out.println("Letter grade: B");
           }
           else if(score >= 70 && score <80){
               System.out.println("Letter grade: C");
           }
           else if(score >= 60 && score <70){
               System.out.println("Letter grade: D");
           }
           else{
               System.out.println("Letter grade: F");
           }
          
       }
       }
       catch(OutOfBoundsException e){
           System.out.println(e);
       }

   }

}

OutOfBoundsException.java


public class OutOfBoundsException extends Exception{
   String errorMsg ;
   public OutOfBoundsException(String s){
       this.errorMsg = s;
   }
public String toString(){
return (errorMsg ) ;
}  
}

Output:

Enter the test score:
82
Letter grade: B

Enter the test score:
105
Invalid test score. Test score must be between 0 and 100.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote