Question
mention algorithms used List data structures used in code. Give a description of how to use the program and expected input/output Explain the purpose of
mention algorithms used
List data structures used in code.
Give a description of how to use the program and expected input/output
Explain the purpose of each class developed in the program.
Programming:
For each method, give the pre and post conditions and invariant, if any
So basically Explain every line or component of the following code and run it:
import java.util.LinkedList; //taken from library files
import java.util.Scanner;
public class phonedir {
String fname; // string operations
String lname;
String phone;
public Phonedir(String fname,String lname, String phone)
{
this.fname = fname;
this.lname = lname;
this.phone = phone;
}
public static void displayMenu()
{
System.out.println("A program to keep a Phone directory");
System.out.println("a Show all records");
System.out.println("d Delete the current record");
System.out.println("f Change the first name in the current record");
System.out.println("l Change the last name in the current record");
System.out.println("n Add a new record");
System.out.println("p Change the phone number in the current record");
System.out.println("q Quit");
System.out.println("s Select a record from the record list to become the current record");
System.out.print("Enter a command from the list above(q to quit) : ");
}
public static void main(String[] args) { public loop starting LinkedList
String choice,lname,fname,phone;
Phonedir currentRecord=null;
Scanner scan = new Scanner(System.in);
do {
displayMenu();
choice = scan.nextLine();
switch(choice)
{ case "A":
case "a":
if(phoneList.size() == 0)
System.out.println("No records present");
else
{
System.out.println(" FirstName \t LastName \t Phone Number");
System.out.println("----------- \t --------- \t-------------");
for(int i=0;i { System.out.println(phoneList.get(i).fname+"\t "+phoneList.get(i).lname+" \t "+phoneList.get(i).phone); } } break; case "D": case "d": if(currentRecord == null) System.out.println(" No Current Record"); else { phoneList.remove(currentRecord); currentRecord = null; } break;//it will take some time case "F": case "f": if(currentRecord == null) System.out.println(" No Current Record"); else { System.out.print(" Enter the new first name: "); fname = scan.nextLine(); currentRecord.fname = fname; System.out.println(" Current record is: "+currentRecord.fname + " "+currentRecord.lname+" "+currentRecord.phone); } break; case "L": case "l" : if(currentRecord == null) System.out.println(" No Current Record"); else { System.out.print(" Enter the new last name: "); lname = scan.nextLine(); phoneList.remove(currentRecord); currentRecord.lname = lname; boolean recordAdded = false; for(int i=0;i if(currentRecord.lname.compareTo(phoneList.get(i).lname) { phoneList.add(i, currentRecord); recordAdded = true; break; } if(!recordAdded) phoneList.add(currentRecord); System.out.println(" Current record is: "+currentRecord.fname + " "+currentRecord.lname+" "+currentRecord.phone); } // ending the loop break; case "N": case "n": System.out.print(" Enter first name : "); fname = scan.nextLine(); System.out.print(" Enter last name : "); lname = scan.nextLine(); System.out.print(" Enter phone number : "); phone = scan.nextLine(); currentRecord = new Phonedir(fname,lname,phone); boolean recordAdded = false; for(int i=0;i if(currentRecord.lname.compareTo(phoneList.get(i).lname) { phoneList.add(i, currentRecord); recordAdded = true; break; } if(!recordAdded) phoneList.add(currentRecord); System.out.println(" Current record is: "+currentRecord.fname + " "+currentRecord.lname+" "+currentRecord.phone); break; case "P": case "p": if(currentRecord == null) System.out.println(" No Current Record"); else { System.out.print(" Enter the new phone number: "); phone = scan.nextLine(); currentRecord.phone = phone; System.out.println(" Current record is: "+currentRecord.fname + " "+currentRecord.lname+" "+currentRecord.phone); } break; case "Q": case "q": break; case "S": case "s": System.out.println(" FirstName \t LastName \t Phone Number"); System.out.println("----------- \t --------- \t-------------"); for(int i=0;i { System.out.println(phoneList.get(i).fname+"\t "+phoneList.get(i).lname+" \t "+phoneList.get(i).phone); } System.out.print(" Enter first name: "); fname = scan.nextLine(); System.out.print(" Enter last name: "); lname = scan.nextLine(); boolean recordFound = false; for(int i=0;i { if(phoneList.get(i).lname.equalsIgnoreCase(lname) && phoneList.get(i).fname.equalsIgnoreCase(fname)) { currentRecord = phoneList.get(i); recordFound = true; break; } } if(recordFound) System.out.println(" Current record is: "+currentRecord.fname + " "+currentRecord.lname+" "+currentRecord.phone); else System.out.println(" No matching record found"); break; default: System.out.println(" Illegal command "); //system representations break; } }while(!choice.equalsIgnoreCase("q")); scan.close(); } } excuted A program to keep a Phone directory a Show all records dDelete the current record f Change the first name in the current record 1 Change the last name in the current record n Add a new record p Change the phone number in the current record q Quit s Select a record from the record list to become the current record Enter a command from the list above (q to quit) f No Current Record A program to keep a Phone directory a Show all records d Delete the current record f Change the first name in the current record 1 Change the last name in the current record Add a new record p Change the phone number in the current record q Quit s Select a record from the record list to become the current record Enter a command from the list above (q to quit) n Enter first name: Barry Enter last name: Drake Enter phone number 770-591-8071
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started