Problem 1 : You have been given PhoneRecord .java and PhoneDirectory_Lab .java.
ID: 3591384 • Letter: P
Question
Problem 1 :
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.
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: 435
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 20500
Problem 2:
Write a Java class called ArrayTwoDim.java which calculates the sum of rows and columns of a 2D array (Matrix) and displays the 2D array.
The 2D array is already provided in Java class ArrayTwoDimTest.java , use this class to test the ArrayTwoDim.java.
Submit only the ArrayTwoDim.java file. We will use our own ArrayTwoDimTest to test your ArrayTwoDim class when grading your submission.
Input 2D array:
20 15 6 19 18
4 46 24 17 18
12 50 23 16 31
Sample Output:
The sum of row 1 = 78
The sum of row 2 = 109
The sum of row 3 = 132
The sum of column 1 = 36
The sum of column 2 = 111
The sum of column 3 = 53
The sum of column 4 = 52
The sum of column 5 = 67
Explanation / Answer
phone record.java
class PhoneRecord {
private static int howMany;
private String firstName;
private String lastName;
private String phoneNumber;
PhoneRecord(String first, String last, String number) {
howMany++;
firstName = first;
lastName = last;
phoneNumber = number;
}
PhoneRecord(String first, String last, long number) {
howMany++;
firstName = first;
lastName = last;
phoneNumber = Helper.dashMaker(number);
}
static int getHowMany() {
return howMany;
}
String getNumber() {
return phoneNumber;
}
String getAreaCode() {
if (phoneNumber.length() == 14 )
return phoneNumber.substring(1,4);
else
return "XXX";
}
String getFirst() { return firstName;}
String getLast() { return lastName;}
public String toString() {
return (firstName + " " + lastName + " " +
phoneNumber);
}
}
phone directory_lab.java
import java.io.*;
public class PhoneDirectory {
/* The data for the directory is stored in a pair of arrays. The phone
number associated with the name names[i] is numbers[i]. These
arrays will grow, as necessary, to accommodate as many entries as
are added to the directory. The variable count keeps track of
the number of entires in the directory. */
private String[] names = new String[1];
private String[] numbers = new String[1];
private int count = 0;
public boolean changed; // This variable is set to true whenever a change
// is made to the data in this directory. The value
// is false when the object is created. The only time
// that it is reset to false is if the load() method
// is used to load a phone directory from a stream.
// (Programs that use the directory can also set the
// value of changed if they want, since it's public.)
public void load(TextReader in) throws IOException {
// Clears any entries currently in the directory, and loads
// a new set of directory entries from the TextReader. The
// data must consist of the following: a line containing the
// number of entries in the directory; two lines for each
// entry, with the name on the first line and the associated
// number on the second line. Note that this method might
// throw an IllegalArgumentException if the data in the file
// is not valid -- for example if the same name occurs twice.
// Note that if an error does occur, then the original
// data in the directory remains.
int newCount = in.getlnInt();
String[] newNames = new String[newCount + 5];
String[] newNumbers = new String[newCount + 5];
for (int i = 0; i < newCount; i++) {
newNames[i] = in.getln();
newNumbers[i] = in.getln();
}
count = newCount;
names = newNames;
numbers = newNumbers;
changed = false;
}
public void save(PrintWriter out) {
// Saves all the entries in the directory to the PrintWriter.
// Data is written in the same format that is used in the load()
// method. Note that PrintWriters do not throw exceptions.
// To test whether the data was written successfully, the
// caller of this routine can call out.checkError().
out.println(count);
for (int i = 0; i < count; i++) {
out.println(names[i]);
out.println(numbers[i]);
}
}
public String numberFor(String name) {
// Get the phone number associated with the given name, if any.
// If the name does not exist in the directory, null is returned. The
// name cannot be null. (If it is, an IllegalArgumentException is thrown.)
if (name == null)
throw new IllegalArgumentException("Name cannot be null in numberFor(name)");
int position = indexOf(name);
if (position == -1)
return null;
else
return numbers[position];
}
public void addNewEntry(String name, String number) {
// Create a new entry in the directory for the given name and number.
// An IllegalArgumentException is thrown if the name is already in
// the directory or if either of the parameters is null. If the
// arrays, "names" and "numbers", that hold the data are full,
// replace them with larger arrays.
if (name == null)
throw new IllegalArgumentException("Name cannot be null in addNewEntry(name,number)");
if (number == null)
throw new IllegalArgumentException("Number cannot be null in addNewEntry(name,number)");
int position = indexOf(name);
if (position != -1)
throw new IllegalArgumentException("Name already exists in addNewEntry(name,number).");
if (count == names.length) {
String[] tempNames = new String[ 2*count ];
String[] tempNumbers = new String[ 2* count];
System.arraycopy(names, 0, tempNames, 0, count);
System.arraycopy(numbers, 0, tempNumbers, 0, count);
names = tempNames;
numbers = tempNumbers;
}
names[count] = name;
numbers[count] = number;
count++;
changed = true;
}
public void deleteEntry(String name) {
// Remove the entry for the specified name from the directory, if
// there is one. If the specified name does not exist in the
// directory, nothing is done and no error occurs.
if (name == null)
return;
int position = indexOf(name);
if (position == -1)
return;
names[position] = names[count-1];
numbers[position] = numbers[count-1];
count--;
changed = true;
}
public void updateEntry(String name, String number) {
// Change the number associated with the given name. An IllegalArgumentException
// is thrown if the name does not exist in the directory or if either of
// the parameters is null.
if (name == null)
throw new IllegalArgumentException("Name cannot be null in updateEntry(name,number)");
if (number == null)
throw new IllegalArgumentException("Number cannot be null in updateEntry(name,number)");
int position = indexOf(name);
if (position == -1)
throw new IllegalArgumentException("Name not found in updateEntry(name,number).");
numbers[position] = number;
changed = true;
}
private int indexOf(String name) {
// Finds and returns the position of the name in the names array,
// ignoring case. Returns -1 if the name does not occur in the
// array.
for (int i = 0 ; i < count; i++) {
if (names[i].equalsIgnoreCase(name))
return i;
}
return -1;
}
} // end class PhoneDirectory
ArrayTwoDim.java
/**
* This class contains common methods to
* manipulate two-dimensional array.
*/
public class TwoDimArrayMethods
{
public void printMatrix(int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
{
System.out.printf("%7d", matrix[row][col]);
}
System.out.println();
}
}
public void sumRows(int[][] matrix)
{
int sum;
// sum of each individual row
for (int row = 0; row < matrix.length; row++)
{
sum = 0;
for (int col = 0; col < matrix[row].length; col++)
{
sum = sum + matrix[row][col];
}
System.out.println("The sum of row " + (row + 1) + " = "
+ sum);
}
}
public void largestInRows(int[][] matrix)
{
int largest;
for (int row = 0; row < matrix.length; row++)
{
// assume that the first element of the row is largest
largest = matrix[row][0];
for (int col = 1; col < matrix[row].length; col++)
{
if (largest < matrix[row][col])
{
largest = matrix[row][col];
}
}
System.out.println("The largest element of row " + (row + 1)
+ " = " + largest);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.