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

Specification: Write a class TelephoneNumber that will hold a telephonenumber. A

ID: 3683169 • Letter: S

Question

Specification: Write a class TelephoneNumber that will hold a telephonenumber. 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: TelephoneNumbert)- A constructor that takes a String of the form xxx-xxx-xxxx OR xxx-xxxx if the area code is missing Throw an exception it the input format is not valid. toString - returns a string m cither 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

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class TelephoneNumber
{
    //class Attributes
   
    //a three digit integer
    int areaCode;
    //a three digit integer
    int exchangeCode;
    //a four digit integer
    int number;
   
    //class methods
    public TelephoneNumber()
    {}
    public TelephoneNumber(String phoneNumber) throws IOException
    {  
        String a[]=phoneNumber.split(" ");
        if(a.length==2)
        {
            if(a[0].length()!=3 || a[1].length()!=4)
                throw new IOException("Input Format not Found");
            else
            {
                areaCode=0;
                exchangeCode=Integer.parseInt(a[0]);
                number=Integer.parseInt(a[1]);
            }
        }
        else if(a.length==3)
        {
            if(a[0].length()!=3 || a[1].length()!=1 || a[2].length()!=4)
            {
               // throw new IOException("Input Format not Found");
                System.out.println("IOException: "+"Input Format not Found");
            }
               
            else
            {
                areaCode=Integer.parseInt(a[0]);
                exchangeCode=Integer.parseInt(a[1]);
                number=Integer.parseInt(a[2]);
            }
        }
    }
   
    @Override
    public String toString()
    {
        String n="";
        if(areaCode!=0)
            n=areaCode+" ";
        n+=exchangeCode+" "+number;
        return n;
    }
}
public class TelephoneReader {

    public static void main(String[] args){
        TelephoneNumber numbers[]=new TelephoneNumber[50];
        int n=0;
        try{
        FileReader freader=new FileReader("directory.txt");
        String s;
            try (BufferedReader inputFile = new BufferedReader(freader)) {
                s=inputFile.readLine();
                while(!s.equals(""))
                {
                   System.out.println(s);
                    numbers[n++]=new TelephoneNumber(s);
                    s=inputFile.readLine();
                }  
            }
        System.out.println(" List of phone Numbers");
        for(int i=0;i<n;i++)
            System.out.println(numbers[i].toString());
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    }
}
   

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