Task #1 Writing a Custom Exception Class 1. Create an exception class called Soc
ID: 3715946 • Letter: T
Question
Task #1 Writing a Custom Exception Class
1. Create an exception class called SocSecException. The UML diagram for this class is below
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.
2. 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.
Task #2 Writing Code to Handle an Exception
1. In the main method:
a. The main method should read a name and social security number from the user as String objects.
b. The main method should contain a try-catch statement. This statement tries to check if the social security number is valid by using the method isValid. If the social security number is valid, it prints the name and social security number. If a SocSecException is thrown, it should catch it and print out the name, social security number entered, and an associated error message indicating why the social security number is invalid.
c. A loop should be used to allow the user to continue until the user indicates that they do not want to continue.
2. The static isValid method:
a. This method throws a SocSecException.
b. Returns true if the social security number is valid, false otherwise.
c. The method checks for the following errors and throws a SocSecException with the appropriate message.
i) Number of characters not equal to 11. (Just check the length of the string) ii) Dashes in the wrong spots. iii) Any non-digits in the SSN. iv) Hint: Use a loop to step through each character of the string, checking for a digit or hyphen in the appropriate spots.
3. Compile, debug, and run your program. Sample output is shown below in italics with user input in bold.
Name? Sam Sly
SSN? 333-00-999
Invalid the social security number, wrong number of characters
Continue? y
Name? George Washington
SSN? 123-45-6789
George Washington 123-45-6789 is valid
Continue? y Name? Dudley Doright
SSN? 222-00-999o
Invalid the social security number, contains a character that is not a digit
Continue? y
Name? Jane Doe
SSN? 333-333-333
Invalid the social security number, dashes at wrong positions
Continue? n
SocSecException + SocSecException(error : String) :Explanation / Answer
import java.util.Scanner;
/**
*
*/
/**
* @author your name
*
*/
public class SocSecProcessor {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // To read the data from user
//Loop to read inputs
String choice="y";
do
{
System.out.println("Name?");
String name = sc.nextLine();
System.out.println("SSN?");
String ssn = sc.nextLine();
// Using try and catch
try {
if (isValid(ssn)) {
System.out.println(name + " " + ssn + " is Valid");
}
} catch (SocSecException e) {
System.out.println(e.getMessage());
}
System.out.println("Continue?");
choice=sc.nextLine();
}while(choice.equalsIgnoreCase("y"));
}
/***
*
* @param ssn
* @return
* @throws SocSecException
*/
private static boolean isValid(String ssn) throws SocSecException {
boolean valid = true;
if (ssn.isEmpty() || ssn.length() != 11) // check for the length
{
valid = false;
throw new SocSecException("Invalid the social security number, wrong number of characters");
} else {
// We need charatcer at 2 places , 3rd index and 6 th index .
int index = 0;
while (index < 11 && valid) {
Character ch = ssn.charAt(index);
// Check if digits are at correct position
if ((!Character.isDigit(ch) && (index == 0 || index == 1 || index == 2 || index == 4 || index == 5
|| index == 7 || index == 8 || index == 9 || index == 10))) {
valid = false;
throw new SocSecException(
"Invalid the social security number, contains a character that is not a digit");
// Check if Dash are at correct position
} else if ((ch != '-' && (index == 3 || index == 6))) {
valid = false;
throw new SocSecException("Invalid the social security number, dashes at wrong positions");
}
else
{
index++;
}
}
}
return valid;
}
}
public class SocSecException extends Exception {
private static final long serialVersionUID = 1L;
public SocSecException(String error) {
super(error);
}
}
Output:
Name?
Sam Sly
SSN?
333-00-999
Invalid the social security number, wrong number of characters
Continue?
y
Name?
George Washington
SSN?
23-45-6789
Invalid the social security number, wrong number of characters
Continue?
y
Name?
George Washington
SSN?
123-45-6789
George Washington 123-45-6789 is Valid
Continue?
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.