Write an application that displays a series of at least 10 student ID numbers (t
ID: 3762411 • Letter: W
Question
Write an application that displays a series of at least 10 student ID numbers (that you have stored in an array) and asks the user to enter a test letter grade for the student. Create an Exception class named GradeException that contains a static public array of valid grade letters (‘A’, ‘B’, ‘C’, ‘D’, ‘F’, and ‘I’) 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. At the end of the application, display all the student IDs and grades. Save the files as GradeException.java and TestGrade.java.
I'm having a lot of difficulty in Java. Can you please explain every step. Thanks
Explanation / Answer
//GradeException.java
public class GradeException
{
//instance variables of class
private int id;
private char grade;
//public character array of grades
public static char grades[]={'A','B','C','D','F','I'};
//Constructor to set student number and grade
public GradeException(int id, char grade)
{
this.id=id;
this.grade=grade;
}
//call valid method that check if grade is valid
//if valid return true otherwiser return false
public boolean valid(char grade)
{
boolean isValid=false;
for (int index = 0; index < grades.length; index++) {
if(grades[index]==grade)
isValid=true;
}
return isValid;
}
//Returns the string representation of GradeException class
@Override
public String toString() {
return "Invalid grade of student ID :"+id;
}
}//end of the class
---------------------------------------------------------------------------------------------------------------------------------------------------------
/**The java program TestGrade that prompts user to ener
* the grades of students from 1 to 10 ,
* If user enters invalid grade then set grade value I
* to student id number.
* Then print the student id and grade for 10 students*/
//TestGrade.java
import java.util.Scanner;
public class TestGrade
{
public static void main(String[] args)
{
//Create an array of id
int studentId[]={1,2,3,4,5,6,7,8,9,10};
//Character array of size 10
char[] grades=new char[10];
//Scanner object
Scanner scanner=new Scanner(System.in);
//set index =0
int index=0;
//read grade of a student
while(index < studentId.length)
{
System.out.print("Enter grade of student "+(index+1)+" : ");
//read grade
char grade = scanner.nextLine().charAt(0);
//create an object of GradeException of id and grade
GradeException gradeOject=new GradeException(studentId[index],grade);
//Check if not valid then set grade I
//print the message of the GradeException object
if(!gradeOject.valid(grade))
{
grades[index]='I';
System.out.println(gradeOject);
}
else
//otherwise set grade
grades[index]=grade;
//increment the index by one
index++;
}
System.out.println("Student Gradees");
System.out.println("-------------------------------");
System.out.printf("%-10s%-10s ","STUDENT ID","GRADE");
//print student id and grades of students
for (index = 0; index < grades.length; index++) {
System.out.printf("%-10d%-10c ",studentId[index],grades[index]);
}
}
}//end of the class
----------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter grade of student 1 : A
Enter grade of student 2 : B
Enter grade of student 3 : C
Enter grade of student 4 : D
Enter grade of student 5 : F
Enter grade of student 6 : R
Invalid grade of student ID :6
Enter grade of student 7 : E
Invalid grade of student ID :7
Enter grade of student 8 : Y
Invalid grade of student ID :8
Enter grade of student 9 : T
Invalid grade of student ID :9
Enter grade of student 10 : A
Student Gradees
-------------------------------
STUDENT IDGRADE
1 A
2 B
3 C
4 D
5 F
6 I
7 I
8 I
9 I
10 A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.