header

Homework 7

Write a class named PhoneNumber that stores a phone number. A phone number isn't really a number of course and should be stored as a String value consisting of a 3-digit area code, a hyphen, a 3-digit exchange, a hyphen, and 4 digits. The college phone number would be stored as the string value

859-858-3511

Your class should include an initialization constructor (as the only constructor), a setter (named setNumber), a getter (named getNumber), and a class method (named isValid) that returns true if its string parameter has a valid format and false otherwise.

The isValid Method

The only thing that is conceptually new in this assignment is the isValid method. It returns a boolean value and since it is a class method rather than an instance method, it must be declared as a static method. The body of the method consists of a single return statement (just like our getter methods):

public static boolean isValid(String str)
{
    return expression;
}

The expression in the return statement is a logical expression that evaluates to true if the string parameter has the form of a phone number and false otherwise. Your task is to write this expression.

The string parameter is a valid phone number if and only if all of the following conditions are met:

The charAt method of the String class returns the character at a specific position within a string. The Character class has a method named isDigit that returns true if its character parameter is a digit and false otherwise. For example, the following logical expression will return true if the first character (the character at position zero) is a digit and false otherwise:

Character.isDigit(str.charAt(0))

The charAt and isDigit methods along with appropriate relational and logical operators are all you need to write the expression.

Testing Your isValid Method

1. After you have successfully compiled your code, right-click on the icon for your PhoneNumber class. You'll get a drop-down menu like this one:

Drop-Down Menu

As with all of the other classes you have written, you can choose to create a new PhoneNumber object by running the constructor method. In addition, however, you can also choose to run the isValid method. This option is possible because isValid is a class method and not an instance method. Consequently, you do not need to create an object before you can execute the isValid method.

2. Run the isValid method with the string value "859-858-3511":

Run isValid Method

3. Confirm that the isValid method returns a value of true:

Result is true

4. Invoke the isValid method with some other string parameters to convince yourself that the method does what it is supposed to do.

5. Right-click on the PhoneNumber class icon and invoke the constructor. Enter the phone number "859-858-3511" as the parameter to the constructor:

Invoke Constructor

6. Right-click on the phoneNum1 object on the work bench and confirm that the isValid method is not listed among the instance methods:

Instance Methods

7. Right-click on the PhoneNumber class icon and run the isValid method. This time, determine whether the phone number currently stored in the phoneNum1 object is valid:

Run isValid Again

If you entered the phone number correctly when you constructed the phoneNum1 object, the isValid method should return true.

Submitting Your Work

Submit your PhoneNumber.java file as an attachment to an email message whose subject is "Hmwk07".