Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the TelephoneNumber class described in Exercise 6 so that it is serializa

ID: 666500 • Letter: M

Question

Modify the TelephoneNumber class described in Exercise 6 so that it is serializable. Write a program that creates an array whose base type is Tele phoneNumber by reading data from the keyboard. Write the array to a binary file using the method writeObject. Then read the data from the file using the method readObject and display the information to the screen. Allow the user to change, add, or delete any telephone number until he or she indicates that all changes are complete. Then write the modified telephone numbers to the file, replacing its original contents.

Exercise 6 is Write a class TelephoneNumber that will hold a telephone number. An object of this class will have the attributes • areaCode—a three-digit integer • exchangeCode—a three-digit integer • number—a four-digit integer and the methods • TelephoneNumber(aString)—a constructor that creates and returns a new instance of its class, given a string in the form xxx–xxx–xxxx or, if the area code is missing, xxx–xxxx. Throw an exception if the format is not valid. (Hint: To simplify the constructor, you can replace each 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.) toString—returns a string in either of the two formats shown previously for the constructor. Using a text editor, create a text file of several telephone numbers, using the two formats described previously. Write a program thatreads this file, displays the data on the screen, and creates an array whose base type is TelephoneNumber. Allow the user to either add or
delete one telephone number. Write the modified data on the text file, replacing its original contents. Then read and display the numbers inthe modified file.

Explanation / Answer

// I am presenting the above program in java, as you didn't mention which programming language to use.

Program:


package telephoneex;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;


public class TelephoneEX {

public static void main(String[] args) throws IOException {
// TODO code application logic here
TelephoneNumber Tele[]=new TelephoneNumber[30];
System.out.println("Enter the total number of entries:");
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter Number:" +(i+1));
Tele[i]=new TelephoneNumber(sc.next());
}
  
//output the object array in to a file
try
{
FileOutputStream fileOut =
new FileOutputStream("output.bin");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(int i=0;i<n;i++)
{
  
out.writeObject(Tele[i]);
// out.writeObject(Tele[i].toString()+" ");
}
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in File output.bin");
}catch(IOException i)
{
i.printStackTrace();
}
//read the data from the file using the method readObject and display the information
//to the screen.
TelephoneNumber Telenum[] = new TelephoneNumber[30];
int tot=0;
try
{
FileInputStream fileIn = new FileInputStream("output.bin");
ObjectInputStream in = new ObjectInputStream(fileIn);

System.out.println(" Telephone numbers");
Object obj = null;
while ((obj = in.readObject()) != null) {
  
if (obj instanceof TelephoneNumber) {
System.out.println(((TelephoneNumber)obj).toString());
Telenum[tot++]=new TelephoneNumber(((TelephoneNumber)obj).toString());

}
}
  
in.close();
fileIn.close();
} catch (EOFException ex) { //This exception will be caught when EOF is reached
System.out.println("End of file reached.");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
// Allow the user to change, add, or delete any telephone number until he or
//she indicates that all changes are complete. Then write the modified telephone numbers
//to the file, replacing its original contents.
int ch;
String choice;
do
{
ch=0;
System.out.println("Add, Delete, or Modify? (A/D/M):");
choice=sc.next();
if(choice.equalsIgnoreCase("A"))
{
ch=1;
System.out.println("Enter Number to Add in file:");
tot+=1;
Telenum[tot]=new TelephoneNumber(sc.next());
System.out.println("new number added");
}
else if(choice.equalsIgnoreCase("D"))
{
ch=1;
System.out.println("Enter Number to delete from file:");
String d=sc.next();
for(int j=0;j<tot;j++)
{
if((Telenum[j].toString()).equals(d))
{
Telenum[j]=new TelephoneNumber("xxx-xxx-xxxx");
System.out.println("number deleted:");
break;
}   
}
  
}
else if (choice.equalsIgnoreCase("M"))
{
ch=1;
System.out.println("Enter Number to Modify :");
String d=sc.next();
for(int j=0;j<tot;j++)
{
if((Telenum[j].toString()).equals(d))
{
System.out.println("Enter new number :");
Telenum[tot]=new TelephoneNumber(sc.next());
System.out.println("number modified:");
break;
}
}
}
System.out.println("All the changes are completed??? (1/0)");
ch=sc.nextInt();   
if(ch==1)
{
try{
FileOutputStream fileOut =
new FileOutputStream("output.bin");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(int i=0;i<n;i++)
{
  
out.writeObject(Tele[i]);
// out.writeObject(Tele[i].toString()+" ");
}
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in File output.bin");
}catch(IOException i)
{
i.printStackTrace();
}
}
}while(ch==0);

}
  
}

class TelephoneNumber implements java.io.Serializable{

public int areaCode;// 3 digit
public int exchangeCode;// 3 digit
public int number;//4 digit
public TelephoneNumber(String T)
{
//xxx-xxx-xxxx
if(T.length()==12)
{
if(T.charAt(3)=='-' && T.charAt(7)=='-')
{
areaCode=Integer.parseInt(T.substring(0, 3));
exchangeCode=Integer.parseInt(T.substring(4, 7));
number=Integer.parseInt(T.substring(8, T.length()));
}
}
else
{
System.out.println("Telephone number format is not valid");
}
}
//toString—returns a string in either of the two formats shown previously for the constructor.
@Override
public String toString()
{
String ss=this.areaCode+"-"+this.exchangeCode+"-"+this.number;
return(ss);
}
}

//Output :

run:
Enter the total number of entries:
3
Enter Number:1
123-123-4567
Enter Number:2
345-567-1235
Enter Number:3
123-123-4567
Serialized data is saved in File output.bin
Telephone numbers
123-123-4567
345-567-1235
123-123-4567
End of file reached.
Add, Delete, or Modify? (A/D/M):
A
Enter Number to Add in file:
345-678-8900
new number added
All the changes are completed??? (1/0)
0
Add, Delete, or Modify? (A/D/M):
D
Enter Number to delete from file:
123-123-4567
number deleted:
All the changes are completed??? (1/0)
0
Add, Delete, or Modify? (A/D/M):
M
Enter Number to Modify :
123-123-4567
Enter new number :
999-999-9999
number modified:
All the changes are completed??? (1/0)
1
BUILD SUCCESSFUL (total time: 1 minute 52 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote