- Suppose you are implementing the following class
representing 7-digit phone numbers in the form "XXX-XXXX" for a
phone in the immediate OSU area. That is, you may assume the
length of the PhoneNumber value is 8 and that each "X" is
a digit '0'-'9'.
/** * Simple class representing a 7-digit phone number in the form "XXX-XXXX" * for a phone in the immediate OSU area. */ public class PhoneNumber { /** * The phone number representation. */ private String rep; /** * Constructor. {@code pNum} must be in the form "XXX-XXXX" where each * "X" is a digit '0'-'9'. */ public PhoneNumber(String pNum) { this.rep = pNum; } ... @Override public int hashCode() { // TODO - fill in body } ... }
Write the code to implement the hashCode instance method. You can use the Character.digit(char ch, int radix) method to convert a "digit" character ch into the corresponding numeric value in the specified radix.
- Some people have phone numbers such as 292-OHIO. Certain
solutions to the previous problem couldn't necessarily be applied
to the PhoneNumber value "292-OHIO" because it has
non-digits. Furthermore, even if your particular implementation
could be applied to this PhoneNumber value, it almost
certainly would not do "the right thing" in an application program
because the hash function probably would not give the same integer
result as it does for the numeric version of the phone number,
"292-6446".
- Explain exactly what problem this would cause; i.e., explain what problem would arise if "292-OHIO" and "292-6446" were both considered legal phone numbers and your hash function from the previous problem could be applied to both of them, and therefore you actually decided to use that hash function for both of them.
- Explain how you could change the hash function to correct this problem; i.e., explain what the hash function would have to do to handle phone numbers like "292-OHIO" and "292-6446" in a proper way.
- While you're at it, you might as well also handle smoothly the case where the phone number is typed in as "292-ohio". Explain how you could further change the hash function to handle this situation, too.