Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Question
Book Icon
Chapter 2, Problem 35P
Program Plan Intro

StringTest class

Program plan:

  • Import the required java packages.
  • Create a class StringTest that contains a list of words and then returns the count of each word in the list.
  • In main() function,
    • Create the list of words.
  • Create the method countWords()
    • Create the object for hash map.
    • If the input word is not equal to null then separate the list of words using white spaces.
    • Execute for loop until it ends with list of words. Inside the loop,
      • Check whether the particular string is present in the hash map.
      • If yes, then start count of each word and increment the word count by 1 if the word is repeated again in the string.
      • Otherwise, remain with the word count as “1”.
      • Finally, print the count of each word in a string.

Blurred answer
Students have asked these similar questions
Write a JAVA program that checks phrases to determine if they are palindromes.  A palindrome is a word, phrase, or sentence that reads left-to-right the same way it reads right-to-left, ignoring all punctuation and capitalization.  For example, the statement “Madam, I’m Adam” is a palindrome as the response of “Eve.”  Use a method to determine if a String is a palindrome.  Test a single word non-palindrome, a single word palindrome, a phrase that is not a palindrome, and a phrase that is a palindrome.  Print the phrase and whether it is a palindrome or not. Some simple palindromes are “racecar,” “taco cat,” and “A man, a plan, a canal, Panama!”  There are a few different approaches for the test; choose one of your liking. Hint: extract all letters, change the remaining letters to lowercase, then test if the phrase is a palindrome.
Write a Java program to read the names of your friends, count the number of friends, identify the longest name and the number of letters in the longest name. Get the names of friends till 'stop' is entered. For example, if the input sequence is Ravi, Raj, Puela, stop, then the output is 3, Puela and 5.
You are learning how to repeat a set of actions, by using loops. There are several different kinds of loops, but the simplest are those that use a counter to control the number of repetitions. We call these counter-controlled loops, (or counted loops). The easiest way to write a counter-controlled loop is by using Java's for statement. The mechanics of using a for loop are the same as using a while loop, including: loop initialization loop condition loop update. The Java for statement provides a convenient syntax which includes all three of these elements in one statement.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education