Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the Name class of Lab 2.1, code a PhoneNumber class and its associated main. The class should have the following state: A single string,

Using the Name class of Lab 2.1, code a PhoneNumber class and its associated main. The class should have the following state:

A single string, number

and the following behavior:

a constructor that accepts a string representing the phone number (you can assume until informed otherwise that the phone number is always in the proper format of (nnn)nnn-nnnn.

methods getAreaCode, getExchange, and getLineNumber that return the first three digits, the moddle three digits, and the last four digits respec. of the number

a method, isToll-Free that returns true if the number is a toll-free phone number (i.e., the area code begins with an '8').

toString, equals, and read methods that correspond to their counterparts in the Name class.

A main method that performs similar logic to that of the Name class, i.e.,

opens the file numbers.text

reads in PhoneNumber objects, using the read method of the class (until null is returned).

prints out the PhoneNUmber object (using toString)

prints out whether the number is toll-free

checks for adjacent duplicate numbers

keeps count of the number of PhoneNumbers read in

The name of your class should be PhoneNumber. Please remove the public attribute from your class definition (CodeLab restriction).

For example, if the file numbers.text contains:

(356)153-9460 (290)261-7625 (761)321-9457 (724)091-7819 (759)301-3133 (072)023-0203 (060)075-3782 (060)075-3782 (369)348-9660 (844)824-5853 (692)839-8466 (216)386-2922 

the program should produce the following output:

phone number: (356)153-9460 area code: 356 exchange: 153 line number: 9460 is toll free: false phone number: (290)261-7625 area code: 290 exchange: 261 line number: 7625 is toll free: false phone number: (761)321-9457 area code: 761 exchange: 321 line number: 9457 is toll free: false phone number: (724)091-7819 area code: 724 exchange: 091 line number: 7819 is toll free: false phone number: (759)301-3133 area code: 759 exchange: 301 line number: 3133 is toll free: false phone number: (072)023-0203 area code: 072 exchange: 023 line number: 0203 is toll free: false phone number: (060)075-3782 area code: 060 exchange: 075 line number: 3782 is toll free: false Duplicate phone number "(060)075-3782" discovered phone number: (369)348-9660 area code: 369 exchange: 348 line number: 9660 is toll free: false phone number: (844)824-5853 area code: 844 exchange: 824 line number: 5853 is toll free: true phone number: (692)839-8466 area code: 692 exchange: 839 line number: 8466 is toll free: false phone number: (216)386-2922 area code: 216 exchange: 386 line number: 2922 is toll free: false --- 12 phone numbers processed.

This is my code:

package phonenumberclass;

import java.io.*;

import java.util.*;

class PhoneNumber

{

private String number;

public PhoneNumber(String phoneNumber)

{

this.number = phoneNumber;

}

public String getAreaCode()

{

if(!this.number.isEmpty())

{

return this.number.substring(1, 4);

}

return null;

}

public String getExchange()

{

if(!this.number.isEmpty())

{

return this.number.substring(5,8);

}

return null;

}

public String getLineNumber()

{

if(!this.number.isEmpty())

{

return this.number.substring(9,13);

}

return null;

}

public Boolean isToll-Free()

{

if(!this.number.isEmpty())

{

if (Integer.parseInt(this.number.substring(1,2))==8)

{

return true;

}

else return false;

}

return null;

}

public boolean equals(PhoneNumber other) {return number.equals(other);}

public String toString(){

return number;

}

public static PhoneNumber read(Scanner scanner) {

if (!scanner.hasNext()) return null;

String num = scanner.next();

return new PhoneNumber(num);

}

public static void main(String[] args) throws Exception {

int i=0;

Scanner scanner = new Scanner(new File("C:\\Users\\M16\\Desktop\ umbers.txt"));

int count = 0;

PhoneNumber num = read(scanner);

HashSet set = new HashSet();

/* while(num!=null){

i++;

System.out.println("phone number: "+num);

System.out.println("area code: "+num.getAreaCode());

System.out.println("exchange: "+num.getExchange());

System.out.println("line number: "+num.getLineNumber());

System.out.println("is toll free: " +num.isToll-Free());

System.out.println("");

num = read(scanner);

}*/

while (num != null) {

// if set does not contain the number than only we are printing

if(!set.contains(num)){

set.add(phoneNumber);

//set.add(num.getFormal());

System.out.println("phone number: " + num);

System.out.println("area code: " + num.getAreaCode());

System.out.println("exchange: " + num.getExchange());

System.out.println("line number: " + num.getLineNumber());

System.out.println("is toll free: " + num.isToll-Free());

System.out.println();

count++;

}

//if it contains than we are printing the required message

else{

System.out.println("Duplicate number \""+num+"\" discovered");

count++;

}}

num = read(scanner);

System.out.println("---");

System.out.println(+i+ " phone numbers processed.");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

When does a change in reporting entity occur?

Answered: 1 week ago