Question
Lab Exercise: Complete the code The following code will read a score form the user. The exception will be thrown with an appropriate message in
Lab Exercise:
Complete the code
The following code will read a score form the user. The exception will be thrown with an appropriate message in the following cases:
If the score is less than 60 and not negative then "Not Passed" message will be displayed.
If the score is between 60 and 100 then " Success! the entered score is ." will be displayed.
See the output to have better understanding.
import java.util.Scanner;
public class ExceptionTest {
public static void main( String args[] ){
// input the number from user in the try block
Scanner input = new Scanner (System.in);
try{
int number = input.nextInt();
System.out.println(checkScore(number));
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
} // end main method
// lessThan60 method will throw an exception if the number is negative
// otherwise it will retuurn the string "Not passed".
public static String lessThan60( int number ) throws Exception
{
}
// checkScore method will thrwo an exception if the number is greater than 100.
// else if the number is less than 60 the lessThan60 method will be invoked.
// otherwise the string ("Sucess! the entered score is "+number) will be returned.
public static String checkScore( int number ) throws Exception
{
}
}
Sample output :
90
Success! the entered score is 90
200
java.lang.Exception: the entered score is higher than 100
40
Not passed
-70
java.lang.Exception: the entered score is negative!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started