Question

Java Language: Write an application that asks the user to enter a student ID and a...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”....
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”. B. Write a java program that creates the following two arrays: String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”}; int[] votes = {7345,4324,3211}; Write the code that will search the votes array for the candidate with the largest number of votes and prints the name...
Write the C++ language statements to ask a user to enter a number within the range...
Write the C++ language statements to ask a user to enter a number within the range of 0 to 100 (0 and 100 are valid inputs). When the value is not valid, you should prompt the user to enter another value. (The user may enter many invalid numbers, so you must use loop). Only after the user enters a valid number should you display the message “Good” and the value of the number.
Write a mips assembly language program that asks the user to enter and integer number and...
Write a mips assembly language program that asks the user to enter and integer number and read it. Then ask him to enter a bit position (between 0 and 31) and display the value of that bit.
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
In JAVA For practice using exceptions, this exercise will use a try/catch block to validate integer...
In JAVA For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by...
Write an application that asks a user to enter an integer. Display a statement that indicates...
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. ------------------------------------------ import java.util.Scanner; class EvenOdd {     public static void main(String[] args) {         // Write your code here     }     public static boolean isEven(int number) {     } }
Write an application from scratch where you are to write in Java the following methods to...
Write an application from scratch where you are to write in Java the following methods to be used by the main(): getFootValue() returning a Double value that is entered getInchValue() returning a Double value that is entered convertToCentimeters() returns a Double value using the calculated results from the getFootValue() and getInchValue() which will be used as the input values for the convertToCentimeters() method Next: The Exceptions that will be thrown if the values are invalid and these values are to...
The following program allows the user to enter the grades of 10 students in a class...
The following program allows the user to enter the grades of 10 students in a class in an array called grade. In a separate loop, you need to test if a grade is passing or failing, and copy the grade to an array to store passing or failing grades accordingly. A passing grade is a grade greater than or equal to 60. You can assume the use will enter a grade in the range of 0 to 100, inclusive. ...
B.2. Write a Java program to display a dialog box that asks you to enter your...
B.2. Write a Java program to display a dialog box that asks you to enter your user name and the age as shown below: The program displays in a dialog box the sum of digits of your age. For example, if you age is 19, the sum will be 1+9 = 10 and the output will be as shown below: ( it just shows a message box that says the sum of the digits age of john is 10.
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT