Question

Modify the following code by JAVA language Write a static method totalDurations that is passed the...

Modify the following code by JAVA language

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), and555-1234 (duration 30), then the output would show 555-1234 (duration 40) and555-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.

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", 26);
size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);

System.out.println("Phone numbers (initially):");
printList(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;
}
}

Homework Answers

Answer #1

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

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

             System.out.println("Phone numbers (initially):");

             printList(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);

            

             // test totalDurations

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

             size = addCall(phoneNumbers, callDurations, size, "555-555-4321", 20);

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

             System.out.println("\nPhone numbers (totalDurations initially):");

             printList(phoneNumbers, callDurations, size);

                          

                          

             totalDurations(phoneNumbers,callDurations,size);

             // end of testing totalDurations

            

             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 totalDurations that is passed parallel arrays of phoneNumbers and callDurations

       * and number of records in the array

       * 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.

       */

       public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size)

       {

             String uniquePhoneNumbers[] = new String[size];

             int totalDurations[] = new int[size];

             int uniqueSize = 0;

            

             // loop over the phone numbers array

             for(int i=0;i<size;i++)

             {

                    // check if phone number is present in uniquePhoneNumbers array

                    int idx =find(uniquePhoneNumbers,uniqueSize,0,phoneNumbers[i]);

                    // if phone number exists, update the totalDurations

                    if(idx != -1)

                    {

                           totalDurations[idx] += callDurations[i];

                    }else // if phone number doesn't exist, add a new entry

                    {

                           uniquePhoneNumbers[uniqueSize] = phoneNumbers[i];

                           totalDurations[uniqueSize] = callDurations[i];

                           uniqueSize++;

                    }

             }

             // print the arrays after the processing

             System.out.println("\nPhone numbers (after totalDurations):");

             printList(uniquePhoneNumbers,totalDurations,uniqueSize);

       }

}

//end of program

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];...
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];       public int hash(int ele){        return ele % this.hashTable.length;    }    public boolean insert(int ele){        int pos = hash(ele);        System.out.println("Hashing key " + pos);        if(this.hashTable[pos] == 0)            this.hashTable[pos] = ele;        else{            for(int i = pos + 1; i < this.hashTable.length; i++){                if(this.hashTable[i]...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT