Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code should be written in Java. Follow the instructions carefully, please. Thank you. You should have one instance variable: An array phoneBookEntry type PhonebookEntry.

This code should be written in Java. Follow the instructions carefully, please. Thank you.

You should have one instance variable: An array phoneBookEntry type PhonebookEntry.

You should be reading in the entries using the read method of your PhonebookEntry class (which in turn uses the read methods of the Name and PhoneNumber classes).

Use the equals methods of the Name and PhoneNumber classes in your lookup and reverseLookup methods.

Use the toString methods to print out information.

Make 100 the capacity of your Phonebook array

Throw an exception (of class Exception) if the capacity of the Phonebook array is exceeded.

Place a try/catch around your entire main and catch both FileNotFoundExceptions and Exceptions (remember, the order of appearance of the exception types in the catch blocks can make a difference).

The name of your application class should be Phonebook. Also, you should submit ALL your classes (i.e., Name, Strip off public from all your classdefinintions

Sample Run #1

For example, if the file phonebook.text contains:

Arnow David (123)456-7890 Harrow Keith (234)567-8901 Jones Jackie (345)678-9012 Augenstein Moshe (456)789-0123 Sokol Dina (567)890-1234 Tenenbaum Aaron (678)901-2345 Weiss Gerald (789)012-3456 Cox Jim (890)123-4567 Langsam Yedidyah (901)234-5678 Thurm Joseph (012)345-6789 

Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:

lookup, reverse-lookup, quit (l/r/q)? l last name? Arnow first name? David David Arnow's phone number is (123)456-7890 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? (456)789-0123 (456)789-0123 belongs to Moshe Augenstein lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Jerrold -- Name not found lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Gerald Gerald Weiss's phone number is (789)012-3456 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? (111)123-4567 -- Phone number not found lookup, reverse-lookup, quit (l/r/q)? q 3 lookups performed 2 reverse lookups performed

Sample Run #2

If the file phonebook.text contains:

   more than 100 names    

Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:

*** Exception *** Phonebook capacity exceeded - increase size of underlying array

Sample Run #3

If the file phonebook.text is missing:

Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:

*** IOException *** phonebook.text (No such file or directory)

Name Class:

import java.io.*; import java.util.*;

class Name {

public Name(String last, String first) {

this.last = last;

this.first = first;

}

public Name(String first) {this("", first);}

public String getFormal() {return first + " " + last;}

public String getOfficial() {return last + ", " + first;}

public boolean equals(Name other) {return first.equals(other.first) && last .equals(other.last);}

public String toString() {return first + " " + last;}

public static Name read(Scanner scanner) {

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

String last = scanner.next();

String first = scanner.next();

return new Name(last, first);

}

public String getInitials(){

/*getinitials method is created and it returns a string consisting of first and last name initials followed by "."*/

String intials = this.first.charAt(0)+"."+this.last.charAt(0)+".";

return intials;

}

private String first, last;

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

Scanner scanner = new Scanner(new File("names.text"));

int count = 0;

Name name = read(scanner);

while(name != null) {

System.out.println("name: " + name.toString());

System.out.println("formal: " + name.getFormal());

System.out.println("official: " + name.getOfficial());

System.out.println("initials: "+name.getInitials()+" ");

count++;

Name nextname = read(scanner);//read the name from file into a new object named nextname.

if(nextname != null)//if it is not null, execution enters to check while.

while(name.equals(nextname) && nextname != null){

/* if name equals to nextname and nextname not equal to null the loop executes*/

System.out.println("Duplicate name \""+name.first+" "+name.last+"\" discovered");

name = nextname;//assigns nextname to name

nextname = read(scanner);//reads name from file into nextname.

count++;//count is incremented by 1.

}

name = nextname;//name is assigned with nextname

}

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

System.out.println(count + " names processed.");

}

}

Phonenumber Class:

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

class PhoneNumber { private String number;

public PhoneNumber(String number) { this.number = number; }

public String getAreaCode() { return number.substring(1, 4); }

public String getExcahnge() { return number.substring(5, 8);

}

public String getLineNumber() { return number.substring(9, 13);

}

public boolean isTollFree() { if(number.charAt(1)=='8'){ return true; } return false; }

public String toString() { return this.number; }

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

public static PhoneNumber read(Scanner sc) { if (!sc.hasNext()) return null; String number = sc.next(); return new PhoneNumber(number); }

public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new File("numbers.text"));

int count = 0;

PhoneNumber number = read(sc); while (number != null) { PhoneNumber prev = number; System.out.println("phone number: " + number.toString()); System.out.println("area code: " + number.getAreaCode()); System.out.println("exchange: " + number.getExcahnge()); System.out.println("line number: " + number.getLineNumber()); System.out.println("is toll free: " + number.isTollFree());

System.out.println(); count++; PhoneNumber nextname = read(sc);//read the name from file into a new object named nextname.

if(nextname != null)//if it is not null, execution enters to check while.

while(number.equals(nextname) && nextname != null){

/* if name equals to nextname and nextname not equal to null the loop executes*/

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

number = nextname;//assigns nextname to name

nextname = read(sc);//reads name from file into nextname.

count++;//count is incremented by 1.

}

number = nextname;//name is assigned with nextname

} System.out.println("---"); System.out.println(count + " phone numbers processed."); } }

Phone Entry Class:

import java.io.*;

import java.util.Scanner;

class PhonebookEntry {

private

Name name;

PhoneNumber phoneNumber;

PhonebookEntry(Name name,PhoneNumber phNo){

this.name = name;

this.phoneNumber = phNo;

}

public static PhonebookEntry read(Scanner sc) {

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

String last = sc.next();

String first = sc.next();

String number = sc.next();

Name name = new Name(last,first);

PhoneNumber phNo= new PhoneNumber(number);

return new PhonebookEntry(name,phNo);

}

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

Scanner sc = new Scanner(new File("phonebook.text"));

int count = 0;

PhonebookEntry entry = read(sc);

while (entry != null) {

System.out.print("phone book entry: ");

System.out.println(entry.name.toString()+": "+entry.phoneNumber.toString());

if(entry.phoneNumber.isTollFree()==true) { System.out.println("Dialing (toll-free) " + entry.name.toString() + ": " + entry.phoneNumber.toString()); }else { System.out.println("Dialing " + entry.name.toString() + ": " + entry.phoneNumber.toString()); } System.out.println();

count++;

PhonebookEntry nextEntry = read(sc);//read the entry from file into a new object named nextEntry.

if(nextEntry != null)//if it is not null, execution enters to check while.

while((entry.phoneNumber.equals(nextEntry.phoneNumber) || entry.name.equals(nextEntry.name)) && (entry != null)){

/* if current phoneNumber equals to nextEntry's phoneNumber and nextEntry not equal to null the loop executes*/

if(entry.phoneNumber.equals(nextEntry.phoneNumber)){

System.out.println("Duplicate phone book entry \""+nextEntry.name.toString()+": "+nextEntry.phoneNumber.toString()+"\" discovered");

}

else if(entry.name.equals(nextEntry.name)){

System.out.println("Warning duplicate name encountered \""+nextEntry.name.toString()+": "+nextEntry.phoneNumber.toString()+"\" discovered");

System.out.print("phone book entry: ");

System.out.println(nextEntry.name.toString()+": "+nextEntry.phoneNumber.toString());

System.out.println("Dialing "+nextEntry.name.toString()+": "+nextEntry.phoneNumber.toString());

System.out.println();

}

entry = nextEntry;//assigns nextEntry to entry

nextEntry = read(sc);//reads entry from file into nextEntry.

count++;//count is incremented by 1.

}

entry = nextEntry;//entry is assigned with nextEntry

}

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

System.out.println(count + " phonebook entries 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

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

Am I just skimming over the problem?

Answered: 1 week ago

Question

Current Attempt in Progress Determine the missing amounts.

Answered: 1 week ago