***Please comment what each line does if possible*** ***This is a JAVA question*
ID: 3591297 • Letter: #
Question
***Please comment what each line does if possible***
***This is a JAVA question***
You have been given PhoneRecord.java and PhoneDirectory_Lab.java.
Phonedirectory_Lab.java has 2 methods:
displayRecords------- has been given already
addNumber ------you need to write the code for it
You need to complete both the methods. Here is a sample output that we expect to see from your program (of course, if the user’s inputs are different, the results will be different from the sample output). Note: you don't need to handle the case where two or more people have the same phone number. Each method is worth 1 point. Submit only the PhoneDirectory_Lab.java file.
---------------------------------------------------------
// Represents a record containing a name and a phone number
class PhoneRecord {
private String firstName;
private String lastName;
private String number;
private int zipCode;
///////////////////////////////////////////////////////////
// NAME: PhoneRecord
// BEHAVIOR: Constructs a phone record containing the
// specified name and phone number
// PARAMETERS: personName - name of a person
// phoneNumber - phone number for that person
///////////////////////////////////////////////////////////
public PhoneRecord(String firstName, String lastName, String phoneNumber, int zipCode) {
this.firstName = firstName;
this.lastName = lastName;
number = phoneNumber;
this.zipCode = zipCode;
}
///////////////////////////////////////////////////////////
// NAME: getName
// BEHAVIOR: Returns the name stored in this record
// PARAMETERS: None
// RETURNS: The name stored in this record
///////////////////////////////////////////////////////////
public String getName() {
return firstName+" "+lastName;
}
///////////////////////////////////////////////////////////
// NAME: getNumber
// BEHAVIOR: Returns the phone number stored in this
// record
// PARAMETERS: None
// RETURNS: The phone number stored in this record
///////////////////////////////////////////////////////////
public String getNumber() {
return number;
}
public int getZipCode(){
return zipCode;
}
}
----------------------------------------------------------
import java.util.Scanner;
public class PhoneDirectory_Lab {
// Class variables
static PhoneRecord[] records = new PhoneRecord[50];
static int recordCount = 0;
static Scanner sc ;
public static void main(String[] args) {
// Display list of commands
System.out.println("Phone directory commands: "
+ " a - Add a new phone number "
+ " d - Display All "
+ " q - Quit ");
// Read and execute commands
while (true) {
sc= new Scanner(System.in);
// Prompt user to enter a command
System.out.print("Enter command (a, d or q): ");
String command = sc.nextLine();
// Determine whether command is "a", "d", "q", or
// illegal; execute command if legal.
if (command.equalsIgnoreCase("a")) {
// Command is "a". Call addNumber to add a new
// name and number to the database
addNumber();
}
else if (command.equalsIgnoreCase("d")) {
// Command is "d". Call displayRecords to display all phone
// numbers
displayRecords();
}
else if (command.equalsIgnoreCase("q")) {
// Command is "r". Call removeNumber to remove phone
// numbers that match the user's criteria.
break;
}
else {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; "
+ "please enter only a, d or q.");
}
System.out.println();
}
}
private static void addNumber() {
//this method adds a phone record, ie. first name, lastname,
//phone number and zipcode
}
private static void displayRecords() {
}
}
Phone directory commands: a - Add a new phone number d Display All q-Quit Enter command (a, d or q): a Enter first name: nick Enter last name: keller Enter phone number: 12345 Enter zip code: 121212 1 record added. Total Records: 1 Enter command (a, d or q): a Enter first name sarah Enter last name: keler Enter phone number: 12345 Enter zip code: 121212 1 record added. Total Records: 2 Enter command (a, d or q): dl Command was not recognized; please enter only a, d or q Enter command (a, d or q): a Enter first name richard Enter last name: keller Enter phone number: 1233939 Enter zip code: 121212 1 record added. Total Records: 3 Enter command (a, d or q): d 1. nick keller 12345 121212 2. sarah keler 12345 121212 3. richard keller 1233939 121212 Enter command (a, d or q) a Enter first name: claire Enter last name underwood Enter phone number: 43!5 Enter zip code: 20500 1 record added. Total Records: 4 Enter command (a, d or q): d 1. nick keller 12345 121212 2. sarah keler 12345 121212 3. richard keller 1233939 121212 4. claire underwood 435 20500Explanation / Answer
please find the defination for the function
private static void addNumber() {
//this method adds a phone record, ie. first name, lastname,
//phone number and zipcode
Scanner sc = new Scanner(System.in);
System.out.println("Enter first name: ");
String firstName = sc.next();
System.out.println("Enter last name: ");
String lastName = sc.next();
System.out.println("Enter phone number: ");
String phone = sc.next();
System.out.println("Enter zip code: ");
String zip = sc.next();
PhoneRecord record = new PhoneRecord(firstName, lastName, phone, zip);
PhoneDirectory_Lab.records[recordCount] = record;
PhoneDirectory_Lab.recordCount++;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.