Using Java modify the code below to Write a static method totalDurations that is
ID: 3841666 • Letter: U
Question
Using Java modify the code below to
Write a static method totalDurations that is passed the parallel arrays from the main program. It creates a new pair of parallel arrays (phone numbers and durations, again) where each different incoming phone number is stored exactly once, and the duration is the total duration of all the calls from that phone number. It then prints these arrays. Nothing is returned.
For example, if the arrays contain 555-1234 (duration 10), 555-4321 (duration 20), and 555-1234 (duration 30), then the output would show 555-1234 (duration 40) and 555-4321 (duration 20).
To help you solve the problem, you can call the find method. Use it to check if a phone number has already been placed in the new arrays, and if so, to determine where that number is. Also note that the size of the new arrays will never be greater than the current list.
public class Activity0C {
public static void main(String[] args) {
String[] phoneNumbers = new String[100];
int[] callDurations = new int[phoneNumbers.length];
int size = 0;
findAllCalls(phoneNumbers, callDurations, size, "555-555-5555");
size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);
System.out.println(" Phone numbers (initially):");
printList(phoneNumbers, callDurations, size);
if (find(phoneNumbers, size, 0, "555-555-5555") >= 0) {
System.out.println("Match found.");
} else {
System.out.println("No calls from 555-555-5555.");
}
size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 9);
size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 201);
System.out.println(" Phone numbers (after):");
printList(phoneNumbers, callDurations, size);
int pos = find(phoneNumbers, size, 0, "555-555-5555");
if (pos >= 0) {
System.out.println("Duration of call to 555-555-5555: " + callDurations[pos] + "s.");
} else {
System.out.println("No calls from 555-555-5555.");
}
System.out.println(" Finding all matches:");
findAllCalls(phoneNumbers, callDurations, size, "555-555-0000");
findAllCalls(phoneNumbers, callDurations, size, "555-555-5555");
findAllCalls(phoneNumbers, callDurations, size, "555-555-9999");
System.out.println(" End of processing.");
}
public static int addCall(String[] phoneNumbers, int[] callDurations, int size, String newNumber, int newDuration) {
if (size >= phoneNumbers.length) {
System.out.println("Error adding " + newNumber + ": array capacity exceeded.");
} else {
phoneNumbers[size] = newNumber;
callDurations[size] = newDuration;
size++;
}
return size;
}
public static void printList(String[] phoneNumbers, int[] callDurations, int size) {
for (int i = 0; i < size; i++) {
System.out.println(phoneNumbers[i] + " duration: " + callDurations[i] + "s");
}
}
public static int find(String[] list, int size, int start, String target) {
int pos = start;
while (pos < size && !target.equals(list[pos])) {
pos++;
}
if (pos == size)
pos = -1;
return pos;
}
public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
int matchPos;
System.out.println("Calls from " + targetNumber + ":");
matchPos = find(phoneNumbers, size, 0, targetNumber);
while (matchPos >= 0) {
System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");
// Find the next match, starting after the last one
matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
}
}
}
Explanation / Answer
public class Activity0D {
public static void main(String[] args) {
String[] phoneNumbers = new String[100];
int[] callDurations = new int[phoneNumbers.length];
int size = 0;
size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 16);
size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);
System.out.println(" Phone numbers (initially):");
printList(phoneNumbers, callDurations, size);
System.out.println(" Total Durations By Number : ");
totalDurations(phoneNumbers, callDurations, size);
size = removeCall(phoneNumbers, callDurations, size, 1); // middle
size = removeCall(phoneNumbers, callDurations, size, size - 1); // last
size = removeCall(phoneNumbers, callDurations, size, 0); // first
System.out.println(" Phone numbers (after):");
printList(phoneNumbers, callDurations, size);
size = removeCall(phoneNumbers, callDurations, size, 0);
System.out.println(" Phone numbers (none left):");
printList(phoneNumbers, callDurations, size);
System.out.println(" End of processing.");
}
public static int addCall(String[] phoneNumbers, int[] callDurations, int size, String newNumber, int newDuration) {
if (size >= phoneNumbers.length) {
System.out.println("Error adding " + newNumber + ": array capacity exceeded.");
} else {
phoneNumbers[size] = newNumber;
callDurations[size] = newDuration;
size++;
}
return size;
}
public static void printList(String[] phoneNumbers, int[] callDurations, int size) {
for (int i = 0; i < size; i++) {
System.out.println(phoneNumbers[i] + " duration: " + callDurations[i] + "s");
}
}
public static int find(String[] list, int size, int start, String target) {
int pos = start;
while (pos < size && !target.equals(list[pos])) {
pos++;
}
if (pos == size)
pos = -1;
return pos;
}
public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
int matchPos;
System.out.println("Calls from " + targetNumber + ":");
matchPos = find(phoneNumbers, size, 0, targetNumber);
while (matchPos >= 0) {
System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");
// Find the next match, starting after the last one
matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
}
}
public static int removeCall(String[] phoneNumbers, int[] callDurations, int size, int posToRemove) {
for (int i = posToRemove + 1; i < size; i++) {
phoneNumbers[i - 1] = phoneNumbers[i];
callDurations[i - 1] = callDurations[i];
}
size--;
return size;
}
public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size) {
String[] distinctNumbers = new String[size];
int[] totalDurations = new int[size];
int counter = 0;
for (int i=0; i<size; i++) {
int matchPos = find(distinctNumbers, size, 0, phoneNumbers[i]);
if (matchPos < 0) {
distinctNumbers[counter] = phoneNumbers[i];
totalDurations[counter] = callDurations[i];
counter++;
}
else {
totalDurations[matchPos] += callDurations[i];
}
}
printList(distinctNumbers, totalDurations, counter);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.