HW1 (Note: this problem is an extension of the Lab Problem1): You have been give
ID: 3593874 • Letter: H
Question
HW1 (Note: this problem is an extension of the Lab Problem1):
You have been given PhoneRecord.java and PhoneDirectory_HW.java.
Phonedirectory_HW.java has 6 methods:
removeNumber------- has been given already
displayRecords------- has been given already
addNumber ------you need to write the code for it
removeNumberWithSameZipCode ------you need to write the code for it
findNumber ------you need to write the code for it
displayRecordsWithSameLastName ------you need to write the code for it
The two methods, removeNumber and displayRecords has already completed. You need to complete the remaining 4 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_HW.java file.
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc 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, da, f, r, rc 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, da, f, r, rc 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, da, f, r, rc or q): f
Enter name to look up: keller
Enter command (a, d, da, f, r, rc or q): f
Enter name to look up: ri
richard keller 1233939 121212
Enter command (a, d, da, f, r, rc or q): dl
Command was not recognized; please enter only a, d, da, f, r, rc or q.
Enter command (a, d, da, f, r, rc or q): da
Enter the last name: richardson
Enter command (a, d, da, f, r, rc or q): da
Enter the last name: keller
nick keller 12345 121212
richard keller 1233939 121212
Enter command (a, d, da, f, r, rc or q): r
Enter phone no:
9
The phone no do not exist.
Enter command (a, d, da, f, r, rc or q): d
1. nick keller 12345 121212
2. sarah keler 12345 121212
3. richard keller 1233939 121212
Enter command (a, d, da, f, r, rc or q): r
Enter phone no:
12345
Record with phone no: 12345 deleted.
Enter command (a, d, da, f, r, rc or q): d
1. sarah keler 12345 121212
2. richard keller 1233939 121212
Enter command (a, d, da, f, r, rc or q): a
Enter first name: Frank
Enter last name: Underwood
Enter phone number: 435
Enter zip code: 20500
1 record added. Total Records: 3
Enter command (a, d, da, f, r, rc or q): d
1. sarah keler 12345 121212
2. richard keller 1233939 121212
3. Frank Underwood 435 20500
Enter command (a, d, da, f, r, rc or q): a
Enter first name: claire
Enter last name: underwood
Enter phone number: 435
Enter zip code: 20500
1 record added. Total Records: 4
Enter command (a, d, da, f, r, rc or q): d
1. sarah keler 12345 121212
2. richard keller 1233939 121212
3. Frank Underwood 435 20500
4. claire underwood 435 20500
Enter command (a, d, da, f, r, rc or q): rc
Enter zip code:20500
No of records deleted: 2. Remaining Records: 2
Enter command (a, d, da, f, r, rc or q): d
1. sarah keler 12345 121212
2. richard keller 1233939 121212
HW2:
You have been given ArrayManipulation.java. You will implement 4 method bodies in ArrayManipulation.java:
createTwoDArray
rotateRowOnce
frequency
SwapRow
The meaning of each method is given before the method header in ArrayManipulation.java. For your testing purpose, we also provide an ArrayTest class (ArrayTest.java) that you may use to test your ArrayManipulation.java. With this ArrayTest program, you should see the following output. Note that we will use different arrays to test your implementation when grading your implementation. Submit only the ArrayManipulation.java file. We will use our own ArrayTest to test your ArrayManipulation class when grading your submission.
{7,7,5,6,0,6,9,3,2,0,4,3}
{{7, 7, 5, 6},
{0, 6, 9, 3},
{2, 0, 4, 3}}
{{7, 7, 5},
{6, 0, 6},
{9, 3, 2},
{0, 4, 3}}
error -- the array is null
error
{{9, 0, 6, 9},
{4, 6, 0, 4},
{5, 1, 7, 2}}
{{9, 9, 0, 6},
{4, 6, 0, 4},
{5, 1, 7, 2}}
{{9, 9, 0, 6},
{4, 6, 0, 4},
{2, 5, 1, 7}}
{{9, 9, 0, 6},
{4, 6, 0, 4},
{2, 5, 1, 7}}
frequency =2
frequency =0
frequency =1
error
error
error
{{9, 9, 0, 6},
{4, 6, 0, 4},
{2, 5, 1, 7}}
{{2, 5, 1, 7},
{4, 6, 0, 4},
{9, 9, 0, 6}}
{{4, 6, 0, 4},
{2, 5, 1, 7},
{9, 9, 0, 6}}
import java.util.Scanner;
public class PhoneDirectory_HW {
// 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 "
+ " da -Display all with same Last Name "
+ " f - Find a phone number "
+ " r - Remove a phone number "
+ " rc -Remove all records with same zip code "
+ " 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, da, f, r, rc or q): ");
String command = sc.nextLine();
// Determine whether command is "a", "f", "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("f")) {
// Command is "f". Call findNumber to find phone
// numbers that match the user's criteria.
findNumber();
} else if (command.equalsIgnoreCase("d")) {
// Command is "d". Call displayRecords to display all phone
// numbers
displayRecords();
}else if (command.equalsIgnoreCase("da")) {
// Command is "d". Call displayRecords to display all phone
// numbers
displayRecordsWithSameLastName();
}
else if (command.equalsIgnoreCase("r")) {
// Command is "r". Call removeNumber to remove phone
// numbers that match the user's criteria.
removeNumber();
}else if (command.equalsIgnoreCase("rc")) {
// Command is "r". Call removeNumber to remove phone
// numbers that match the user's criteria.
removeNumberWithSameZipCode();
}
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, da, f, r, rc 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 removeNumber() {
//this code removes a phone no.
System.out.println("Enter phone no:");
String pFind = sc.next();
int index = 0;
boolean found = true;
for(int i=0;i<recordCount;i++){
if(records[i].getNumber().equals(pFind))
{
index =i;
for(int j=index;j<recordCount;j++){
records[j]=records[j+1];
}
System.out.println("Record with phone no: "+ pFind+" deleted.");
records[recordCount-1]=null;
recordCount--;
found = true;
}
else
{
found = false;
}
}
if(!found)
System.out.println("The phone no do not exist.");
}
private static void displayRecords() {
for (int i = 0; i < recordCount; i++) {
System.out.println(i + 1 + ". " + records[i].getName() + " "
+ records[i].getNumber()+" "+records[i].getZipCode());
}
}
private static void removeNumberWithSameZipCode() {
// your code here
}
// use PhoneRecord getName() method in conjunction with startsWith(String prefix) String method, see:
// http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29
private static void findNumber() {
// your code here
}
private static void displayRecordsWithSameLastName(){
// your code here
}
}
public class ArrayManipulation {
/** given a 1-D array return a 2-D array
* For example: int[] array = {1, 2, 3, 4, 5, 6}, numRows = 2
* return {{1, 2, 3}, {4, 5, 6}}
* if the array can't be evenly divided by numRows, then return null
*/
public static int[][] createTwoDArray( int[] array, int numRows){
// implement
}
/** int[row][column] array
* rotate row = indexOfRowToRotate once. If a row is composed of numbers:
* {1, 4, 9, 8}, then after one rotation it is {8, 1, 4, 9}
* If the indexOfRowToRotate is out of boundary, print error and do nothing
*/
public static void rotateRowOnce( int[][] array, int indexOfRowToRotate){
// implement
}
/**
* return the number of occurrence of the given integer in the array
*/
public static int frequency( int[][] array, int integer){
// implement
}
// swap the two rows of the 2D array and return the resulting array
// If rowOne or rowTwo is out of boundary, or if rowOne is the same as rowTwo, print error and do nothing
public static void swapRow( int[][] array, int rowOne, int rowTwo){
// implement
}
// may be helpful for debugging
public static void display2DArray( int[][] array){
if (array == null){
System.out.println("error -- the array is null");
return;
}
System.out.print("{");
for (int i = 0; i < array.length; i++){
System.out.print("{");
for (int k = 0; k < array[0].length - 1; k++){
System.out.print( array[i][k] + ", ");
}
System.out.print( array[i][array[0].length - 1] + "}");
if (i != (array.length - 1)){
System.out.print(", ");
}
}
System.out.print("} ");
}
public static void display1DArray(int[] array){
if (array == null){
System.out.println("error -- the array is null");
return;
}
System.out.print("{");
for (int i = 0; i < array.length - 1; i++){
System.out.print(array[i] + ",");
}
System.out.print(array[array.length - 1]);
System.out.print("} ");
}
}
public class ArrayTest {
public static int[][] arrayTwoSmall = {{9, 0, 6, 9},
{4, 6, 0, 4},
{5, 1, 7, 2}};
public static int[] arrayOneSmall = {7, 7, 5, 6, 0, 6, 9, 3, 2, 0, 4, 3};
public static void main(String[] args) {
ArrayManipulation.display1DArray(arrayOneSmall);
ArrayManipulation.display2DArray( ArrayManipulation.createTwoDArray( arrayOneSmall, 3));
ArrayManipulation.display2DArray( ArrayManipulation.createTwoDArray( arrayOneSmall, 4));
ArrayManipulation.display2DArray( ArrayManipulation.createTwoDArray( arrayOneSmall, 5));
ArrayManipulation.rotateRowOnce( arrayTwoSmall, 6);
ArrayManipulation.display2DArray(arrayTwoSmall);
ArrayManipulation.rotateRowOnce( arrayTwoSmall, 0);
ArrayManipulation.display2DArray(arrayTwoSmall);
ArrayManipulation.rotateRowOnce( arrayTwoSmall, 2);
ArrayManipulation.display2DArray(arrayTwoSmall);
ArrayManipulation.display2DArray( arrayTwoSmall );
System.out.println("frequence ="+ArrayManipulation.frequency(arrayTwoSmall, 6));
System.out.println("frequence ="+ArrayManipulation.frequency(arrayTwoSmall, -1));
System.out.println("frequence ="+ArrayManipulation.frequency(arrayTwoSmall, 7));
ArrayManipulation.swapRow( arrayTwoSmall, 1, 6);
ArrayManipulation.swapRow( arrayTwoSmall, 1, 1);
ArrayManipulation.swapRow( arrayTwoSmall, -1, -1);
ArrayManipulation.display2DArray(arrayTwoSmall);
ArrayManipulation.swapRow( arrayTwoSmall, 0, 2);
ArrayManipulation.display2DArray(arrayTwoSmall);
ArrayManipulation.swapRow( arrayTwoSmall, 0, 1);
ArrayManipulation.display2DArray(arrayTwoSmall);
}
}
Explanation / Answer
// File Name: PhoneRecord.java
// Class PhoneRecord definition
public class PhoneRecord
{
// Instance variables to store data
String firstName;
String lastName;
String phoneNumber;
String zipCode;
// Parameterized constructor
PhoneRecord(String fn, String ln, String ph, String zc)
{
firstName = fn;
lastName = ln;
phoneNumber = ph;
zipCode = zc;
}// End of constructor
}// End of class
---------------------------------------------------------------------------------------------------------------------------------------------
// File Name: PhoneDirectory_HW.java
import java.util.*;
public class PhoneDirectory_HW
{
String fName, lName, pn, zp;
// Creates an array of object of class PhoneRecord
PhoneRecord pr[] = new PhoneRecord[50];
// Record counter
int record = 0;
// Scanner class object created to accept data
Scanner sc = new Scanner(System.in);;
// Method to display menu and accept user choice
String menu()
{
String choice;
// Displays menu information
System.out.println(" Phone directory commands: ");
System.out.println(" a - Add a new phone number ");
System.out.println(" d - Display All ");
System.out.println(" da -Display all with same Last Name ");
System.out.println(" f - Find a phone number ");
System.out.println(" r - Remove a phone number ");
System.out.println(" rc -Remove all records with same zip code ");
System.out.println(" q – Quit ");
System.out.println(" Enter command (a, d, da, f, r, rc or q): ");
// Accepts user choice
choice = sc.next();
sc.nextLine();
// Returns user choice
return choice;
}// End of method
// Method to add a phone record
void addNumber()
{
// Accepts data from the user
System.out.println(" Enter first name: ");
fName = sc.next();
System.out.println(" Enter last name: ");
lName = sc.next();
System.out.println(" Enter phone number: ");
pn = sc.next();
System.out.println(" Enter zip code: ");
zp = sc.next();
// Adds the record
pr[record] = new PhoneRecord(fName, lName, pn, zp);
// Increase the record counter
record++;
System.out.println(" 1 record added. Total Records: " + record);
}// End of method
// Method to display all the records
void displayRecords()
{
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Displays record number
System.out.print(x + ". ");
// Displays information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber + " " + pr[x].zipCode);
}// End of for loop
}// End of method
// Method to display all records with same last name
void displayRecordsWithSameLastName()
{
// Accepts last name from the user
System.out.println(" Enter the last name: ");
lName = sc.next();
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Checks if the last name entered by the user and last name in the phone record are same
if(pr[x].lastName.equalsIgnoreCase(lName))
{
// If same display the information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber + " " + pr[x].zipCode);
}// End of if condition
}// End of for loop
}// End of method
// Method to display phone number matching with specified first name
void findNumber()
{
// Accept first name from the user
System.out.println(" Enter name to look up:: ");
fName = sc.next();
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Compares first name entered by the user and the first name in the phone record
if(pr[x].firstName.equalsIgnoreCase(fName))
{
// If same display the information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber + " " + pr[x].zipCode);
}// End of if condition
}// End of while loop
}// End of method
// Method to delete a record based on the phone number entered by the user
void removeNumber()
{
// Initializes flag to zero for record not found
int f = 0, x;
// Accepts phone number from the user
System.out.println(" Enter phone no: ");
pn = sc.next();
// Loops till end of records
for(x = 0; x < record; x++)
{
// Checks if the phone number entered by the user is same as the phone number in phone record
if(pr[x].phoneNumber.equalsIgnoreCase(pn))
{
// Set the flag to one for record found
f = 1;
// Come out of the loop
break;
}// End of if
}// End of for
// Checks if flag value is zero display record not found
if(f == 0)
System.out.println("The phone no do not exist.");
// Otherwise record found
else
{
// Loops till end of records
for(int y = x; y < record; y++)
// Swaps record one position back
pr[y] = pr[y+1];
// Displays message
System.out.print(" Record with phone no: " + pn + " deleted.");
// Decrease the record counter by one
record--;
}// End of else
}// End of method
// Method to delete all the record matching with the zip code entered by the user
void removeNumberWithSameZipCode()
{
// Initializes flag to zero for record not found
int f = 0, x;
// Counter for number of record deleted
int no = 0;
// Accepts zip code from the user
System.out.println(" Enter zip code: ");
zp = sc.next();
// Loops till end of records
for(x = 0; x < record; x++)
{
// Checks if the phone number entered by the user is same as the phone number in phone record
if(pr[x].zipCode.equalsIgnoreCase(zp))
{
// Loops till end of records
for(int y = x; y < record; y++)
// Set the flag to one for record found
pr[y] = pr[y+1];
// Decrease the record counter by one
record--;
// Increase the deleted record counter by one
no++;
// Set the flag to one for record found
f = 1;
}// End of if
}// End of for
// Checks if flag value is zero display record not found
if(f == 0)
System.out.println(" The phone no do not exist.");
// Otherwise record found
else
System.out.println(" No of records deleted: " + no + ". Remaining Records: " + record);
}// End of method
// Main method definition
public static void main(String[] args)
{
// Creates an object of class PhoneDirectory_HW
PhoneDirectory_HW pw = new PhoneDirectory_HW();
// Loops till user enters 'q'
do
{
// Displays menu
String choice = pw.menu();
// Checks the menu option and calls the appropriate method
if(choice.equalsIgnoreCase("a"))
pw.addNumber();
else if(choice.equalsIgnoreCase("d"))
pw.displayRecords();
else if(choice.equalsIgnoreCase("da"))
pw.displayRecordsWithSameLastName();
else if(choice.equalsIgnoreCase("f"))
pw.findNumber();
else if(choice.equalsIgnoreCase("r"))
pw.removeNumber();
else if(choice.equalsIgnoreCase("rc"))
pw.removeNumberWithSameZipCode();
else if(choice.equalsIgnoreCase("q"))
System.exit(0);
else
System.out.println(" Command was not recognized; please enter only a, d, da, f, r, rc or q. ");
}while(true); // End of do - while
}// End of main method
}// End of method
Sample Run:
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
a
Enter first name:
pyari
Enter last name:
sahu
Enter phone number:
123
Enter zip code:
111
1 record added. Total Records: 1
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
a
Enter first name:
Ram
Enter last name:
sahu
Enter phone number:
456
Enter zip code:
121
1 record added. Total Records: 2
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
a
Enter first name:
Suresh
Enter last name:
padhi
Enter phone number:
783
Enter zip code:
1254
1 record added. Total Records: 3
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
d
0. pyari sahu 123 111
1. Ram sahu 456 121
2. Suresh padhi 783 1254
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
da
Enter the last name:
sahu
pyari sahu 123 111
Ram sahu 456 121
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
f
Enter name to look up::
pyari
pyari sahu 123 111
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
r
Enter phone no:
123445
The phone no do not exist.
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
f
Enter name to look up::
456
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
d
0. pyari sahu 123 111
1. Ram sahu 456 121
2. Suresh padhi 783 1254
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
r
Enter phone no:
555
The phone no do not exist.
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
f
Enter name to look up::
Ram
Ram sahu 456 121
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
r
Enter phone no:
456
Record with phone no: 456 deleted.
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
d
0. pyari sahu 123 111
1. Suresh padhi 783 1254
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
rc
Enter zip code:
111
No of records deleted: 1. Remaining Records: 1
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
d
0. Suresh padhi 783 1254
Phone directory commands:
a - Add a new phone number
d - Display All
da -Display all with same Last Name
f - Find a phone number
r - Remove a phone number
rc -Remove all records with same zip code
q – Quit
Enter command (a, d, da, f, r, rc or q):
q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.