jaredec18

Untitled

Sep 9th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. public class Activity0D {
  2. public static void main(String[] args) {
  3. String[] phoneNumbers = new String[100];
  4. int[] callDurations = new int[phoneNumbers.length];
  5. int size = 0;
  6.  
  7. size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
  8. size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
  9. size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 28);
  10. size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 12);
  11. size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 12);
  12. size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
  13. size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
  14. size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);
  15. size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 382);
  16.  
  17. System.out.println("Phone numbers (initially):");
  18. printList(phoneNumbers, callDurations, size);
  19.  
  20. /* Calling totalDurations function */
  21. totalDurations(phoneNumbers, callDurations, size);
  22.  
  23. size = removeCall(phoneNumbers, callDurations, size, 1); // middle
  24. size = removeCall(phoneNumbers, callDurations, size, size - 1); // last
  25. size = removeCall(phoneNumbers, callDurations, size, 0); // first
  26.  
  27. System.out.println("\nPhone numbers (after):");
  28. printList(phoneNumbers, callDurations, size);
  29.  
  30. size = removeCall(phoneNumbers, callDurations, size, 0);
  31.  
  32. System.out.println("\nPhone numbers (none left):");
  33. printList(phoneNumbers, callDurations, size);
  34.  
  35. System.out.println("\nEnd of processing.");
  36. }
  37.  
  38. public static int addCall(String[] phoneNumbers, int[] callDurations, int size, String newNumber, int newDuration) {
  39. if (size >= phoneNumbers.length) {
  40. System.out.println("Error adding " + newNumber + ": array capacity exceeded.");
  41. } else {
  42. phoneNumbers[size] = newNumber;
  43. callDurations[size] = newDuration;
  44. size++;
  45. }
  46.  
  47. return size;
  48. }
  49.  
  50. public static void printList(String[] phoneNumbers, int[] callDurations, int size) {
  51. for (int i = 0; i < size; i++) {
  52. System.out.println(phoneNumbers[i] + " duration: " + callDurations[i] + "s");
  53. }
  54. }
  55.  
  56. public static int find(String[] list, int size, int start, String target) {
  57. int pos = start;
  58.  
  59. while (pos < size && !target.equals(list[pos])) {
  60. pos++;
  61. }
  62.  
  63. if (pos == size)
  64. pos = -1;
  65.  
  66. return pos;
  67. }
  68.  
  69. public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
  70. int matchPos;
  71.  
  72. System.out.println("Calls from " + targetNumber + ":");
  73. matchPos = find(phoneNumbers, size, 0, targetNumber);
  74. while (matchPos >= 0) {
  75. System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");
  76.  
  77. // Find the next match, starting after the last one
  78. matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
  79. }
  80. }
  81.  
  82. public static int removeCall(String[] phoneNumbers, int[] callDurations, int size, int posToRemove) {
  83. for (int i = posToRemove + 1; i < size; i++) {
  84. phoneNumbers[i - 1] = phoneNumbers[i];
  85. callDurations[i - 1] = callDurations[i];
  86. }
  87. size--;
  88.  
  89. return size;
  90. }
  91.  
  92. /* Method that finds total duration of each phone call */
  93. public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size)
  94. {
  95. int newSize = 0, pos, i;
  96.  
  97. //New parallel arrays
  98. String[] uPhoneNumbers = new String[size];
  99. int[] uCallDurations = new int[size];
  100.  
  101. //Iterating over old list
  102. for(i=0; i<size; i++)
  103. {
  104. //Identifying position of phone number in new list
  105. pos = find(uPhoneNumbers, newSize, 0, phoneNumbers[i]);
  106.  
  107. //If not found ==> New Entry
  108. if(pos == -1)
  109. {
  110. //Adding to new list
  111. uPhoneNumbers[newSize] = phoneNumbers[i];
  112. uCallDurations[newSize] = callDurations[i];
  113.  
  114. //Incrementing size
  115. newSize++;
  116. }
  117. else
  118. {
  119. //Updating duration
  120. uCallDurations[pos] = uCallDurations[pos] + callDurations[i];
  121. }
  122. }
  123.  
  124. //Printing list
  125. System.out.println("\n Total Durations: \n");
  126. printList(uPhoneNumbers, uCallDurations, newSize);
  127. System.out.println("\n\n\n");
  128. }
  129. }
  130.  
  131. _______________________________________________________________________________________________
  132.  
  133. Sample Output:
  134.  
  135. https://d2vlcm61l7u1fs.cloudfront.net/media%2F8d8%2F8d8d6c1a-b2ee-4313-afd0-c5ed11bfc4f2%2Fphpo6teZC.png
Advertisement
Add Comment
Please, Sign In to add comment