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

The two methods, removeNumber and displayRecords has already completed. Complete

ID: 674101 • Letter: T

Question

The two methods, removeNumber and displayRecords has already completed. Complete the remaining 4 methods.

// Represents a record containing a name and a phone number
class PhoneRecord {
   private String firstName;
   private String lastName;
   private String number;
   private int zipCode;

   /**
   * NAME:       PhoneRecord
   * BEHAVIOR:   Constructs a phone record containing the specified name and phone number
   * PARAMETERS: personName - name of a person phoneNumber - phone number for that person
   */

   public PhoneRecord(String firstName, String lastName, String phoneNumber, int zipCode) {
       this.firstName = firstName;
       this.lastName = lastName;
       number = phoneNumber;
       this.zipCode = zipCode;
   }

   /**
   * NAME:       getName
   * BEHAVIOR:   Returns the name stored in this record
   * PARAMETERS: None
   * RETURNS:    The name stored in this record
   */
   public String getName() {
       return firstName+" "+lastName;
   }

   /**
   * NAME:       getNumber
   * BEHAVIOR:   Returns the phone number stored in this record
   * PARAMETERS: None
   * RETURNS:    The phone number stored in this record
   */
   public String getNumber() {
       return number;
   }
  
   /**
   * NAME:       getZipCode
   * BEHAVIOR:   Returns the zip code stored in this record
   * PARAMETERS: None
   * RETURNS:    zip code
   */

   public int getZipCode(){
       return zipCode;
   }
}

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

import java.util.Scanner;

public class PhoneDirectory {
   // Class variables
   static PhoneRecord[] records = new PhoneRecord[50];
   static int recordCount = 0;
   static Scanner sc ;


   public static void main(String[] args) {


       // Display list of commands
       System.out.println("Phone directory commands: "
               + " a - Add a new phone number "
               + " d - Display All "
               + " da -Display all with same Last Name "
               + " f - Find a phone number "
               + " r - Remove a phone number "
               + " rc -Remove all records with same zip code "
               + " q - Quit ");

       // Read and execute commands
       while (true) {
           sc= new Scanner(System.in);

           // Prompt user to enter a command
           System.out.print("Enter command (a, d, da, f, r, rc or q): ");
           String command = sc.nextLine();

           // Determine whether command is "a", "f", "q", or
           // illegal; execute command if legal.
           if (command.equalsIgnoreCase("a")) {

               // Command is "a". Call addNumber to add a new
               // name and number to the database
               addNumber();
           } else if (command.equalsIgnoreCase("f")) {

               // Command is "f". Call findNumber to find phone
               // numbers for given first name.
               findNumber();
           } else if (command.equalsIgnoreCase("d")) {

               // Command is "d". Call displayRecords to display all phone
               // numbers
               displayRecords();
           }else if (command.equalsIgnoreCase("da")) {

               // Command is "d". Call displayRecords to display all phone
               // numbers
               displayRecordsWithSameLastName();
           }
           else if (command.equalsIgnoreCase("r")) {

               // Command is "r". Call removeNumber to remove phone
               // numbers that match the user's criteria.
               removeNumber();
           }else if (command.equalsIgnoreCase("rc")) {

               // Command is "r". Call removeNumber to remove all phone
               // numbers that match the given zipcode.
               removeNumberWithSameZipCode();
           }
           else if (command.equalsIgnoreCase("q")) {

               // Command is "r". Call removeNumber to remove phone
               // numbers that match the user's criteria.
               break;
           }
           else {

               // Command is illegal. Display error message.
               System.out.println("Command was not recognized; "
                       + "please enter only a, d, da, f, r, rc or q.");
           }

           System.out.println();
       }
   }

   /**
   * NAME:       addNumber
   * BEHAVIOR:   add a new name (first and last) , phone number and zipcode to the database
   * PARAMETERS: None
   * COMMENTS:   refer the word doc for example of adding record
   */
   private static void addNumber() {
   }

   /**
   * NAME:       removeNumber
   * BEHAVIOR:   removes the first record of a given phone no.,
   */

   private static void removeNumber() {
       System.out.println("Enter phone no:");
       String pFind = sc.next();
       int index = 0;
       boolean found = false;
       for(int i=0;i<recordCount;i++){
           if(records[i].getNumber().equals(pFind))
           {
               index =i;
               for(int j=index;j<recordCount;j++){
                   records[j]=records[j+1];
               }
               System.out.println("Record with phone no: "+ pFind+" deleted. Remaining records:"+(recordCount-1));
               records[recordCount-1]=null;
               recordCount--;
               found = true;
               break;
           }

       }
       if(!found)
           System.out.println("Record not found.");


   }

   /**
   * NAME:       displayRecords
   * BEHAVIOR:   display all phone records
   */
   private static void displayRecords() {
       for (int i = 0; i < recordCount; i++) {
           System.out.println(i + 1 + ". " + records[i].getName() + " "
                   + records[i].getNumber()+" "+records[i].getZipCode());
       }
   }

   /**
   * NAME:       removeNumberWithSameZipCode
   * BEHAVIOR:   remove all phone numbers that match the given zipcode.
   */

   private static void removeNumberWithSameZipCode() {
       // your code here
   }

   /**
   * NAME:       findNumber
   * BEHAVIOR:   findNumber to find phone number for a give first name
   * COMMENT:    it should list all the numbers for matching input (Refer word doc for example)
   * HINT:        use PhoneRecord getName() method in conjunction with startsWith(String prefix) String method, see:
   *            http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29
   */

   private static void findNumber() {
       // your code here
      
   }

   /**
   * NAME:       displayRecordsWithSameLastName
   * BEHAVIOR:   display all phone records with given last name
   */
   private static void displayRecordsWithSameLastName(){
       // your code here

   }

Explanation / Answer

public class PhoneRecord
{
private String name; // person's name: unique key for a PhoneRecord object
private int phoneNum; // person's phone number
public PhoneRecord(String aName, int aPhoneNum)
{
name = aName;
phoneNum = aPhoneNum;
}
public void setPhoneNum(int aPhoneNum)
{
phoneNum = aPhoneNum;
}
public String getName()
{
return name;
}
public int getPhoneNum()
{
return phoneNum;
}
public String toString()
{
return name; // use name+phoneNum if you need phone # too
}
public String toFormattedString()
{
final int PADDED_NAME_LENGTH = 10;
String paddedName = name;
while (paddedName.length() < PADDED_NAME_LENGTH)
paddedName += " ";
return paddedName + " " + phoneNum;
}
}


zip


public class ZipCode
{
private int zipCode = -1;
public ZipCode(int inZipCode)
{
setZipCode(inZipCode);
}
public void setZipCode(int inZipCode)
{
if ((inZipCode > 9999) & (inZipCode < 100000))
zipCode = inZipCode;
}
public int getZipCode()
{
return zipCode;
}
public static void main(String[] arguments)
{
ZipCode zip1 = new ZipCode(75044);
ZipCode zip2 = new ZipCode(320840);
if (zip1.getZipCode() != -1)
System.out.println("Zip1: " + zip1.getZipCode());
if (zip2.getZipCode() != -1)
System.out.println("Zip2: " + zip2.getZipCode());
}
}