Java Language:
Write an application that asks the user to enter a student ID and a test letter grade.
Create an Exception class names GradeException that contains an Array of valid grade letters that you can use to determine whether a grade entered from the application is valid.
In your application, throw a GradeException if the user does not enter a valid letter grade. Catch the GradeException, and then display an appropriate message. In addition, store an 'I' (for incomplete) for any student for whom an exception is caught. Display the student ID and Grade.
Java Code:
import java.util.*;
class GradeException extends Exception
{
String[] Array={"A","B","C","D"};
public GradeException(String s)
{
super("Incomplete");
}
public GradeException()
{
}
}
public class Main
{
public static void main(String[] args) {
int sid;
String grade;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Student ID");
sid=sc.nextInt();
try{
System.out.println("Enter test letter grade");
grade=sc.next();
int flag=0;
GradeException obj=new GradeException();
for(int i=0;i<obj.Array.length;i++)
{
if(grade.equals(obj.Array[i]))
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Student ID: "+sid+" Grade: "+grade);
else
throw new GradeException("Exeption");
}
catch (GradeException ex)
{
grade="I";
System.out.println("Student ID: "+sid+" Grade: "+grade);
}
}
}
Output:
Enter Student ID
8
Enter test letter grade
A //present is array
Student ID: 8 Grade: A
Enter Student ID
9
Enter test letter grade
P //not present is array
Student ID: 9 Grade: I
Note: File of the name has to be Main.java or else change name of
class from Main to the file name
Get Answers For Free
Most questions answered within 1 hours.