Question

IV. Grading 1. A program that does not run will receive 0 point. 2. There is...

IV. Grading 1. A program that does not run will receive 0 point. 2. There is a 10-20 points deduction for each major error, e.g., missing a word in the output. 3. There is a 1-9 points deduction for each minor error, e.g., incorrect format of output. 4. A program that does not follow the guidelines will lose 1-10 points. 5. Any type of cheating is not tolerated. You may be asked to explain your program in person. V. Bonus features (optional) You may implement as many bonus features as you want. If you implement any bonus feature in your program, please put a comment in your Blackboard submission and in your main Java file. In your comments, explain which bonus feature(s) you implemented. 1. (2 points) Your program will ignore the case of words, e.g., it will count “On” and “on” as the same word; 2. (8 points) Your program will also print the total occurrences of the words. For example, Display entries in ascending order of key a: 2 an: 2 eye: 2 fish: 1 fit: 1 hook: 2 into: 2 like: 1 me: 1 open: 1 you: 1 Total: 16

Homework Answers

Answer #1

Please find the program attached below. Here we have used a TreeMap because it orders the keys in the ascending order. Comments have been provided at each line for better understanding. Have a look at the program.

import java.util.*;

public class CountOccurences {
        public static void main(String[] args) {
                //enclose your input within quotes.
                String sentence = "Hi, this is a test sentence. THIS IS A TEST";
                showFrequency(sentence); //call the showFrequency method
        }
        public static void showFrequency(String sentence)
        {
                //tree map is created because it orders keys in ascending order
                TreeMap<String, Integer> map = new TreeMap<>(); 
                //remove all punctuation marks and split sentence on basis of whitespace
                String[] words = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); 
                //iterate through the array of words in the sentence
                for(String s : words)
                {
                        if(map.containsKey(s)) //if word is already present in the set
                        {
                                int oc = map.get(s); //get it's count
                                map.put(s, ++oc); //increment the count and put it back to the set
                        }
                        else
                        {
                                //if word was not already present, it is found first time.
                                map.put(s, 1); //put it with 1 frequency
                        }
                }
                //total number of words in the sentence is equal to the length of the words array
                int total = words.length;
                //iterate through the TreeSet using for each loop
                //entry set consists of both key and value
                for (Map.Entry<String,Integer> entry : map.entrySet())
                {
                        String word = entry.getKey(); //get word
                        int freq = entry.getValue(); //get it's frequency
                        System.out.print(word+": "+freq+" "); //print word and corresponding frequency
                }
                System.out.println("Total: "+total); //print total number of words in the sentence.
        }
}

Output:

Refer to below screenshot for an idea of indentation:

Please reach out for any other clarifications or doubts in the comments section. If you find this helpful, Please give an upvote. Thanks :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main()...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main() { ​cout << "Hello, world." << endl; } Open a Word Document and copy and paste the program and the output. 2. Create a new program: #include <iostream> using namespace std; int main() { ​float num1; ​float num2 ​float avg; ​cout << "Enter two numbers" << endl; ​cin >> num1 >> num2; ​avg = num1 + num2/2; ​cout << "The average is " <<...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
IMPORTANT INSTRUCTIONS 1. Read the Scenario below. Decide upon the appropriate content and structure you would...
IMPORTANT INSTRUCTIONS 1. Read the Scenario below. Decide upon the appropriate content and structure you would use to write an effective e-mail message to your employees. 2.(Do not repeat To, From, or Date.) Then, create a subject line that would be appropriate for your e-mail message based on the Scenario below. 3. Next, write the body of your message, following all of the message techniques we have discussed in class. At the bottom of the message, be sure to type...
Assignment goals • Build experience solving problems by implementing branch and loop algorithms. • Become able...
Assignment goals • Build experience solving problems by implementing branch and loop algorithms. • Become able to program using control structures. • Understand and implement string comparison in Java. • Develop skills required to write and debug Java programs. Description You program must start and keep dialog with the user. Please create the first prompt for the dialog. After that your program must accept the user’s response and echo it (output to screen) in upper case and adding the question...
§ Comment your code - you will be glad you did. You will be reusing code...
§ Comment your code - you will be glad you did. You will be reusing code throughout the semester. § You will demo each of your projects. In each demo, you will be asked to demonstrate the functionality of the program and possibly describe how some of the code works. Be prepared for this. § You will upload your projects on Blackboard. Details about this will be announced in class. § Late projects will lose points: 20% per day of...
2) Allowing (or requiring) users to use numerical digits (including 0) and one of 28 “special...
2) Allowing (or requiring) users to use numerical digits (including 0) and one of 28 “special characters” dramatically increases the number of possible passwords. Furthermore, passwords often are case-sensitive, effectively doubling the size of the alphabet by defining 52 distinct letter characters.    Assuming that passwords are case-sensitive and can include numerical digits and special characters, how many possible 6, 8 and 10 character passwords can be created? (8 points) 3) Allowing digits and special characters creates an enormous number...