Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class TelephoneNumber that will hold a telephone number. An object of th

ID: 3682931 • Letter: W

Question

Write a class TelephoneNumber that will hold a telephone number. An object of this class will have the following attributes:

areaCode - a three-digit integer

exchangeCode - a three-digit integer

number - a four-digit integer

This class will also have the following methods:

TelephoneNumber(<String>) - A constructor that takes a String of the form xxx-xxx-xxxx OR xxx-xxxx if the area code is missing. Throw an exception if the input format is not valid.

Hint: To simplify the constructor, you can replace each hyphen in the telephone number with a blank.

toString - returns a string in either of the two formats shown previously for the constructor.

Using a text editor (e.g. notepad), create a text file of several telephone numbers, using the two formats described previously. Write a program TelephoneReader that reads this file, displays the data on the screen.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class TelephoneNumber {

   int areaCode;
   int exchangeCode;
   int number;

   /**
   * @param phoneNumber
   */
   public TelephoneNumber(String phoneNumber) {

       String stringArray[] = phoneNumber.split("-");
       int len = stringArray.length;

       if (len == 3) {
           this.areaCode = Integer.parseInt(stringArray[0]);
           this.exchangeCode = Integer.parseInt(stringArray[1]);
           ;
           this.number = Integer.parseInt(stringArray[2]);
           ;
       } else if (len == 2) {
           this.areaCode = 0;
           this.exchangeCode = Integer.parseInt(stringArray[0]);

           this.number = Integer.parseInt(stringArray[1]);

       } else {
           try {
               throw new Exception("Invalid Format");
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }

       }
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       if (areaCode != 0)
           return areaCode + "-" + exchangeCode + "-" + number;
       else
           return exchangeCode + "-" + number;
   }

}
import java.io.File;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class TelephoneReader {
   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File("phonenums.txt"));
           while (scanner.hasNext()) {
               TelephoneNumber number = new TelephoneNumber(scanner.next());
              
               System.out.println(number);

           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}
phonenums.txt
790-123-4564
125-5856
5877
387-664-3105
890-123-4567
6588

OUTPUT:
790-123-4564
125-5856
0-0
387-664-3105java.lang.Exception: Invalid Format
   at TelephoneNumber.<init>(TelephoneNumber.java:33)
   at TelephoneReader.main(TelephoneReader.java:14)

890-123-4567
0-0
java.lang.Exception: Invalid Format
   at TelephoneNumber.<init>(TelephoneNumber.java:33)
   at TelephoneReader.main(TelephoneReader.java:14)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote