Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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", 28);
- size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 12);
- size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 12);
- size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
- size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
- size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);
- size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 382);
- System.out.println("Phone numbers (initially):");
- printList(phoneNumbers, callDurations, size);
- /* Calling totalDurations function */
- 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("\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;
- }
- /* Method that finds total duration of each phone call */
- public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size)
- {
- int newSize = 0, pos, i;
- //New parallel arrays
- String[] uPhoneNumbers = new String[size];
- int[] uCallDurations = new int[size];
- //Iterating over old list
- for(i=0; i<size; i++)
- {
- //Identifying position of phone number in new list
- pos = find(uPhoneNumbers, newSize, 0, phoneNumbers[i]);
- //If not found ==> New Entry
- if(pos == -1)
- {
- //Adding to new list
- uPhoneNumbers[newSize] = phoneNumbers[i];
- uCallDurations[newSize] = callDurations[i];
- //Incrementing size
- newSize++;
- }
- else
- {
- //Updating duration
- uCallDurations[pos] = uCallDurations[pos] + callDurations[i];
- }
- }
- //Printing list
- System.out.println("\n Total Durations: \n");
- printList(uPhoneNumbers, uCallDurations, newSize);
- System.out.println("\n\n\n");
- }
- }
- _______________________________________________________________________________________________
- Sample Output:
- https://d2vlcm61l7u1fs.cloudfront.net/media%2F8d8%2F8d8d6c1a-b2ee-4313-afd0-c5ed11bfc4f2%2Fphpo6teZC.png
Advertisement
Add Comment
Please, Sign In to add comment