Write an application that displays a series of at least five Student IF numbers ( that you have stored in an array ) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException , display an appropriate message, and then store a 0 for the student's score. At the end of the application , display all the student IDs and scores. Save the files as ScoreException.java and Test Score.java.
//ScoreException.java
public class ScoreException extends Exception{
public ScoreException(String s) {
super(s);
}
}
//TestScore.java
import java.util.Scanner;
public class TestScore{
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
int ids[] = {123, 457, 875, 654, 666};
int scores[] = new int[5];
for(int i = 0; i < 5; i++) {
try {
System.out.print("Enter score for the student id #" + ids[i] + ": ");
scores[i] = in.nextInt();
if(scores[i] > 100 || scores[i] < 0)
throw new ScoreException("Invalid Score");
}
catch(ScoreException e) {
scores[i] = 0;
System.out.println("Entered invalid score");
}
}
System.out.println("The final scores are:");
for(int i = 0; i < 5; i++)
System.out.println("The score of the student id #" + ids[i] + " is " + scores[i]);
}
}
Comments
Leave a comment