Use your class named PhoneEntry from Project 4 to create a driver program/class
ID: 3554433 • Letter: U
Question
Use your class named PhoneEntry from Project 4 to create a driver program/class to prompt the user for and to enter an array of up to 100 PhoneEntry objects. Then, for as long as the user wants, input a last name and display all of the information (first name, last name and phone) for every PhoneEntry with that last name. The list does not need to be sorted.
Be sure to add comments with a description of the program, your name, the date, and a short comment for each variable declaration. Also, add comments for each method describing the method and commenting all parameters, return values and local variables. Also be sure to use good variable names.
Explanation / Answer
//PhoneEntry.java
public class PhoneEntry {
String first, last;
int phone;
public PhoneEntry(){
first = "";
last = "";
phone = 000000000;
}
public PhoneEntry(String f, String l, int p){
first = f;
last = l;
phone = p;
}
public void setFirst(String f){
first = f;
}
public void setLast(String l){
last = l;
}
public void setPhone(int p){
phone = p;
}
public String getFirst(){
return first;
}
public String getLast(){
return last;
}
public int getPhone(){
return phone;
}
public String toString(){
return "First name: " + getFirst() + " Last name: " + getLast() + " Phone number: " + getPhone() + " ";
}
}
// Phone.java
import java.util.Scanner;
public class Phone { // declaring the class
public static void main(String args[]){ // defining the main method
int n, i; // n = number of enteries, i = variable for iteration
String name, more; /* variables to take input for searching and asking the user for more input */
Scanner in = new Scanner(System.in); // declaring Scaner object for input
System.out.print("Enter the number of entries in your phone book: "); /* Prompt the user for number of enteries */
n = in.nextInt(); // taking the number of enteries
PhoneEntry arr[] = new PhoneEntry[n]; // array for storing entries
System.out.println("Enter " + n + " last names, first names and phone numbers:"); /* Prompting for the contacts in the list */
for(i = 0; i < n; i++){ // loop to take list input
arr[i] = new PhoneEntry(); // initialize the array's ith element
arr[i].last = in.next(); // last name input
arr[i].first = in.next(); // first name input
arr[i].phone = in.nextInt(); // phone number input
}
do{ // loop to continue the search
System.out.print(" Enter the last name of the contact you want to search: "); /* prompt the user for a last name to search */
name = in.next(); // last name input to search
for(i = 0; i < n; i++){ // loop to search the list for desired last name
if(arr[i].last.equals(name)){ // if match found, print details of the matched contact
System.out.println(arr[i].toString()); // print match
}
}
System.out.print("Do you want to continue? Enter y/n:"); /* Prompting if the user wants to search again */
more = in.next(); // y/n input
if(more.equals("y")) continue; // if user enters y, search again
else break; // if user does not enter y, exit the program
}while(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.