Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Science: Lab Assignment Week 8 Problem I ------------------------------

ID: 3594670 • Letter: C

Question

Computer Science: Lab Assignment Week 8

Problem I

---------------------------------------------

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

Problem 2

-----------------------

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}}

Code (for problem 1)

----------------------------------------------------------------------------------------------------------------------

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

   }

}

Code (for problem I continued)

------------------------------------------------------------------------

// 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;
}
}

Code (for problem 2)

--------------------------------------------------------------------------------

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);

   }

}

Code (for problem 2 continued)

----------------------------------------------------------------------------------------

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("} ");
   }
}

Explanation / Answer

import java.util.Scanner;
public class PhoneDirectory {
// 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 for given first name.
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 all phone
// numbers that match the given zipcode.
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();
}
}
/**
* NAME: addNumber
* BEHAVIOR: add a new name (first and last) , phone number and zipcode to the database
* PARAMETERS: None
* COMMENTS: refer the word doc for example of adding record
*/
private static void addNumber() {
System.out.print("Enter the first name: ");
String firstName = sc.next();
System.out.print("Enter the last name: ");
String lastName = sc.next();
System.out.print("Enter phone no: ");
String pNumber = sc.next();
System.out.print("Enter the zipcode: ");
int zipcode = sc.nextInt();
records[recordCount] = new PhoneRecord(firstName, lastName, pNumber, zipcode);
recordCount++;
System.out.println("One record added. Total records: "+recordCount);
}
/**
* NAME: removeNumber
* BEHAVIOR: removes the first record of a given phone no.,
*/
private static void removeNumber() {
System.out.println("Enter phone no:");
String pFind = sc.next();
int index = 0;
boolean found = false;
for(int i=0;i<recordCount;i++){
if(records[i].getNumber().equals(pFind))
{
index =i;
for(int j=index;j<recordCount-1;j++) records[j]=records[j+1];
System.out.println("Record with phone no: "+ pFind+" deleted. Remaining records:"+(recordCount-1));
records[recordCount-1]=null;
recordCount--;
found = true;
break;
}   
}
if(!found)
System.out.println("Record not found.");

}
/**
* NAME: displayRecords
* BEHAVIOR: display all phone records
*/
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());
}
}
/**
* NAME: removeNumberWithSameZipCode
* BEHAVIOR: remove all phone numbers that match the given zipcode.
*/
private static void removeNumberWithSameZipCode() {
int removedRecordCount = 0;
System.out.println("Enter zipcode:");
int pFind = sc.nextInt();
int index = 0;
boolean found = false;
for(int i=0;i<recordCount;i++){
if(records[i].getZipCode()==(pFind))
{
removedRecordCount++;
index =i;
for(int j=index;j<recordCount-1;j++) records[j]=records[j+1];
}
System.out.println("Records with Zipcode: "+ pFind+" deleted. Remaining records:"+(recordCount-removedRecordCount));
for(i=0;i<removedRecordCount;i++)
records[recordCount-1-i]=null;
recordCount = recordCount - removedRecordCount;
found = true;
break;
}
if(!found)
System.out.println("Record not found.");
}
/**
* NAME: findNumber
* BEHAVIOR: findNumber to find phone number for a give first name
* COMMENT: it should list all the numbers for matching input (Refer word doc for example)
* HINT: 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() {
System.out.println("Enter the first name: ");
String pFind = sc.next();
boolean found = false;
for(int i = 0; i < recordCount; i++) {
if(records[i].getName().startsWith(pFind))
{
System.out.println(records[i].getName()+": "+records[i].getNumber());
found = true;
}
}
if(!found)
System.out.println("No record found with given FirstName.");
  
}
/**
* NAME: displayRecordsWithSameLastName
* BEHAVIOR: display all phone records with given last name
*/
private static void displayRecordsWithSameLastName(){
System.out.println("Enter the last name: ");
String pFind = sc.next();
boolean found = false;
for(int i = 0; i < recordCount; i++) {
if(records[i].getName().endsWith(pFind))
{
System.out.println(records[i].getName()+": "+records[i].getNumber());
found = true;
}
}
if(!found)
System.out.println("No record found with given LastName.");   
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote