The two methods, removeNumber and displayRecords has already completed. Complete
ID: 674102 • 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;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
/**
* 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
Below is the required code for the procedures; I have written the code with an assumption that there are a total of 100 records, and we have to do search, remove operation on them. Please let me know if you have any query related to the question
/* Assuming there are 100 records for Phone Numbers */
PhoneRecord pr[100];
private static void removeNumberWithSameZipCode(int zip) {
for(i=0; i<100; i++)
{
if( zip == pr[i].zipCode ) pr[i].number = null;
}
}
private static void findNumber(String fname) {
for(i=0; i<100; i++)
{
if( fname.equals(pr[i].firstName) ) System.out.println(pr[i].number);
}
}
private static void displayRecordsWithSameLastName(String lname){
String name = null, phone = null;
int zip = 0;
for(i=0; i<100; i++)
{
if( lname.equals(pr[i].lastName) )
{
name = pr[i].getName();
phone = pr[i].getNumber();
System.out.println("Name is : " + name);
System.out.println("Phone is :" + phone);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.