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

By looking at the codes below please answer the following question: Question: Ex

ID: 3839325 • Letter: B

Question

By looking at the codes below please answer the following question:

Question:

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

Yes, it is a good practice to have code like that of testSSN as a static method, as the definition of this method is not dependent on the object definition. So there is no need to allocate memory to these method for every object of Employee class created. So we can just create one static method,that can be shared by all instances of the class and can be called from constructor as well as setter method.

it is a very bad practice to call setter method from constructor because, these methods are public and any child class can override its functionality . This would impact the structure of the Employee class. If we are making the class Final or making setter function private .. this will not impact anything, as final class cannot be inherited and private methods cannot be accesed by child classes.