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

File task9.java contains an incomplete program. : Complete that program, by defi

ID: 3674401 • Letter: F

Question

File task9.java contains an incomplete program. :

Complete that program, by defining a count_occurrences function, and by modifying the existing most_frequent_character function, so as to satisfy the following specs:

Function count_occurrences takes two arguments, a string and a character. It returns the number of times the character occurs in the string.

Function most_frequent_character takes a string as an argument. It returns the character that occurs the most times in the string. If multiple characters tie for occurring the most times, the function can return any one of those characters tying for most occurrences.

IMPORTANT: you are NOT allowed to modify in any way the main function. HINT: 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...

Explanation / Answer

import java.util.Scanner;

public class task9
{
   // function that returns number of occurence of c in s
public static int count_occurrences(String s, char c){
   int counter = 0;
   for( int i=0; i<s.length(); i++ ) {
       if( s.charAt(i) == c ) {
       counter++;
       }
       }
   return counter;
}
public static char most_frequent_character(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 = count_occurrences(text, current);
System.out.println("Current: "+current+" Counter:"+counter);
if(counter > max_counter){
   max_counter = counter;
   max_char = current;
}

}
return max_char;
}
  
  
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 = most_frequent_character(text);
int number = count_occurrences(text, c);
System.out.printf("Most frequent character: '%c' ", c);
System.out.printf("Number of occurrences of '%c': %d ", c, number);
}
System.out.printf("Exiting... ");
}
}

/*

Output:

Enter some text, or q to quit: praveshkumar
Current: p Counter:1
Current: r Counter:2
Current: a Counter:2
Current: v Counter:1
Current: e Counter:1
Current: s Counter:1
Current: h Counter:1
Current: k Counter:1
Current: u Counter:1
Current: m Counter:1
Current: a Counter:2
Current: r Counter:2
Most frequent character: 'r'
Number of occurrences of 'r': 2

Enter some text, or q to quit: yatraonlinepvtltd
Current: y Counter:1
Current: a Counter:2
Current: t Counter:3
Current: r Counter:1
Current: a Counter:2
Current: o Counter:1
Current: n Counter:2
Current: l Counter:2
Current: i Counter:1
Current: n Counter:2
Current: e Counter:1
Current: p Counter:1
Current: v Counter:1
Current: t Counter:3
Current: l Counter:2
Current: t Counter:3
Current: d Counter:1
Most frequent character: 't'
Number of occurrences of 't': 3

Enter some text, or q to quit: q
Exiting...

*/

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