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)...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
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...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
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 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)...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...