Download the complete Caller ID program. Write a static method totalDurations th
ID: 3870689 • Letter: D
Question
Download the complete Caller ID program. 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 You will need to modify the main program to adequately test your new method. But you should submit only your totalDurations method for marking, by the due date specified in the course schedule.Explanation / Answer
Please find attached code and its output.
======ONLY METHOD===
public static void totalDurations(String[] phoneNumbers, int[] durations, int size) {
Map<String, Integer> mapOfNumToDur = new HashMap<String, Integer>();
for (int i = 0; i < size; i++) {
if (mapOfNumToDur.containsKey(phoneNumbers[i])) {
int newDur = (int) mapOfNumToDur.get(phoneNumbers[i]) + durations[i];
mapOfNumToDur.put(phoneNumbers[i], newDur);
} else {
mapOfNumToDur.put(phoneNumbers[i], durations[i]);
}
}
for (Map.Entry<String, Integer> entry : mapOfNumToDur.entrySet()) {
System.out.println(entry.getKey() + "(duration " + entry.getValue()+")");
}
}
==WHOLE CODE==
import java.util.HashMap;
import java.util.Map;
/*
* Created on Sep 27, 2017
*
* $Id: CodeTemplates.xml,v 1.1 2012/11/29 19:54:11 adets Exp $
*
* Copyright 2017 InfoDesk, Inc. All rights reserved.
*/
public class Activity0A {
public static void main(String args[]) {
String[] phoneNumbers;
phoneNumbers = new String[100];
int size = 0;
phoneNumbers[0] = "555-555-5555";
phoneNumbers[1] = "555-555-1234";
phoneNumbers[2] = "555-555-9876";
size = 3;
System.out.println("Phone numbers (initially):");
printList(phoneNumbers, size);
phoneNumbers[size] = "555-555-0000";
size++;
phoneNumbers[size] = "555-555-1234";
size++;
phoneNumbers[size] = "555-555-0000";
size++;
System.out.println(" Phone numbers (after):");
printList(phoneNumbers, size);
System.out.println(" Call Duration: ");
int[] durations = new int[100];
durations[0] = 10;
durations[1] = 20;
durations[2] = 30;
durations[3] = 40;
durations[4] = 5;
durations[5] = 15;
totalDurations(phoneNumbers, durations, size);
System.out.println(" End of processing. ");
}
public static void printList(String[] phoneNumbers, int size) {
for (int i = 0; i < size; i++) {
System.out.println(phoneNumbers[i]);
}
}
public static void totalDurations(String[] phoneNumbers, int[] durations, int size) {
Map<String, Integer> mapOfNumToDur = new HashMap<String, Integer>();
for (int i = 0; i < size; i++) {
if (mapOfNumToDur.containsKey(phoneNumbers[i])) {
int newDur = (int) mapOfNumToDur.get(phoneNumbers[i]) + durations[i];
mapOfNumToDur.put(phoneNumbers[i], newDur);
} else {
mapOfNumToDur.put(phoneNumbers[i], durations[i]);
}
}
for (Map.Entry<String, Integer> entry : mapOfNumToDur.entrySet()) {
System.out.println(entry.getKey() + "(duration " + entry.getValue()+")");
}
}
}
===O/P==
Phone numbers (initially):
555-555-5555
555-555-1234
555-555-9876
Phone numbers (after):
555-555-5555
555-555-1234
555-555-9876
555-555-0000
555-555-1234
555-555-0000
Call Duration:
555-555-1234(duration 25)
555-555-9876(duration 30)
555-555-0000(duration 55)
555-555-5555(duration 10)
End of processing.
==Let me know in case of any issues.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.