Jump to content

Call ID Program, Need to consolidate all times for specific number into one entry

- - - - -

  • Please log in to reply
No replies to this topic

#1
Godlymonkey

Godlymonkey

    Newbie

  • Members
  • Pip
  • 1 posts
Hey guys I need some help, I have this java exercise, I need to write a method that consolidates all the time in a caller ID program so that there is only one number entry for every number EXAMPLE 222-22 2-2222 (40sec) + 222-22 2-1111 (10sec) + 222-22 2-222(60sec) Into 222-22 2-2222(100sec ) + 22-22 2-1111(10) The Method I wrote was totalDurations, can you guys see what Im doing wrong


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-0000", 12);

      size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);

      size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);

      printList(phoneNumbers,callDurations,size);

      totalDurations(phoneNumbers,callDurations,size);

   }


   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 totalDurations(String[] phoneNumbers, int[] callDurations, int size){

      String[] newphoneNumbers = new String[phoneNumbers.length];

      int[] newcallDurations = new int[newphoneNumbers.length];

      int newsize = 0;

      int matchPos = 0;

      int newpos = 0;

      String target = phoneNumbers[0];

      int target1 = 0;

      matchPos = find(phoneNumbers, size, 0, target);

      while (matchPos >= 0){

      phoneNumbers[matchPos] = target;

      callDurations[matchPos] = target1;

      newpos = find(newphoneNumbers,size,0,target);

      if (newpos >= 0) {

      newcallDurations[newpos] += callDurations[matchPos];

      matchPos = find(phoneNumbers, size, matchPos + 1, target);

      }

      else{

      newsize = addCall(newphoneNumbers, newcallDurations, size, target,target1);

      matchPos = find(phoneNumbers, size, matchPos + 1, target);

      }

      }

      

   }

}

Edited by Roger, 17 September 2011 - 07:11 AM.
added code tags





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users