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."); } } 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 toString() { return name+": "+phoneNumber; } public boolean equals(PhonebookEntry otherEntry) { if(name.equals(otherEntry.name) && !(phoneNumber.equals(otherEntry.phoneNumber)) ) { System.out.println("Warning duplicate name encountered \""+ name+": "+phoneNumber+"\" discovered"); return true; } else if(phoneNumber.equals(otherEntry.phoneNumber) && name.equals(otherEntry.name)) { System.out.println("Duplicate phone book entry \""+otherEntry+"\" discovered"); return false; } return true; } public static PhonebookEntry read(Scanner scanner) { if (!scanner.hasNext()) return null; Name newName = Name.read(scanner); PhoneNumber newphoneNumber = PhoneNumber.read(scanner); return new PhonebookEntry(newName, newphoneNumber); } public void call() { System.out.println("phone book entry: "+name+": "+phoneNumber); if(phoneNumber.isTollFree()) { System.out.println("Dialing (toll-free) "+name+": "+phoneNumber); System.out.println(); } else { System.out.println("Dialing "+name+": "+phoneNumber); System.out.println(""); } } public static void main(String[] args) throws Exception { String tempFirst="temp", tempLast="temp", tempNumber="temp"; PhoneNumber oldNumber= new PhoneNumber(tempNumber); ....not finished written here due to no space. Please follow intructions on the picture on writting phonebook class.
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.");
}
}
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 toString()
{
return name+": "+phoneNumber;
}
public boolean equals(PhonebookEntry otherEntry)
{
if(name.equals(otherEntry.name) && !(phoneNumber.equals(otherEntry.phoneNumber)) )
{
System.out.println("Warning duplicate name encountered \""+ name+": "+phoneNumber+"\" discovered");
return true;
}
else if(phoneNumber.equals(otherEntry.phoneNumber) && name.equals(otherEntry.name))
{
System.out.println("Duplicate phone book entry \""+otherEntry+"\" discovered");
return false;
}
return true;
}
public static PhonebookEntry read(Scanner scanner)
{
if (!scanner.hasNext()) return null;
Name newName = Name.read(scanner);
PhoneNumber newphoneNumber = PhoneNumber.read(scanner);
return new PhonebookEntry(newName, newphoneNumber);
}
public void call()
{
System.out.println("phone book entry: "+name+": "+phoneNumber);
if(phoneNumber.isTollFree())
{
System.out.println("Dialing (toll-free) "+name+": "+phoneNumber);
System.out.println();
}
else
{
System.out.println("Dialing "+name+": "+phoneNumber);
System.out.println("");
}
}
public static void main(String[] args) throws Exception
{
String tempFirst="temp", tempLast="temp", tempNumber="temp";
PhoneNumber oldNumber= new PhoneNumber(tempNumber);
....not finished written here due to no space. Please follow intructions on the picture on writting phonebook class.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps