Homework 13

In this assignment, you will modify the PhoneNumber class you wrote back in homework 7. You will add a class method named "getDigit" that accepts a single character (as a char parameter) and returns the corresponding digit on a telephone keypad (as a char value).

Output Requirements

The telephone digit corresponding to a given character.

Input Requirements

A single character.

Processing Requirements

Each of the digits '2' through '9' in a telephone "number" corresponds to three or four letters of the alphabet as shown below. Given a letter of the alphabet (either uppercase or lowercase), the getDigit method should return the corresponding digit. For example, PhoneNumber.getDigit('A') should return '2'. If the character parameter is a digit then getDigit should return that digit. For example, PhoneNumber.getDigit('3') should return '3'. Finally, if the parameter value is neither a letter nor a digit then the getDigit method should return '*'. For example, PhoneNumber.getDigit('?') should return '*'.

Miscellaneous Requirements

You must use a switch statement to determine the digit that corresponds to the parameter value.

Don't forget that the only thing that marks a method as a class method rather than an instance method is the "static" modifier in the method declaration as in:

public static char getDigit(char c)

The toUpperCase Method

Using the toUpperCase method of the Character class simplifies the logic since all you have to worry about are uppercase letters rather than both uppercase and lowercase letters. If ch is a character variable (such as the parameter to the isDigit method), the function


returns the corresponding uppercase value. More specifically, if ch is a lowercase letter of the alphabet then toUpperCase(ch) returns the corresponding uppercase letter. Otherwise, it returns the value of ch unchanged. Here are some examples:

Character.toUpperCase('A') returns 'A'
Character.toUpperCase('d') returns 'D'
Character.toUpperCase('3') returns '3'

Submitting Your Work

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