Welcome to Westonci.ca, the Q&A platform where your questions are met with detailed answers from experienced experts. Connect with a community of experts ready to help you find solutions to your questions quickly and accurately. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

Part A. Write a method countOfLongest that accepts an array of Strings as a parameter and returns the count (int) of elements in that array with length equal to the longest String in that array. See two examples above. Part B. Write a method countOfLongest that accepts an array of int's as a parameter and returns the count (int) of elements in that array with number of digits equal to the largest int in that array. See two examples above.

Sagot :

Answer:

The methods are as follows:

public static int countOfLongest(String [] myarray){

    int strlen = 0, kount = 0;

    for(int i = 0; i<myarray.length;i++){

        if(myarray[i].length() > strlen){

            strlen = myarray[i].length();         }     }

    for(int i = 0; i<myarray.length;i++){

        if(myarray[i].length() == strlen){

            kount++;         }     }

    return kount; }

public static int countOfLongest(int [] myarray){

    int diglen = 0, kount = 0;

    for(int i = 0; i<myarray.length;i++){

    int lengt = String.valueOf(myarray[i]).length();

        if(lengt > diglen){

            diglen = lengt;         }     }

    for(int i = 0; i<myarray.length;i++){

        int lengt = String.valueOf(myarray[i]).length();

        if(lengt == diglen){

            kount++;         }     }

    return kount; }

Explanation:

This defines the countOfLongest of the string array

public static int countOfLongest(String [] myarray){

This initializes the length of each string and the kount of strings with that length to 0

    int strlen = 0, kount = 0;

This iterates through the array

    for(int i = 0; i<myarray.length;i++){

The following if condition determines the longest string

        if(myarray[i].length() > strlen){

            strlen = myarray[i].length();         }     }

This iterates through the array, again

    for(int i = 0; i<myarray.length;i++){

The following if condition checks for the count of strings with the longest length

        if(myarray[i].length() == strlen){

            kount++;         }     }

This returns the calculated kount

    return kount; }

This defines the countOfLongest of the string array

public static int countOfLongest(int [] myarray){

This initializes the length of each integer and the kount of integer with that length to 0

    int diglen = 0, kount = 0;

This iterates through the array

    for(int i = 0; i<myarray.length;i++){

The calculates the length of each integer

    int lengt = String.valueOf(myarray[i]).length();

The following if condition determines the longest integer

        if(lengt > diglen){

            diglen = lengt;         }     }

This iterates through the array, again

    for(int i = 0; i<myarray.length;i++){

The calculates the length of each integer

        int lengt = String.valueOf(myarray[i]).length();

The following if condition checks for the count of integer with the longest length

        if(lengt == diglen){

            kount++;         }     }

This returns the calculated kount

    return kount; }

See attachment for complete program which includes main

View image MrRoyal