CSIT115 Class 8 Exercise Add a Method to the PhoneEntry Example
                                                                             NAME:_____________________________

Here is the encapsulated PhoneEntry.java, with toString():

// A PhoneEntry object represents a name and a phone number for that person

// 5th version: encapsulated, and with toString

 

public class PhoneEntry {

    private String name;

    private String phoneNo;  // "617-334-1234" for ex.

   

    public PhoneEntry(String name0, String phoneNo0) {

      name = name0;

      phoneNo = phoneNo0;

    }

 

    public String getName() {

        return name;

    }

    public String getPhoneNo() {

        return phoneNo;

    }

    public String areaCode() {

      return phoneNo.substring(0,3);

    }

    public void changeAreaCode(String newCode) {

      phoneNo = newCode + phoneNo.substring(3);

    }

 

 

 

 

 

 

 

 

 

 

 

 

    public String toString() {

      return name + ": " + phoneNo;

    }

}

 

1.      In the space above, write an additional object method “isValid()” that checks that the phone number looks OK because the phoneNo String is the right length and has “-“ in the right places.  If you want to check more, fine.  This method returns boolean, true if the phone number is good, false otherwise.

 

2.      Write client code that creates a PhoneEntry object and then checks it by calling isValid for it.