Modify the following code by JAVA language
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), and555-1234 (duration 30), then the output would
show 555-1234 (duration 40) and555-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 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-9876",
382);
System.out.println("Phone numbers (initially):");
printList(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("\nPhone numbers (after):");
printList(phoneNumbers, callDurations, size);
size = removeCall(phoneNumbers, callDurations, size, 0);
System.out.println("\nPhone numbers (none left):");
printList(phoneNumbers, callDurations, size);
System.out.println("\nEnd 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 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-9876", 382);
System.out.println("Phone numbers (initially):");
printList(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("\nPhone numbers (after):");
printList(phoneNumbers, callDurations, size);
size = removeCall(phoneNumbers, callDurations, size, 0);
System.out.println("\nPhone numbers (none left):");
printList(phoneNumbers, callDurations, size);
// test totalDurations
size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 10);
size = addCall(phoneNumbers, callDurations, size, "555-555-4321", 20);
size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 30);
System.out.println("\nPhone numbers (totalDurations initially):");
printList(phoneNumbers, callDurations, size);
totalDurations(phoneNumbers,callDurations,size);
// end of testing totalDurations
System.out.println("\nEnd 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;
}
/*
* method totalDurations that is passed parallel arrays of phoneNumbers and callDurations
* and number of records in the array
* 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.
*/
public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size)
{
String uniquePhoneNumbers[] = new String[size];
int totalDurations[] = new int[size];
int uniqueSize = 0;
// loop over the phone numbers array
for(int i=0;i<size;i++)
{
// check if phone number is present in uniquePhoneNumbers array
int idx =find(uniquePhoneNumbers,uniqueSize,0,phoneNumbers[i]);
// if phone number exists, update the totalDurations
if(idx != -1)
{
totalDurations[idx] += callDurations[i];
}else // if phone number doesn't exist, add a new entry
{
uniquePhoneNumbers[uniqueSize] = phoneNumbers[i];
totalDurations[uniqueSize] = callDurations[i];
uniqueSize++;
}
}
// print the arrays after the processing
System.out.println("\nPhone numbers (after totalDurations):");
printList(uniquePhoneNumbers,totalDurations,uniqueSize);
}
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.