Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SocSecException\" For the \"SocSecProcessor\"class, the \"isValid\" method will

ID: 3529832 • Letter: S

Question

SocSecException"

For the "SocSecProcessor"class, the "isValid" method will need to use various string/wrapper class methods like:

- length()

- charAt(#)

- Character.isDigit(char)

Check for valid SSN format: ###-##-####


This program will ask the user 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.


=================================================================================================


1. Create an exception class called SocSecException. The UML 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.


+SocSecException(String error)


2. Create a driver program called SocSecProcessor.java. This program will have a

main method and a static method called isValid that will check if the social

security number is valid.


+main(args:String[ ]):void

+isValid(ssn:String):boolean



1. In the main method:

a) The main method should read a name and social security number from the

user as Strings.


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) True is returned 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 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

Explanation / Answer

import java.util.Scanner;

public class SocSecProcessor {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inputScanner = new Scanner(System.in);
String name;
String SSN;
String response;
do {

System.out.println("Enter a name: ");
name = inputScanner.nextLine();
System.out.println("Enter a SSN: ");
SSN = inputScanner.nextLine();
try {

if (isValid(SSN)) {
System.out.println("The name of the person: " + name);
System.out.println("The SSN is: " + SSN);
}
} catch (SocSecException sse) {
System.out.println("The name of the person: " + name);
System.out.println("The SSN is: " + SSN);
System.out.println(sse.getMessage());

}
System.out.println("Do you want to Continue? (y/n): ");
response = inputScanner.nextLine();

} while (response.equals("y"));

}

public static boolean isValid(String SSN) throws SocSecException {
if (SSN.length() != 11)
throw new SocSecException(
"Invalid the social security number, wrong number of characters ");
if (SSN.charAt(3) != '-' && SSN.charAt(6) != '-')
throw new SocSecException(
"Invalid the social security number, dashes at wrong");
for (int i = 0; i < SSN.length(); i++) {
if (i != 3 && i != 6 && !Character.isDigit(SSN.charAt(i))) {
throw new SocSecException(
"Invalid the social security number, contains a character that is not a digit ");
}
}
return true;

}

}

Output:

sss
Enter a SSN:
333-99
The name of the person: sss
The SSN is: 333-99
Invalid the social security number, wrong number of characters
Do you want to Continue? (y/n):
y
Enter a name:
AAA
Enter a SSN:
333-00-9999
The name of the person: AAA
The SSN is: 333-00-9999
Do you want to Continue? (y/n): n


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote