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

Introduction This program will ask the user for a person’s name and social secur

ID: 3848693 • Letter: I

Question

Introduction

This program will ask the user for a person’s 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. Note: Since you are creating all the classes for this lab, there are no files associated with this lab.

Task #1

Writing a Custom Exception Class

1. Create an exception class called SocSecException. The UML diagram for this class is below. SocSecException + 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.

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. SocSecProcessor + main(args: String[]) : void + isValid(ssn: String) : boolean

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 with user input

OUTPUT

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

This is Exception class

package com.SocialSecurityChecking;

public class SocSecException extends Exception {

   private String setMessage() {
       // TODO Auto-generated method stub

       return "Invalid social security number";
   }
}

This is Main class:

package com.SocialSecurityChecking;

public class SocSecProcessor {

   String name;
   String ssn;
   static boolean isValid(String ssn) throws SocSecException{
      
       int count = 0;
   for (char c : ssn.toCharArray()) {
   if (count == 3 || count == 6) {
   if (c != '-') {
   System.out.println("Invalid the social security number, dashes at wrong positions");
   throw new SocSecException();
   }
   } else if (c < '0' || c > '9') {
   System.out.println("Invalid the social security number, contains a character that is not a digit");
   throw new SocSecException();
   }
   count++;
   }
   if (count != 11) {
   System.out.println("Invalid the social security number, wrong number of characters");
   throw new SocSecException();
   }
   if(ssn.matches("\d\d\d-\d\d-\d\d\d\d")){
           //
      
       return true;
   }
   throw new SocSecException();
   // return true;
   }  
      
}

This is Driver class :

package com.SocialSecurityChecking;

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Scanner sc=new Scanner(System.in);
       SocSecProcessor ssp=new SocSecProcessor();
       while(true){
           System.out.print(" Continue enter y else n ? ");
           String ch=sc.next();
           if("y".equals(ch)){
               System.out.print(" Name? ");
               String name=sc.next();
              
               System.out.print("SSN? ");
               String ssn=sc.next();
              
               try {
                   ssp.isValid(ssn);
                   System.out.println(name+ssn+" is valid");
               } catch (SocSecException e) {
                  
                   //e.printStackTrace();
               }
              
              
              
           }else{
               System.exit(0);
           }
          
       }
   }
      
}

Output:


Continue enter y else n ? y

Name? Sam_Sly
SSN? 333-00-999
Invalid the social security number, wrong number of characters

Continue enter y else n ? y

Name? George_Washington
SSN? 123-45-6789
George_Washington123-45-6789 is valid

Continue enter y else n ? y

Name? Dudley_Doright
SSN? 222-00-999o
Invalid the social security number, contains a character that is not a digit

Continue enter y else n ? y

Name? Jane_Doe
SSN? 333-333-333
Invalid the social security number, dashes at wrong positions

Continue enter y else 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