Question

In: Computer Science

How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...

How to write Method in Java: public static in firstLetterFreq(char letter, String[] words]

{

// it returns 0 if (words == null OR words.length == 0)

// returns number of times letter is the first letter of a String in words array

// use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case

}

Solutions

Expert Solution

Explanation:I have written the method firstLetterFreq() and have also provided a demo code with main method to show the output of the program, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//method

public static int firstLetterFreq(char letter, String[] words)
   {
       int frequency = 0;
       if (words == null || words.length == 0)
           return 0;
       for (int i = 0; i < words.length; i++)
       {
           if (String.valueOf(words[i].charAt(0)).equalsIgnoreCase(String.valueOf(letter)))
           {
               frequency++;
           }
       }
       return frequency;
   }

//Demo code with main method


public class LetterFrequency
{

   public static int firstLetterFreq(char letter, String[] words)
   {
       int frequency = 0;
       if (words == null || words.length == 0)
           return 0;
       for (int i = 0; i < words.length; i++)
       {
           if (String.valueOf(words[i].charAt(0)).equalsIgnoreCase(String.valueOf(letter)))
           {
               frequency++;
           }
       }
       return frequency;
   }

   public static void main(String[] args)
   {
       String words[] =
       { "hello", "hi", "How", "Are", "You", "Hannah" };
       char letter = 'h';
       int frequency = firstLetterFreq(letter, words);
       System.out.println("The frequency of letter " + letter + " ignoring the case is: " + frequency);

   }

}

Output:


Related Solutions

Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
How would you write the following recursively? This is in Java.    public static String allTrim(String...
How would you write the following recursively? This is in Java.    public static String allTrim(String str) { int j = 0; int count = 0; // Number of extra spaces int lspaces = 0;// Number of left spaces char ch[] = str.toCharArray(); int len = str.length(); StringBuffer bchar = new StringBuffer(); if (ch[0] == ' ') { while (ch[j] == ' ') { lspaces++; j++; } } for (int i = lspaces; i < len; i++) { if (ch[i]...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { }...
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { } The method accepts a String parameter, inputStr, and a char parameter, target. It returns the result string consisting of all the characters in inputStr which match the target character (matching is case sensitive, for example, 'c' does not match "C"). If the characters match, the character is appended to the result String. After all the characters in inputStr have been compared, the method returns...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT