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


Sign In
Create Account

Back to top









