Create a class to deal with telephone numbers that your customers enter. User’s
ID: 3688917 • Letter: C
Question
Create a class to deal with telephone numbers that your customers enter. User’s aren’t very disciplined, so even if you indicate that you want the number entered like this:
407 582 2213
They sometimes enter the number like any of these:
(407)582-2213
407.582.2213
4075822213
Or other combinations that I can’t think of, but they will.
The constructor for your class takes a String. If the number they pass to your class does not have 10 digits, (or 11 digits if it starts with a 1), you should throw an Exception. InvalidTelephoneException. It’s the constructor method that will throw this exception.
A test of your class can be though of as: try to make a phone number out of a String.
Telephone phone;
String testString = “407.582.2213”;
try {
phone = new Telephone(testString);
}catch(InvalidTelephoneException ite) {
Send some information to the console about why that string is not a phone number
}
Your class must have a getAreaCode, getExchange, getLocalNumber methods. For the example above, the area code is 407, the exchange is 582, and the local number is 2213.
Follow good practices for OO programming.
A toString method will present the number in a canonical form: (407) 582-2213
Create a test class that demonstrates that your code will work correctly for each of the documented cases above.
Include all of the code, and the output from running your test class.
Use JavaDoc comments to document the class. Include examples IN THE JAVADOC of what format of phone numbers will be acceptable by the class (service provider) that you have created.
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
Include the JavaDoc that is generated by running the JavaDoc tool on your code. Paste in the web page that is created.
There is no end to this one…. The ‘service’ that this class provides will vary in its capabilities. This is a pretty common real-world problem. As soon as you think you are done someone will want it to handle something that you didn’t think of. You are ‘done’ when your code meets the requirements that are documented, and it is demonstrated.
Explanation / Answer
class InvalidTelephoneException extends Exception //create a user defined exception
{
String str = null;
InvalidTelephoneException(String str)
{
this.str = str;
}
public String msg()
{
return str ;
}
}
class ValidPhone
{
ValidPhone(String phno)
{
int len = phno.length();
boolean f = false;
try
{
if(phno.charAt(0) != '1')
{
f = true;
int count = 0;
for(int i=0;i<len;i++)
{
if (Character.isDigit(phno.charAt(i)))
count++;
}
if(count != 10)
throw new InvalidTelephoneException("Illegal Telephone Number. Number should contain 10 digits if number does not begin with 1"); //call user defiend exception
else
{
System.out.println("VALID PHONE NUMBER");
getCanonicalForm(phno, f); //call the method for canonical form
}
}
if(phno.charAt(0) == '1')
{
int count = 0;
f = false;
for(int i=0;i<len;i++)
{
if (Character.isDigit(phno.charAt(i)))
count++;
}
if(count != 11)
throw new InvalidTelephoneException("Illegal Telephone Number. Number should contain 11 digits if number begin with 1");
else
{
System.out.println("VALID PHONE NUMBER");
getCanonicalForm(phno, f); //call the method for canonical form
}
}
}
catch(InvalidTelephoneException e)
{
System.out.println(e.msg());
}
}
void getCanonicalForm(String s, boolean b)
{
char c[] = new char[11];
for(int i=0;i<s.length();i++)
{
if(Character.isDigit(s.charAt(i)))
{
c[i] = s.charAt(i);
}
}
String a_code = "" + c[0] + c[1] + c[2]; //compute the area code
String exchange = "" + c[3] + c[4] + c[5]; //compute the exchange
if(b == true)
String localnumber = "" + c[6] + c[7] + c[8] + c[9]; //compute localnumber
if(b == false)
String localnumber = "" + c[6] + c[7] + c[8] + c[9] + c[10];
System.out.println("THE CANONICAL FORM IS : " + "(" + a_code + ") " + exchange + "-" + localnumber);
}
public static void main(String args[]) throws Exception
{
ValidPhone v = new ValidPhone("123 456 7890");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.