Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 2 Download the file PhoneTest.java. Its purpose is to create an ordered list of phone numbers from a file of names and phone numbers

Exercise 2

Download the file PhoneTest.java. Its purpose is to create an ordered list of phone numbers from a file of names and phone numbers which you will supply (see step 3 of this exercise), and then print that list.

Download the file InStringFile.java that is used for the file I/O.

Create a file of at least 5 name and phone number pairs, such that each name is on a separate line and each phone number is on a separate line. For example, your phone list might look like this:

Gates, Bill 888-888-8888 Hopper, Grace 123-456-7890 

The main method of PhoneTest already contains code to input a filename and to read a name/phone number pair from that file. You will add code in the appropriate places to:

Create a new ArrayOrderedList of Phone objects.

Store each name/phone number pair from your file in a Phone object.

Add each new Phone object to the ArrayOrderedList, so the Phone objects are sorted in increasing order by their name attributes.

When all of the information in the phone list file has been added to the ArrayOrderedList, print the list of phone numbers ordered by name.

Run your completed PhoneTest with your file of phone numbers. The output should be your phone list in alphabetical order by names.

Exercise 3

You will now change the ordering of your phone list so that it is ordered by phone numbers rather than by names:

Change the compareTo method of the Phone class such that Phone entries will be ordered by phone number.

Run your PhoneTest with your changed Phone class. The output should be your phone list in order by phone numbers.

Phone.java.

/** Phone class * represents a phone listing * @author CS1027 for Lab */ public class Phone{ private String name; private String phone; public Phone(){ name = ""; phone = ""; } public Phone(String name, String phone){ this.name = name; this.phone = phone; } public String getName(){ return name; } public String getPhone(){ return phone; } public void setName(String name){ this.name = name; } public void setPhone(String phone){ this.phone = phone; } public String toString(){ return (name + " " + phone); } public boolean equals(Phone other) { return (name == other.name)&&(phone == other.phone); } }

PhoneTest.java

import java.io.*; /** * PhoneTest.java: * This class creates an Ordered List of Phone objects. * @author CS1027 for Lab */ public class PhoneTest { public static void main(String[] args) throws Exception { // get the filename from the user BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in),1); System.out.println("Enter name of the input file: "); String filename= keyboard.readLine(); // create object that controls file reading and opens the file InStringFile reader = new InStringFile(filename); System.out.println(" Reading from file " + filename + " "); // your code to create (empty) ordered list here // read data from file two lines at a time (name and phone number) String name, phone; do { name = (reader.read()); phone = (reader.read()); // your code to add the entry to your ordered list here } while (!reader.endOfFile()); System.out.println("Here is my phone book:"); // your code to print the ordered list here // close file reader.close(); System.out.println(" File " + filename + " is closed."); } }

InStringFile.java

import java.io.*; /** * InStringFile makes file reading simpler. It allows * information to be read one line at a time from a * data file, as a String. * @author CS1027 */ public class InStringFile { /** * the handle to read in the file */ private BufferedReader in; /** * the next line of the file to be read */ private String nextLine; /** * Constructs the object that controls file reading. * Exits gracefully if file not found or file cannot be read. * @param filename the name of the file to be read */ public InStringFile(String filename) { try { in = new BufferedReader(new FileReader(filename)); nextLine = in.readLine(); } catch (FileNotFoundException ee){ System.out.println("File " + filename + " not found."); System.exit(0); } catch (IOException e){ System.out.println("File " + filename + " cannot be read."); System.exit(0); } } /** * Reads the next line of input as a String. * Exits gracefully if an error occurs while reading the file. * @return the next line from the input file */ public String read() { String current = nextLine; try { nextLine = in.readLine(); } catch (IOException e){ System.out.println("File cannot be read."); System.exit(0); } return current; } /** * Lookahead test for end of input. * @return true if end of file has been reached */ public boolean endOfFile() { return (nextLine == null); } /** * Closes the file (making it inaccessible though this InStringFile). */ public void close(){ try { in.close(); in = null; } catch (IOException e){ System.out.println("Problem closing file."); System.exit(0); } } }

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions