Question

Write a method called averageVowels that takes an ArrayList of strings as a parameter and returns...

Write a method called averageVowels that takes an ArrayList of strings as a parameter and returns the average number of vowel characters (a, e, i, o, u) in all Strings in the list. If your method is passed an empty ArrayList, it should return 0.0.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Description:

  1. Add some strings in the array list
  2. pass the array list to averageVowels method
  3. loop through the array list elements
  4. call countVowels method to calculate vowels in the string
  5. keep adding the returned number of vowels from countVowels method
  6. return the average by dividing the total number of vowels by total number of strings in the list

Code copiable:

import java.util.*;
public class AverageVowels {
  
    public static int countVowels(String str)
    {
        int numVowels = 0;
        char ch;
      
        // iterate through string character by character
        for(int i = 0; i < str.length(); i++)
        {
            // get one char using index
            ch = str.charAt(i);
          
            // check if character is vowel
            if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
            {
                //increment the count
                numVowels++;
            }
        }
      
        // return the count
        return numVowels;
    }
  
    public static int averageVowels(ArrayList arrList)
    {
        int numOfVowels = 0;
      
        // iterate throuugh Array List
        for(int i = 0; i < arrList.size(); i++)
        {
            // call method to calculate vowels in string and add
            numOfVowels += countVowels((String)arrList.get(i));
        }
      
        // return the average by dividing total number of vowels by total number of strings in array list
        return numOfVowels/arrList.size();
    }

    public static void main(String args[]) {
      // create an array list
      ArrayList arrList = new ArrayList();

      // add elements to the array list
      arrList.add("apple");
      arrList.add("fruit");
      arrList.add("Hello");
      arrList.add("aunt");
      arrList.add("uncle");
      arrList.add("pizza");
      arrList.add("byeeee");
    
      System.out.println("Average number of vowels: " + averageVowels(arrList));
   }
}

Code screenshots showing indentation:

Output:

Add a comment
Know the answer?
Add Answer to:
Write a method called averageVowels that takes an ArrayList of strings as a parameter and returns...
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
  • Write a method called removeDuplicates that takes as a parameter a sorted ArrayList of strings and...

    Write a method called removeDuplicates that takes as a parameter a sorted ArrayList of strings and eliminates any duplicates from the list. For example, if the list stores the values ["be", "be", "is", "not", "or", "question", "that", "the", "to", "to"] before the method is called, it should store the values ["be", "is", "not", "or", "question", "that", "the", "to"] after the method finishes executing. Because the values will be sorted, all of the duplicates will be grouped together. Assume that the...

  • Write a static method that takes an array of Strings and returns a double. Determine the...

    Write a static method that takes an array of Strings and returns a double. Determine the average number of characters for the Strings assigned to the array. Return the average.

  • 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)

  • 2a) Write a method countEvens that takes an ArrayList of String objects as input and returns...

    2a) Write a method countEvens that takes an ArrayList of String objects as input and returns the number of even length strings contained in the input. For example, if the input is [ one, peach, pear, plum ] then countEvents(inp) should return 2. 2b) Write a method, mirror, that doubles the size of a list of integers by appending a mirror image of the list. For example, given an array list containing [ 1, 5, 2, 6 ], the method...

  • (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an...

    (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a",...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Write a method called stutter that accepts an ArrayList of strings and an integer k as...

    Write a method called stutter that accepts an ArrayList of strings and an integer k as parameters and that replaces every string with k copies of that string. For example, if the list stores the values ["how", "are", "you?"] before the method is called and k is 4, it should store the values ["how", "how", "how", "how", "are", "are", "are", "are", "you?", "you?", "you?", "you?"] after the method finishes executing. If k is 0 or negative, the list should be...

  • This method takes an array of Strings as a parameter and has no return value. The...

    This method takes an array of Strings as a parameter and has no return value. The purpose of this method is to count all the characters in an array of strings. Count the number of characters in every string in the array and assign to numberChars. Count the number of lowercase letters in every string in the array and assign to numberLower. Count the number of uppercase letters in every string in the array and assign to numberUpper. Count characters...

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character...

    Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character objects of the same size. The returned elements of the ArrayList are assigned letter grade corresponding to integer grade of the same index element of the ArrayList parameter (A if 90 or above, F if less than 60). Include code to test your method. [For other letter grades: 80 ?? 89 −> ?, 70 ?? 79 −> ?, 60 ?? 69 −> ?]

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