62 558
Assignments Done
98,8%
Successfully Done
In June 2018

Answer to Question #8881 in Java | JSP | JSF for sam mathews

Question #8881
Write a class TelephoneNumber that will hold a telephone number. An object of this class will have attributes .. areaCode—a three-digit integer,exchangeCode—a three-digit integer,number—a four-digit int
and the methods
1.TelephoneNumber(aString)—a constructor that creates and returns a new instance of its class xxx–xxx–xxxx or,if the area code is missing, xxx–xxxx. Throw an exception if the format is not valid Hint hyphen in the telephone number with a blank. To accept a telephone number containing hyphens, you could process the string one character at a time or learn how to use Scanner to read words separated by a character—such as a hyphen—other than whitespace.)
2 .toString—returns a string in either of the two formats shown previously for the contructor.ae
using a text editor,create a text file of several telephone numbers,using the two formats described previously.Write a program that reads this file,displays the data on the screen,creates an array whose base type is Telephone Number.allow the user to
Expert's answer
/*
& * To change this template, choose Tools | Templates
& * and open the template in the editor.
& */

/**
& *
& *
& */
public class TelephoneNumber {
private String areaCode;
private String exchangeCode;
private String number;


/*
& * To change this template, choose Tools | Templates
& * and open the template in the editor.
& */

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
& *
& *
& */
public class TestProblem {

public static void main(String[] args) throws IOException{
& File file = new File("./Test.txt");
& String str;
& try {
BufferedReader br = new BufferedReader(new FileReader(file));
while((str = br.readLine())!= null){
TelephoneNumber tn = new TelephoneNumber(str);
System.out.println(tn);
}

& } catch (FileNotFoundException ex) {
Logger.getLogger(TestProblem.class.getName()).log(Level.SEVERE, null, ex);
& }
}
}


TelephoneNumber(String num){
& String str[] = num.split("-");
& if(str.length == 2){
exchangeCode = str[0];
number = str[1];
& }else
if(str.length == 3){
areaCode = str[0];
exchangeCode = str[1];
number = str[2];
}
&
}

&


@Override
public String toString(){
& if(areaCode == null)
return exchangeCode + "-" + number;
& else
return areaCode + "-" + exchangeCode + "-" + number;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions