Question

Write a method named avgLength which takes an array of Strings as an input parameter and...

Write a method named avgLength which takes an array of Strings as an input parameter and returns a double. The method should calculate and return the average length of all the strings in the array (in java langauge)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Method Implementation

//This method will find the average length of all strings in the string array

private static double avgLength(String[] str) {

double sum=0.0,avg=0.0;

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

{

//Calculating the sum of length of all strings in the array

sum+=str[i].length();

}

//Calculating the average

return sum/str.length;

}

_________________

CalAverageLengthOfArray.java(Complete Program)

public class CalAverageLengthOfArray {

public static void main(String[] args) {

String str[] = {
"Hello",
"Sachin",
"Tendulkar",
"Ricky",
"Pointing",
"Helen"
};

double average = avgLength(str);
System.out.printf("The average length of all the strings : %.2f", average);
}

//This method will find the average length of all strings in the string array
private static double avgLength(String[] str) {
double sum = 0.0, avg = 0.0;

for (int i = 0; i < str.length; i++) {
//Calculating the sum of length of all strings in the array
sum += str[i].length();
}
//Calculating the average
return sum / str.length;
}

}

__________________

Output:

The average length of all the strings : 6.33

_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
Write a method named avgLength which takes an array of Strings as an input parameter and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT