Question

In: Computer Science

~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by...

~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:
  • Function countOccurrences takes two arguments, a string and a character. It returns the number of times the character occurs in the string.
  • Function mostFrequentCharacter 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.
  • Upper-case letters should be treated as different from lower-case letters. Character 'A' is NOT the same as character 'a'.
  • If there is a tie, and multiple characters appear the maximum amount of times, then the function can return any of those character that appear a maximum amount of times. Any of those return values will be considered correct.

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");
  }
}

Solutions

Expert Solution

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


Related Solutions

Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
write a java program that allows main to work public static void main(String[] args) { /*...
write a java program that allows main to work public static void main(String[] args) { /* Start with the empty list. */ LinkedList list = newLinkedList(); // // ******INSERTION****** // // Insert the values deleteFront(list); deleteBack(list); list = insert(list, 1); list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list = insert(list, 7); list = insert(list, 8);    // Basic Operations on the LinkedList printList(list); insertFront(list,0); printList(list); insertBack(list,999); printList(list);...
could you do all the challenges? public class Main { public static void main(String[] args) {...
could you do all the challenges? public class Main { public static void main(String[] args) { // declare an array so you can easily use them under // one name //ex1 array of test score 0-100 int[] testScores = new int[100]; //ex2 array of 200 gpa double[] gpa = new double[200]; //ex3 50 element array of age int[] age; //1 - declares age array age = new int[50]; //2 - instantiates age array age[0] = 10; //3 - put initial...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
Could you do it with python please? public class BSTTraversal {         public static void main(String[]...
Could you do it with python please? public class BSTTraversal {         public static void main(String[] args) {                 /**                  * Creating a BST and adding nodes as its children                  */                 BSTTraversal binarySearchTree = new BSTTraversal();                 Node node = new Node(53);                 binarySearchTree.addChild(node, node, 65);                 binarySearchTree.addChild(node, node, 30);                 binarySearchTree.addChild(node, node, 82);                 binarySearchTree.addChild(node, node, 70);                 binarySearchTree.addChild(node, node, 21);                 binarySearchTree.addChild(node, node, 25);                 binarySearchTree.addChild(node, node, 15);                 binarySearchTree.addChild(node, node, 94);                 /**         ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT