Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

please use the name, phonenumber and phonebookentry to build this.

The three parallel arrays are gone — replaced with a single array of 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).

Do not use

BufferedReader while(true)

breaks

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 class definintions

name class:

import java.io.*;
import java.util.*;
public class Name {
public Name(String last, String first) {
this.last = last;
this.first = first;
}
public Name(String first) {
this("", first);
}
public boolean equals(Name same) {
return first.equals(same.first) && last .equals(same.last);

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

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

public static Name read(Scanner scan) {
if (!scan.hasNext())
return null;
String first = scan.next();
String last = scan.next();
return new Name(first, last);
}
public String getInitials() {
return Character.toUpperCase(first.charAt(0)) + "." + Character.toUpperCase(last.charAt(0))+".";
}
private String last, first;
public static void main(String [] args) throws Exception {
Scanner scan = new Scanner(new File("names.text"));
int count = 0;
Name readName = read(scan);
Name readLastName =null;
while(readName != null) {
if(readLastName!=null && readName!=null && readLastName.equals(readName)){
System.out.println("Duplicate name \""+readName+"\" discovered");
}
else{
System.out.println("name: " + readName);
System.out.println("formal: " + readName.getFormal());
System.out.println("official: " + readName.getOfficial());
System.out.println("initials: " + readName.getInitials());
System.out.println();
}
count++;
readLastName= readName;
readName = read(scan);
}
System.out.println("---");
System.out.println(count + " names processed.");
}
}

Phonenumber class:

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

public class PhoneNumber {

private String num;

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

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

public String getExchange() {
return num.substring(5, 8);
}

public String getLineNumber() {
return num.substring(9);
}

public boolean isFreeToll() {
if (getAreaCode().charAt(0) == '8') {
return true;
}
return false;
}

public String toString() {
return "phone number: " + num + "\narea code: " + getAreaCode()
+ "\nexchange: " + getExchange() + "\nline number: "
+ getLineNumber();
}

public boolean equals(PhoneNumber phoneNum) {
if (phoneNum.getAreaCode().compareTo(this.getAreaCode()) == 0
&& phoneNum.getExchange().compareTo(this.getExchange()) == 0
&& phoneNum.getLineNumber().compareTo(this.getLineNumber()) == 0) {
return true;
}
return false;
}

public static String read(Scanner scan) {
if (scan.hasNextLine()) {
return scan.nextLine();
}
return null;
}

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

Scanner scan = new Scanner(new File("numbers.text"));

PhoneNumber phoneNumNext, phoneNum = null;

int counts = 0;

String val = PhoneNumber.read(scan);

while (val != null) {

if (phoneNum == null) {
phoneNum = new PhoneNumber(val);

System.out.println(phoneNum + "\nis toll free: "
+ phoneNum.isFreeToll() + "\n");
counts++;
} else {

phoneNumNext = new PhoneNumber(val);


if (phoneNum.equals(phoneNumNext)) {

System.out.println("Duplicate phone number \"("
+ phoneNumNext.getAreaCode() + ")"
+ phoneNumNext.getExchange() + "-"
+ phoneNumNext.getLineNumber() + "\" discovered");

} else {

System.out.println(phoneNumNext + "\nis toll free: "
+ phoneNumNext.isFreeToll() + "\n");
}

counts++;

phoneNum = phoneNumNext;
}

val = PhoneNumber.read(scan);
}
System.out.println("---");
System.out.println(counts + " phone numbers processed.");
}
}

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

[画像:phonebookentry class: import java.util.": import java.io.": class PhonebookEntry { private PhoneNumber phoneNumber: private Name name; public PhonebookEntry(Name name, PhoneNumber number) { this.phoneNumber= number: this.name=name: public String toString0 return name+": "+phoneNumber; public boolean equals(PhonebookEntry otherEntry) if(name.equals(otherEntry.name) && (phoneNumber.equals(otherEntry.phoneNumber) System.out.printin("Warning duplicate name encountered \"+ name-": "+phoneNumber-"" discovered"): return true; } else iffphoneNumber.equals(otherEntry.phoneNumber) && name.equals(otherEntry.name) System.out.printin("Duplicate phone book entry \"+otherEntry-"\" discovered"): return false; return true; } public static PhonebookEntry read(Scanner scanner) { if (Iscanner.hasNext() return null; Name newName = Name.read(scanner): PhoneNumber newphoneNumber = PhoneNumber.read(scanner): return new PhonebookEntry(newName, newphoneNumber): } public void call) System.out.printin("phone book entry: *+name+": "+phoneNumber): if(phoneNumber.isTollFree) ]
expand button
Transcribed Image Text:phonebookentry class: import java.util.": import java.io.": class PhonebookEntry { private PhoneNumber phoneNumber: private Name name; public PhonebookEntry(Name name, PhoneNumber number) { this.phoneNumber= number: this.name=name: public String toString0 return name+": "+phoneNumber; public boolean equals(PhonebookEntry otherEntry) if(name.equals(otherEntry.name) && (phoneNumber.equals(otherEntry.phoneNumber) System.out.printin("Warning duplicate name encountered \"+ name-": "+phoneNumber-"" discovered"): return true; } else iffphoneNumber.equals(otherEntry.phoneNumber) && name.equals(otherEntry.name) System.out.printin("Duplicate phone book entry \"+otherEntry-"\" discovered"): return false; return true; } public static PhonebookEntry read(Scanner scanner) { if (Iscanner.hasNext() return null; Name newName = Name.read(scanner): PhoneNumber newphoneNumber = PhoneNumber.read(scanner): return new PhonebookEntry(newName, newphoneNumber): } public void call) System.out.printin("phone book entry: *+name+": "+phoneNumber): if(phoneNumber.isTollFree)
[画像:System.out.printin("phone book entry: "+name+": "+phoneNumber): if(phoneNumber.isTollFree() { System.out.printin("Dialing (toll-free) "+name+": "+phoneNumber): System.out.printin): else { System.out.printin("Dialing "+name+": "+phoneNumber): System.out.printin("): } } public static void main(String] args) throws Exception { String tempFirst="temp", templast="temp", tempNumber="temp": PhoneNumber oldNumber= new PhoneNumber(tempNumber): Name oldName= new Name(templast,tempFirst): PhonebookEntry temp= new PhonebookEntry(oldName, oldNumber): int count=0; Scanner scanner = new Scanner(new File("phonebook.text")): PhonebookEntry entry=read(scanner): while(entry!=nul) if(entry.equals(temp) { entry.call): count++; else count++; oldName=entry.name; oldNumber=entry.phoneNumber: temp= new PhonebookEntry(oldName,oldNumber): entry=read(scanner): } System.out.printin("--): System.out.printin(count+" phonebook entries processed."): } } ]
expand button
Transcribed Image Text:System.out.printin("phone book entry: "+name+": "+phoneNumber): if(phoneNumber.isTollFree() { System.out.printin("Dialing (toll-free) "+name+": "+phoneNumber): System.out.printin): else { System.out.printin("Dialing "+name+": "+phoneNumber): System.out.printin("): } } public static void main(String] args) throws Exception { String tempFirst="temp", templast="temp", tempNumber="temp": PhoneNumber oldNumber= new PhoneNumber(tempNumber): Name oldName= new Name(templast,tempFirst): PhonebookEntry temp= new PhonebookEntry(oldName, oldNumber): int count=0; Scanner scanner = new Scanner(new File("phonebook.text")): PhonebookEntry entry=read(scanner): while(entry!=nul) if(entry.equals(temp) { entry.call): count++; else count++; oldName=entry.name; oldNumber=entry.phoneNumber: temp= new PhonebookEntry(oldName,oldNumber): entry=read(scanner): } System.out.printin("--): System.out.printin(count+" phonebook entries processed."): } }
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education