A java program that will implement a rudimentary phone book, storing the first n
ID: 3707340 • Letter: A
Question
A java program that will implement a rudimentary phone book, storing the first name, last name, and phone number
for each person in the phone book. The "phonebook" will be stored in a 10x3 array of
String
s, with the first
column storing first names, the second column storing last names, and the third column storing the phone
number.
Here is an example of how the array might look for a 5x3 phonebook:
Column 0
Column 1
Column 2
Row 0
"Wanda"
"Maximoff"
"513-222-3333"
Row 1
"Jean"
"Grey"
"111-2222"
Row 2
"Peter"
"Quill"
"4021110000"
Row 3
"Professor"
"X"
"x612"
Row 4
"Black"
"Widow"
"none"
The first and last names will always be a single "word", and the phone number in column 2 can be in any
format.
Program 11 - Spring 2018
Overview of Assignment
All eight methods and parameters are required for Web-CAT to test and score them.
Arrays passed to
methods may have any number of rows, but will always have three columns. (This allows for future
expansion of your code.)
1.
public static void initBook(String[][] book)
This method will set every element of the array parameter
book
to an empty string
""
, initializing the
phone book array to a default state that has no entries in it.
2.
public static boolean bookEmpty(String[][] book)
This method will return
true
if the phone book is empty,
false
otherwise.
Must handle
null
s
:
Entries with
null
should be treated as empty.
3.
public static void displayBook(String[][] book)
This method will display each entry in the format
#. LAST, FIRST (PHONE)
, where
#
is the row in
the phonebook array,
LAST
is the last name,
FIRST
is the first name and
PHONE
is the phone
number. If there are no valid entries, display
No entries.
.
4.
public static int findEmpty(String[][] book)
This method will return an
int
value corresponding to the first "empty" (at least the firstname cell is
null
or
""
) position located in the array, starting from row
0
.
If the array is full (i.e., all rows have
an entry), return
-1
.
Must handle
null
s
: Entries with
null
should be treated as empty.
5.
public static void addEntry(String[][] book, Scanner g)
If there is an empty/available row, this method will add a new entry to the phone book, asking for first
name, then last name, then phone number.
Otherwise, display
Phonebook is full!
.
6.
public static void deleteEntry(String[][] book, Scanner g)
If there are no entries in the phonebook, the message
No entries.
should be displayed. Otherwise,
the phonebook should be displayed and prompt for an entry number to delete an entry or non-entry
number to cancel. Catch out of bounds entries. Deleting an entry sets all elements in that row to
""
.
Display
No record deleted.
or a deletion message (see Sample run) as appropriate.
7.
public static void sortBook(String[][] book)
This method will sort the phonebook entries by last name. You can either implement a bubble sort or a
selection sort for this. See "Points to Think About" for information about duplicate last names. The prompt
Phonebook sorted.
will print if the array is non-empty and
No entries.
if it is empty.
8.
public static void menu(String[][] book, Scanner g)
This provided method displays a menu and calls the appropriate methods.
(Only remove comments.
The following starter code is provided to begin the program:
import java.util.Scanner;
public class Phonebook {
private static Scanner get;
/**
* It's-a-me, the mainio!
* @param args An array of strings
*/
public static void main(String[] args) {
// YOU SHOULDN'T NEED TO MODIFY ANYTHING HERE IN THE MAIN METHOD
get = new Scanner(System.in);
String[][] phonebook = new String[10][3];
initBook(phonebook);
menu(phonebook, get);
}
/**
* Initialize the phonebook to all empty entries.
*
* @param book The 2-D array representing the phone book
*/
public static void initBook(String[][] book) {
// YOU WILL NEED TO FILL THIS METHOD IN
}
/**
* Displays the main menu.
*
* @param book The 2-D array representing the phone book
* @param g A Scanner to use
*/
public static void menu(String[][] book, Scanner g) {
int choice = 0;
System.out.println("Phonebook Main Menu");
do {
System.out.println(" 1. Display Phonebook");
System.out.println("2. Add Entry");
System.out.println("3. Delete Entry");
System.out.println("4. Sort Book");
System.out.println("9. Quit");
System.out.print(" Enter choice: ");
choice = g.nextInt();
System.out.println();
switch (choice) {
case 1:
//displayBook(book);
break;
case 2:
//addEntry(book, g);
break;
case 3:
//deleteEntry(book, g);
break;
case 4:
//sortBook(book);
break;
}
} while (choice != 9);
}
}
Explanation / Answer
the below implemented Phonebook class which contains all the methods.
****** Phonebook *****
import java.util.Scanner;
public class Phonebook {
public static void main(String[] args) {
String phoneBooks [][] = new String[10][3];
Scanner userInput = new Scanner(System.in);
initBook(phoneBooks);
menu(phoneBooks, userInput);
}
public static void initBook(String[][] book) {
for (int i=0;i<book.length;i++) {
for (int j=0;j<book[i].length;j++) {
book[i][j] = "";
}
}
}
public static boolean bookEmpty(String[][] book) {
for (int i=0;i<book.length;i++) {
for (int j=0;j<book[i].length;j++) {
if (book[i][j] != null && !book[i][j].equalsIgnoreCase("")) {
return false;
}
}
}
return true;
}
public static void displayBook(String[][] book) {
if (!bookEmpty(book)) {
System.out.println();
for (int i=0;i<book.length;i++) {
if (book[i][0] != null && !book[i][0].equalsIgnoreCase("") &&
book[i][1] != null && !book[i][1].equalsIgnoreCase("") &&
book[i][2] != null && !book[i][2].equalsIgnoreCase("")) {
String line = i+ ". " + book[i][1] + ", " + book[i][0] + " ("+ book[i][2]+")";
System.out.println(line);
}
}
System.out.println();
} else {
System.out.println(" No entries. ");
}
}
public static int findEmpty(String[][] book) {
for (int i=0;i<book.length;i++) {
boolean emptyFlag = false;
for (int j=0;j<book[i].length;j++) {
if (book[i][j] == null || book[i][j].equalsIgnoreCase("")) {
emptyFlag = true;
} else {
emptyFlag = false;
}
}
if (emptyFlag){
return i;
}
}
return -1;
}
public static void addEntry(String[][] book, Scanner g) {
System.out.println();
int entryRow = findEmpty(book);
if (entryRow != -1) {
while (true) {
System.out.print("Enter first name: ");
String firstName = g.nextLine();
if (firstName != null && !firstName.equalsIgnoreCase("")) {
book[entryRow][0] = firstName;
break;
}
}
while (true) {
System.out.print("Enter last name: ");
String lastName = g.nextLine();
if (lastName != null && !lastName.equalsIgnoreCase("")) {
book[entryRow][1] = lastName;
break;
}
}
while (true) {
System.out.print("Enter phone number: ");
String phoneNumber = g.nextLine();
if (phoneNumber != null && !phoneNumber.equalsIgnoreCase("")) {
book[entryRow][2] = phoneNumber;
break;
}
}
System.out.println();
} else {
System.out.println(" Phonebook is full. ");
}
}
public static void deleteEntry(String[][] book, Scanner g) {
if (!bookEmpty(book)) {
System.out.println();
displayBook(book);
System.out.println();
System.out.print("Enter number of entry to delete, any other integer value to cancel: ");
int entry = g.nextInt();
if (entry >=0 && entry <=9) {
if (book[entry][0] != null && !book[entry][0].equalsIgnoreCase("") &&
book[entry][1] != null && !book[entry][1].equalsIgnoreCase("") &&
book[entry][2] != null && !book[entry][2].equalsIgnoreCase("")) {
System.out.println(" Record "+entry+" deleted: "+book[entry][1]+", "+book[entry][0]+" ("+book[entry][2] +") ");
for (int j=0;j<3;j++) {
book[entry][j] = "";
}
} else {
System.out.println(" No record deleted. ");
}
} else {
System.out.println(" No record deleted. ");
}
} else {
System.out.println(" No entries. ");
}
}
public static void sortBook(String[][] book) {
for (int j = 0; j < book.length; j++) {
for (int i = 0; i < book.length - j - 1; i++) {
if (book[i + 1][1].compareTo(book[i][1]) < 0) {
String tempFirstName = book[i][0];
book[i][0] = book[i + 1][0];
book[i+1][0] = tempFirstName;
String tempLastName = book[i][1];
book[i][1] = book[i + 1][1];
book[i+1][1] = tempLastName;
String tempPhoneNumber = book[i][2];
book[i][2] = book[i + 1][2];
book[i+1][2] = tempPhoneNumber;
}
}
}
System.out.println(" Phonebook sorted. ");
}
public static void menu(String[][] book, Scanner g) {
int choice = 0;
System.out.println("Phonebook Main Menu");
do {
System.out.println();
System.out.println("1. Display Phonebook");
System.out.println("2. Add Entry");
System.out.println("3. Delete Entry");
System.out.println("4. Sort Book");
System.out.println("9. Quite");
System.out.print(" Enter choice: ");
choice = g.nextInt();
g.nextLine();
switch(choice) {
case 1:
displayBook(book);
break;
case 2:
addEntry(book, g);
break;
case 3:
deleteEntry(book, g);
break;
case 4:
sortBook(book);
break;
}
} while (choice != 9);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.