Question: USING JAVA... This program will ask the suer for a persons name and social security number. THe program will then check to see if the
USING JAVA...
This program will ask the suer for a persons name and social security number. THe program will then check to see if the social security number is valid. An exception will be thrown if an invalid SSN is entered.
You will be creating your own exception class in this program. You will also create a driver program that will use the exception class. Within the driver program you will include a static method that throws the exception.
Task 1
Create an exception class called SocSecException The UML diagram for this class is below
| SoSecException |
| +SocSecException(error : String): |
The constructor will call the superclass constructor. It will set the message associated with the exception to "invalid social security number" concatenated with the error string.
Create a driver program called SocSecProcessor this program will have a main method and a static method called isValid that will check if the social security number is valid.
| SoSecProcessor |
| +main(args : String[ ]): void +isValid(ssn : String) : boolean |
Writing Code to Handle an Exception
In the main method-
The main method should read a name and social security number from the user as String objects.
The main method should contain a try-catch statement. This statement tries to check if the social number is valid using the method isValid. If the social security number is valid, it prints the name and social security number. If a SocSecException is thrown is should catch it and print out the name, social security number entered, and an associated error message indicating why the social secuirty number is invalid.
A loop should be used to allow the user to continue until the use indicates that they do not want to continue.
The static isValid method-
This method throws a SocSecException
Returns true if the social security number is valid, false otherwise
the method checks of the following errors and throws a SocSecException with the appropriate message
-number of character not equal to 11
- dashes in the wrong spots
- any non-digits in the SSN
-use a loop to step through each character of the string checking for a digit or a hypher in the appropriate spots
Your submission to BB should be your "SocSecException.java" and "SocSecProcessor.java" files.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
