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
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...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25 points) // Note: You may notice some warnings for variables when you compile in GCC, that is okay. #define macro_1(x) ((x > 0) ? x : 0) #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10) int function_1(int a, int b) { return (3*a - 3*b*b + 4*a * b - a*b * 10); } // Part 1: //...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Month Machine Hours (hrs.) Maintenance Costs ($) 1 1,330 102,694 2 1,400 103,694 3 1,500 108,694...
Month Machine Hours (hrs.) Maintenance Costs ($) 1 1,330 102,694 2 1,400 103,694 3 1,500 108,694 4 1,470 108,694 5 1,620 116,694 6 1,690 115,694 7 1,490 107,694 8 1,310 102,694 9 1,450 106,694 10 1,580 113,694 11 1,300 100,694 12 1,600 113,694 13 1,650 114,694 14 1,440 109,694 15 1,340 102,694 16 1,670 114,694 17 1,480 106,694 18 1,360 103,694 19 1,340 103,694 20 1,540 112,694 Assume that the following relationship holds: Maintenance Costs = (v * Machine Hours)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT