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

i. Write a generic class definition for class PhoneBook that can could be suitab

ID: 639030 • Letter: I

Question

i. Write a generic class definition for class PhoneBook that can could be suitable to create PhoneBook objects that use names as person identifiers (which would have type String), or social security numbers (considered as integer numbers without any blanks or dashes). Your class should define the following operations: findPerson, addPerson, deletePerson. While the PhoneBook objects can be created to have the above two types of person IDs, the same object can't mix and match these ID types. (3 points) ii. Write the main method that (a) creates two phonebook objects, one for using names as IDs and the other for using SSN as IDs, then (b) adds one entry into each phonebook then (c) tries to find a person in each phone book and print out the result. Compile, run, and provide the printout. (2 points) iii. Write the main method that, again, creates two phonebook objects as above (one for each type of person ID), and then attempts to insert a person with SSN as ID into the phonebook object that is supposed to use names. Compile, run, and provide the printout. (2 points)

Explanation / Answer

1)

Program code: PhoneBook

import java.util.*;

public class PhoneBook<T>

{

     T SSNID;

     String phoneNumber;

     Scanner in=new Scanner(System.in);

     ArrayList<PhoneBook> arr=new ArrayList<PhoneBook>();

     public PhoneBook()

     {        

     }

     public T getSSNID()

     {

          return SSNID;

     }

     public void setSSNID(T ssnid)

     {

          SSNID = ssnid;

     }

     public String getPhoneNumber()

     {

          return phoneNumber;

     }

     public void setPhoneNumber(String phoneNumber)

     {

          this.phoneNumber = phoneNumber;

     }

    

     void addPerson(T name, String phoneNumber)

     {

          PhoneBook pb=new PhoneBook();

          pb.setSSNID(name);

          pb.setPhoneNumber(phoneNumber);

          arr.add(pb);

     }

    

     boolean findPerson()

     {        

          T name;

          System.out.println("Enter the ID of the person: ");

          name=(T)in.next();

          boolean flag=false;

          for(int i=0;i<arr.size();i++)

          {

              if(name.equals(arr.get(i).getSSNID()))

              {

                   System.out.println("The person was found at "+(i+1)+" position.");

                   System.out.println("The details are: ");

                   System.out.println("ID: "+arr.get(i).getSSNID());

                   System.out.println("Phone number: "+arr.get(i).getPhoneNumber());

                   flag=true;

                   break;

              }            

          }

          return flag;

     }

    

     boolean deletePerson()

     {

          T name;

          System.out.println("Enter the ID of the person: ");

          name=(T)in.next();

          boolean flag=false;

          for(int i=0;i<arr.size();i++)

          {

               if(name.equals(arr.get(i).getSSNID())||name==(arr.get(i).getSSNID()))

              {

                   arr.remove(i);

                   flag=true;

                   break;

              }            

          }        

          return flag;

     }

    

     public String toString()

     {

          String s="";

          for(int i=0;i<arr.size();i++)

          {

              s+="ID: "+arr.get(i).getSSNID()+" ";

              s+="Phone number: "+arr.get(i).getPhoneNumber()+" ";              

          }        

          return s;

     }

}

---------------------------------------------------------------------------------------------------------------------

2)

MainMethod:

import java.util.*;

public class PhoneBookImplementation

{

     public static void main(String args[])

     {

          Scanner input=new Scanner(System.in);

          String ssName;

          String phoneNum;

          int ssID;

          PhoneBook<String> pbName=new PhoneBook<String>();

          PhoneBook<Integer> pbInt=new PhoneBook<Integer>();

         

          System.out.println("Enter 3 persons phone numbers using name as Id: ");

          for(int i=0;i<3;i++)

          {

              System.out.println("Enter the name of the person as ID: ");

              ssName=input.next();

              System.out.println("Enter the phone number: ");

              phoneNum=input.next();

              pbName.addPerson(ssName, phoneNum);

          }

          System.out.println("Enter 3 persons phone numbers using SSN as Id: ");

          for(int i=0;i<3;i++)

          {

              System.out.println("Enter the SSN of the person as ID: ");

              ssID=input.nextInt();

              System.out.println("Enter the phone number: ");

              phoneNum=input.next();

              pbInt.addPerson(ssID, phoneNum);

          }

          System.out.println("=======================================================");

          System.out.println("The details of the PhoneBooks: ");

          System.out.println("The details of PhoneBook-1: "+pbName.toString());

          System.out.println("=======================================================");

          System.out.println("The details of PhoneBook-2: "+pbInt.toString());

          System.out.println("=======================================================");

          System.out.println("Find the record: ");

          System.out.println("Whick book would you like to search?");

          System.out.println("1. To search in Phone Book-1.");

          System.out.println("2. To search in Phone Book-2.");

          int search=input.nextInt();

          if(search==1)

          {

              if(pbName.findPerson())

                   System.out.println("Found: True");

              else

                   System.out.println("Sorry! Could not find the person.");

          }

          else if(search==2)

          {

              if(pbInt.findPerson())

                   System.out.println("Found: True");

              else

                   System.out.println("Sorry! Could not find the SSID.");

          }

         

          System.out.println("=======================================================");

          System.out.println("From whick book would you like to delete?");

          System.out.println("1. To search in Phone Book-1.");

          System.out.println("2. To search in Phone Book-2.");

          int search1=input.nextInt();

          if(search1==1)

          {

              System.out.println("Deleting a person from the PhoneBook-1" );

              if(pbName.deletePerson())

                   System.out.println("Found: True");

              else

                   System.out.println("Sorry! could not find the record.");

          }

          else if(search==2)

          {

              System.out.println(" Deleting a person from the PhoneBook-2" );

              if(pbInt.deletePerson())

                   System.out.println("The record is sucessfully deleted.");

              else

                   System.out.println("Sorry! could not find the record.");

          }

         

          System.out.println(" =======================================================");

          System.out.println("The details of the PhoneBooks after deleting records: ");

          System.out.println("The details of PhoneBook-1: "+pbName.toString());

          System.out.println("The details of PhoneBook-2: "+pbInt.toString());

         

     }

}

-------------------------------------------------------------------------------------------------------

Sample output:

Enter 3 persons phone numbers using name as Id:

Enter the name of the person as ID:

Tito

Enter the phone number:

012-12345

Enter the name of the person as ID:

Rachel

Enter the phone number:

012-23456

Enter the name of the person as ID:

Walter

Enter the phone number:

012-345345

Enter 3 persons phone numbers using SSN as Id:

Enter the SSN of the person as ID:

123234

Enter the phone number:

123-23456

Enter the SSN of the person as ID:

234214

Enter the phone number:

123-67875

Enter the SSN of the person as ID:

333222

Enter the phone number:

123-23457

=======================================================

The details of the PhoneBooks:

The details of PhoneBook-1:

ID: Tito Phone number: 012-12345

ID: Rachel    Phone number: 012-23456

ID: Walter    Phone number: 012-345345

=======================================================

The details of PhoneBook-2:

ID: 123234    Phone number: 123-23456

ID: 234214    Phone number: 123-67875

ID: 333222    Phone number: 123-23457

=======================================================

Find the record:

Whick book would you like to search?

1. To search in Phone Book-1.

2. To search in Phone Book-2.

1

Enter the ID of the person:

Eric

Sorry! Could not find the person.

=======================================================

From whick book would you like to delete?

1. To search in Phone Book-1.

2. To search in Phone Book-2.

1

Deleting a person from the PhoneBook-1

Enter the ID of the person:

Eric

Sorry! could not find the record.

=======================================================

The details of the PhoneBooks after deleting records:

The details of PhoneBook-1:

ID: Tito Phone number: 012-12345

ID: Rachel    Phone number: 012-23456

ID: Walter    Phone number: 012-345345

The details of PhoneBook-2:

ID: 123234    Phone number: 123-23456

ID: 234214    Phone number: 123-67875

ID: 333222    Phone number: 123-23457