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 =...
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...
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...
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,...
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;...
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;...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • For Parts #1 through #9, consider the following information: The superintendent’s office for the Jersey Dunes...
    asked 2 minutes ago
  • 1. Find the intersections and unions below, and give a proof for each. (a) ∩r∈(0,∞)[−r,r]. (b)...
    asked 12 minutes ago
  • Using the South University Online Library or the Internet, research a health issue in the world....
    asked 13 minutes ago
  • Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
    asked 37 minutes ago
  • Find a commercial, product, or advertisement and fill in the conditioning procedure. Unconditioned Stimulus → Unconditioned...
    asked 38 minutes ago
  • In a conversation with someone who you feel may have faced discrimination. Examples include someone with...
    asked 45 minutes ago
  • One measure of the meat quality of pigs is backfat thickness. Suppose two researchers, Jones and...
    asked 57 minutes ago
  • Polychlorinated biphenyls (PCBs), used in the manufacture of large electrical transformers and capacitors, are extremely hazardous...
    asked 58 minutes ago
  • 3. (10 marks) Describe a recursive algorithm for finding the maximum element in a array A...
    asked 1 hour ago
  • Three identical very small 50-kg masses are held at the corners of an equilateral triangle, 0,30m...
    asked 1 hour ago
  • C programming 1. Create a file function.c that contains a function called printNumbers() that prints the...
    asked 1 hour ago
  • You visit a local Starbucks to buy a Mocha Frappuccino. The barista explains that this blended...
    asked 1 hour ago