java . Linear Search - countLetter For this exercise, you will write a method th
ID: 3819385 • Letter: J
Question
java
. Linear Search - countLetter
For this exercise, you will write a method that implements a linear search. To get started, download the class Main.java. Update the file by replacing the /* missing code */ with your own implementation of the method countLetter.
countLetter accepts as input an ArrayList of Strings and a letter, stored in a String. The method returns the number of Strings in the input ArrayList that start with the given letter. Your implementation should use a linear search algorithm that ignores the case of the Strings in the ArrayList.
Test your code by running the main method and verifying that your output matches the Sample Run that follows.
Sample Run
Submission
When you are done coding and testing, copy and paste your entire Main class into the Code Runner and press "Run" to submit the exercise. Use the "Check Answer" button to get additional feedback on your work.
Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
public static void main(String []args){
List<String> x=new ArrayList<String>();
x.add("Zebra");
x.add("Aardvark");
x.add("Emu");
x.add("Hippo");
x.add("Alligator");
x.add("Lion");
x.add("Giraffe");
x.add("Seal");
x.add("Tiger");
x.add("Elephant");
char search='A';
int count=0;
for (int i = 0; i < x.size(); i++) {
char[] words=x.get(i).toCharArray();
for(int j=0;j<words.length;j++){
if(words[j]==search)
{
count++;
}
}
}
System.out.println(count);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.