Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete JAVA method called countWordsThatDoNotContainChar. The method:

ID: 3594961 • Letter: W

Question

Write a complete JAVA method called countWordsThatDoNotContainChar. The method:

takes a String array and a char as a parameter

returns an int that is the number of Strings in the array that do NOT contain that char

For example, if you invoke the method with {"hello", "there", "bye"} and 'q', the method will return 3, because all three words do not contain the letter z. If you invoke with the same array and 'h' the method will return 1, because "bye" does not contain the letter h, but "hello" and "there" do contain the letter h.

For full credit, use good principles of code design. When possible, use existing methods!

Explanation / Answer

import java.io.*;

public class Demo6{

   public static int countWordsThatDoNotContainChar(String[] s,char a){

       int count = 0;
       for (int i = 0; i<s.length; i++){
           if (s[i].indexOf(a) < 0)
              count++;
       }
       return count;
   }

   public static void main(String[] args){
       String[] s = new String[3];
       s[0] = "hello";
       s[1] = "there";
       s[2] = "bye";
       System.out.println(countWordsThatDoNotContainChar(s,'q'));
       System.out.println(countWordsThatDoNotContainChar(s,'h'));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote