In: Computer Science
~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by defining a countOccurrences function, and by modifying the existing mostFrequentCharacter function, so as to satisfy the following specs:
IMPORTANT: you are NOT allowed to modify public static void main.
Initialize appropriately variables max_counter and max_char. For every letter in the string, count how many times it occurs in the string. If it occurs more times than max_counter, then update max_counter and max_char.
This is an example run of the complete program:
Enter some text, or q to quit: hello world Most frequent character: 'l' Number of occurrences of 'l': 3 Enter some text, or q to quit: 786768hjk jkh89' Most frequent character: '8' Number of occurrences of '8': 3 Enter some text, or q to quit: Q Exiting...
import java.util.Scanner;
public class FrequentCharacter
{
  public static char mostFrequentCharacter(String text)
  {
    int max_counter = 0;
    char max_char = 'a'; // the initial value of max_char makes no difference.
    
    for (int i = 0; i < text.length(); i++)
    {
      char current = text.charAt(i);
      int counter = countOccurrences(text, current);
      // You need to complete the code for this function.
    }
    return max_char;
  }
  
 \*IMPORTANT: you are NOT allowed to modify public static void main.*\ 
  public static void main(String[] args) 
  {
    Scanner in = new Scanner(System.in);
    
    while (true)
    {
      System.out.printf("Enter some text, or q to quit: ");
      String text = in.nextLine();
      if (text.toLowerCase().equals("q"))
      {
        break;
      }
      if (text.length() == 0)
      {
        break;
      }
      
      char c = mostFrequentCharacter(text);
      int number = countOccurrences(text, c);
      System.out.printf("Most frequent character: '%c'\n", c);
      System.out.printf("Number of occurrences of '%c': %d\n\n", c, number);
    }
    System.out.printf("Exiting...\n");
  }
}
import java.util.Scanner;
public class FrequentCharacter {
   public static char mostFrequentCharacter(String text)
{
       int max_counter = 0;
       char max_char = 'a'; // the initial
value of max_char makes no
          
           
        // difference.
       for (int i = 0; i <
text.length(); i++) {
           char current =
text.charAt(i);
           int counter =
countOccurrences(text, current);
           // if current
counter is greater than maxCounter
           // than make max
as current counter
          
if(counter>max_counter){
          
    max_char=current;
          
    max_counter=counter;
           }
           // You need to
complete the code for this function.
       }
       return max_char;
   }
   // iterating the string and checking with given
char
   // than increasing the counter
   private static int countOccurrences(String str, char
c) {
       int count = 0;
       for (int i = 0; i <
str.length(); i++)
           if
(str.charAt(i) == c)
          
    count++;
       return count;
   }
   /* IMPORTANT: you are NOT allowed to modify public
static void main. */
   public static void main(String[] args) {
       Scanner in = new
Scanner(System.in);
       while (true) {
          
System.out.printf("Enter some text, or q to quit: ");
           String text =
in.nextLine();
           if
(text.toLowerCase().equals("q")) {
          
    break;
           }
           if
(text.length() == 0) {
          
    break;
           }
           char c =
mostFrequentCharacter(text);
           int number =
countOccurrences(text, c);
          
System.out.printf("Most frequent character: '%c'\n", c);
          
System.out.printf("Number of occurrences of '%c': %d\n\n", c,
number);
       }
      
System.out.printf("Exiting...\n");
   }
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me