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
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:
Get Answers For Free
Most questions answered within 1 hours.