By looking at the codes below please answer the two following questions: Questio
ID: 3839311 • Letter: B
Question
By looking at the codes below please answer the two following questions:
Questions:
1 - Is the exception you create a checked or an unchecked exception? Why?
2 - Explain why it is a good practice to use a static method in order to avoid code duplication in the constructor and set method for the validation of the SSN number in the Employee class. Could you have just called the set method from the constructor? Is this considered a good practice? If not why?
Codes:
Source code: SSNFormatException
public class SSNFormatException extends Exception {
public SSNFormatException()
{
super("Invalid SSN Format");
}
public SSNFormatException(String msg)
{
super(msg);
}
public SSNFormatException(String msg, Throwable t)
{
super(msg,t);
}
public SSNFormatException(Throwable t)
{
super("Invalid SSN format", t);
}
}
---
Source code: Employee
public class Employee {
private String firstName, lastName, socialSecurityNumber;
//default constructor
Employee()
{
firstName = lastName = socialSecurityNumber ="";
}
//parameterizied constructor
Employee(String fname, String lname, String ssn) throws SSNFormatException
{
firstName = fname;
lastName = lname;
if(testSSN(ssn))
socialSecurityNumber = ssn;
}
private static boolean testSSN(String ssn) throws SSNFormatException
{
if(ssn.length() != 11)
throw new SSNFormatException("Invalid the social security number, wrong number of characters");
else
{
for(int i=0;i<11;i++)
{
if(i==3 || i==6 ) //position for dashes
{
if(ssn.charAt(i)!='-')
throw new SSNFormatException("Invalid the social security number, dashes at wrong positions");
}
else //digit positions
{
if(!Character.isDigit(ssn.charAt(i))) //not a digit
throw new SSNFormatException("Invalid the social security number, contains a character that is not a digit");
}
}
}
return true;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(String socialSecurityNumber) throws SSNFormatException {
if(testSSN(socialSecurityNumber))
this.socialSecurityNumber = socialSecurityNumber;
}
}
---
Source code: TestSSN
import java.util.Scanner;
public class TestSSN {
public static void main(String[] args) {
String ans;
Scanner scanner=new Scanner(System.in);
Employee e;
String fname, lname, ssn;
do
{
System.out.print(" Insert Employee First Name: ");
fname = scanner.next();
System.out.print(" Insert Employee Last Name: ");
lname = scanner.next();
System.out.print(" Insert Employee SSN: ");
ssn = scanner.next();
try {
e = new Employee(fname, lname, ssn);
System.out.println("Employee created successfully.");
} catch (SSNFormatException ex) {
System.out.println(ex.getMessage());
}
System.out.print(" Continue? ");
ans = scanner.next();
}while(ans.equalsIgnoreCase("y"));
}
}
Explanation / Answer
Question-1
Java Exception class is categorized into two types:
1.Checked exception
2.Unchecked exception
An exception is called unchecked exception when it is determined or checked during runtime.Error falls under unchecked exception.
An exception is called checked exception when it is happened during compile time.IOException,ClassNotFound exceptions fall under checked exception.
In the above program, the exception( SSNFormatException) created is called Custom exception as the exception is created or customize according to the need of the program.
The exception is checked exception as it extends Exception class. It is a subclass of Exception class. If the created exception extends RuntimeException then it would be said unchecked exception.
Question 2:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.