Only using lists instead of bags. Write JAVA statements from the client perspect
ID: 667399 • Letter: O
Question
Only using lists instead of bags. Write JAVA statements from the client perspective to accomplish the following task.
You are given two ListInterface<String> objects called letters and vowels.
letters contains one-letter strings.
vowels is initially empty.
Remove the Strings from letters.
If the String is a vowel, add it to the vowels list.
Otherwise, discard the String.
Once the letters list is empty, print out the number of vowels that are in the vowels list and the number of times each vowel appears in the list .
For example, if letters initially contained
a, f, v, q, e, a, o, g, y, h, e, q
Then after your method completes, letters will be empty and vowels will contain:
a, e, a, o, e
And your program will output:
There are five vowels in the list:
2 a's
2 e's
0 i's
1 o
0 u's
You can use a second, initially empty ListInterface<String> object, called tempList, if it helps.
#do not use the toArray() method.
Explanation / Answer
import java.util.*;
public class Vowels {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> letter = Arrays.asList("a", "f", "v", "q", "e", "a", "o", "g", "y", "h", "e", "q");
ArrayList<String> Vowel = new ArrayList<String>();
for(String s: letter){
if ("aeiouAEIOU".indexOf(s) != -1){
Vowel.add(s);
// System.out.println(s);
}
}
int i;
for(i=0; i<letter.size();i++){
letter.set(i, null);
}
for(i=0; i<Vowel.size();i++){
System.out.println(Vowel.get(i));
}
for(String s: Vowel){
System.out.println(s + " " +Collections.frequency(Vowel,s));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.