Question

WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with...

WITH JAVA

Follow the instructions in the attached to complete Task#2 and submit work along with screenshots of your output.

I have attached the class code for Task#1 that you can use while completing Task#2.

Task#1 CODE

/**
SocSecException exception class
*/

public class SocSecException extends Exception
{
public SocSecException(String error)
{
super("Invalid the social security number, " + error);
}
}

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 in bold.

OUTPUT (boldface is user input)

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

Homework Answers

Answer #1

Check The Code Below code Fragment (Find the Code explanation at the end):

Expected Output:


Following is the Code Sample,

CODE :

//FILE- App.java

package p1;

import java.util.Scanner;

public class App {

   public static void main(String[] args) {

       boolean userWantToContinue = true;

       // Scanner is used for accepting data from the User through Standard Input: in
       // Standard input is the Console
       Scanner scanner = new Scanner(System.in);

       // Actual Logic for Social Security Number
       while (userWantToContinue) {

           System.out.println("Name? ");

           String userName = scanner.nextLine();

           System.out.println("SSN? ");

           String socialSecurityNumber = scanner.nextLine();

           try {
               if (isValid(socialSecurityNumber)) {
                   System.out.println(userName + " " + socialSecurityNumber + " is Valid" );
               }
           } catch (SocSecException e) {

               System.out.println("User Name : " + userName);
               System.out.println(e.getMessage());
           }

           // Check if User wants to Continue using the System
           System.out.println("Continue ?");

           String enter = scanner.nextLine();

           if (enter.equals("y")) {
               userWantToContinue = true;
           } else {
               userWantToContinue = false;
               System.out.println("Good Bye");
           }
       }
   }

   static boolean isValid(String socialSecurityNumber) throws SocSecException {

       char[] charArray = socialSecurityNumber.toCharArray();
      
       for (int i = 0; i < charArray.length; i++) {
           if(i==0) {
               if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                   throw new SocSecException(socialSecurityNumber + " Invalid the social security number, contains\r\n"
                           + "a character that is not a digit");
               }
           }else {
               if(i==1) {
                   if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                       throw new SocSecException(socialSecurityNumber + " Invalid the social security number, contains\r\n"
                               + "a character that is not a digit");
                   }
               }else {
                   if(i==2) {
                       if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                           throw new SocSecException(socialSecurityNumber + " Invalid the social security number, contains\r\n"
                                   + "a character that is not a digit");
                       }
                   }else {
                       if(i==3) {
                           if(!(socialSecurityNumber.charAt(i) == '-')) {
                               throw new SocSecException(socialSecurityNumber
                                       + " Invalid the social security number, dashes at wrong\r\n" + "positions");
                           }
                       }else {
                           if(i==4) {
                               if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                   throw new SocSecException(socialSecurityNumber + " Invalid the social security number, "
                                           + "contains\r\n"
                                           + "a character that is not a digit");
                               }
                           }else {
                               if(i==5) {
                                   if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                       throw new SocSecException(socialSecurityNumber + " Invalid the social security number, "
                                               + "contains\r\n"
                                               + "a character that is not a digit");
                                   }
                               }else {
                                   if(i==6) {
                                       if(!(socialSecurityNumber.charAt(i) == '-')) {
                                           throw new SocSecException(socialSecurityNumber
                                                   + " Invalid the social security number, dashes at wrong\r\n" + "positions");
                                       }
                                   }else {
                                       if(i==7) {
                                           if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                               throw new SocSecException(socialSecurityNumber
                                                       + " Invalid the social security number, "
                                                       + "contains\r\n"
                                                       + "a character that is not a digit");
                                           }
                                       }else {
                                           if(i==8) {
                                               if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                                   throw new SocSecException(socialSecurityNumber
                                                           + " Invalid the social security number, "
                                                           + "contains\r\n"
                                                           + "a character that is not a digit");
                                               }
                                           }else {
                                               if(i==9) {
                                                   if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                                       throw new SocSecException(socialSecurityNumber
                                                               + " Invalid the social security number, contains\r\n"
                                                               + "a character that is not a digit");
                                                   }
                                               }else {
                                                   if(i==10) {
                                                       if(!Character.isDigit(socialSecurityNumber.charAt(i))) {
                                                           throw new SocSecException(socialSecurityNumber
                                                                   + " Invalid the social security number, contains\r\n"
                                                                   + "a character that is not a digit");
                                                       }
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }

       if(charArray.length < 11) {
           throw new SocSecException(socialSecurityNumber + " Invalid the social security number, Wrong Number of Characters");
       }
       return true;

   }

}

//FILE: SocSecException.java

package p1;

public class SocSecException extends Exception {
   public SocSecException(String error) {
       super("Invalid the social security number: " + error);
   }
}

Proper Explanation of Code:





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
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Objective The Final Project aims to demonstrate your ability to analyze data from a big database,...
Objective The Final Project aims to demonstrate your ability to analyze data from a big database, exercise use of arrays of objects, external classes, processing files and user interaction. For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT