Please write the following answer in JAVA (not javascript). Please provide answe
ID: 3861758 • Letter: P
Question
Please write the following answer in JAVA (not javascript). Please provide answer in copyable text. Show input and output. Thanks in advance.
S: Declare and populate an ArrayList with this set of words (as Strings) Twas brillig, and the slithy toves Did gyre and gimble in the wabe write a static method called averagevowels that returns the average number of vowels in each word of this ArrayList. Ifthis method is called with an empty ArrayList, return 0.0. If a null ArrayList is passed, return an IllegalArgumentException. Write a test case for the words above. No Printing in the method. This is worth 10 ptsExplanation / Answer
AverageVowelsTest.java
import java.util.ArrayList;
public class AverageVowelsTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Twas");
list.add("brilling");
list.add("and");
list.add("the");
list.add("slithy");
list.add("toves");
list.add("Did");
list.add("gyre");
list.add("and");
list.add("gimble");
list.add("int");
list.add("the");
list.add("wabe");
try{
System.out.println(averageVowels(list));
}
catch(IllegalArgumentException e){
System.out.println(e);
}
}
public static double averageVowels(ArrayList<String> list) throws IllegalArgumentException{
if(list == null){
throw new IllegalArgumentException("Arraylist is null which is invalid.");
}
else if(list.isEmpty()){
return 0.0;
}
else{
String vowels = "aeiouAEIOU";;
int total = 0;
for(String s: list){
for(int i=0; i<s.length(); i++){
if(vowels.contains(""+s.charAt(i))){
total++;
}
}
}
return total/(double)(list.size());
}
}
}
Output:
1.3076923076923077