Question

In: Computer Science

Instructions Today, all input and output has been taken care of for you. All you need...

Instructions

Today, all input and output has been taken care of for you. All you need to do is finish off the function wordCount that calculates the number of words in a string.

Function Details

Input

  • Input has been handled for you. A string will be read in.

Processing

  • Complete the wordCount function. Note that you have been given Java comments describing its expected parameters and return value.
/**
 * wordCount (String) -> int
 * Calculates the number of words in a given string
 * @param ?? (str): String of words
 * @return Number of words in the string (int)
 */
  • The method takes in a string. It then calculates the the number of words in that string and returns that value.

    HINT: This can be done by first splitting the string into individual words using the String.split() method. Then you just need to count how many objects the array holds (and there is an array method for that as well!

Output

  • Output has also been taken care of for you.

Sample input/output:

use JAVA method complete this question

Input Output
I have really enjoyed teaching this class and all of you!

Word count: 11

I am designing a test and do not want to get bogged down in what the text actually says.

Word count: 19

Solutions

Expert Solution

Program

filename: WordCount.java

public class WordCount{

/**
* wordCount (String) -> int
* Calculates the number of words in a given string
* @param inputString String of words
* @return Number of words in the string (int)
*/
public static int wordCount(String inputString) {
int count = 0;
int inWord = 0;
for (char letter : inputString.toCharArray()) {
  
//non-space means were "in a word"
if (letter != ' ') {
inWord = 1;
} else {
// space is the delimiter, but we only want to count a word if
// we were previously "in a word"
if (inWord == 1) {
count++;
inWord = 0;
}
}
}
// Just in case we were "in a word," but never encountered another space
if (inWord == 1) {
count++;
}
return count;
}

public static void main(String args[]){
String input = "I have really enjoyed teaching this class and all of you!";
System.out.println("wordCount(\"I have really enjoyed teaching this class and all of you!\") : " + wordCount(input));
}
}

Output

==========================================================================================

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.


Related Solutions

In this assignment, you need to demonstrate your ability in using input, output, data types, and...
In this assignment, you need to demonstrate your ability in using input, output, data types, and if statement in C++ program. Assume that you need write a C++ program for a cash register. There are only four items in the store: Cereal, $3.99 Milk, $3.99 Egg, $0.25 Water, $ 1.50 Once a customer purchases items, you will ask her/his how many of them are bought. The quantity can be in the range of 0-10 (including 0 and 10). Then, calculate...
You can use any language. You have to provide all the data, output and input. You...
You can use any language. You have to provide all the data, output and input. You are to use Linked Lists to do this Program. The XYZ Widget store receives shipments of widgets at various costs. The store’s policy is to charge a 30% markup, and to sell widgets which were received earlier before widgets which were received later. This is called a FIFO policy. Write a program using linked lists that reads in three types of input data and...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to analyze the information below from BOTH the management AND the employee perspective. This information pertains to a labor union in a simulated/made up/not real firm in Glen Ellyn. The first part of your information relates to Management – the second part relates to the Labor Union employees. I have provided you with information from the last union negotiations at the plant in 2016. It...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
there are files called LinkedList.h, and Array.h and all the functionality you need has already been...
there are files called LinkedList.h, and Array.h and all the functionality you need has already been implemented. There is also a file called TimeSupport.h that allows you to measure the running time of code segments. There is also a file called RandomSupport.h, which you can use to generate good random numbers. In your app.cpp , create a demo illustrating a scenario where storing numbers in a linked list is more efficient than an array. Your demo should generate a sufficiently...
"It has been contended that all contracts must be agreements but not all agreements need to...
"It has been contended that all contracts must be agreements but not all agreements need to be contracts" Comment on the validity of this statement. Support with references.
Need this program in python. The data must be taken from user as input. Write a...
Need this program in python. The data must be taken from user as input. Write a program that prompts the user to select either Miles-to-Kilometers or Kilometers-to-Miles, then asks the user to enter the distance they wish to convert. The conversion formula is: Miles = Kilometers X 0.6214 Kilometers = Miles / 0.6214 Write two functions that each accept a distance as an argument, one that converts from Miles-to-Kilometers and another that converts from Kilometers-to-Miles The conversion MUST be done...
A repairman has 20 jobs that need to be completed today. Usually, 70% of all jobs...
A repairman has 20 jobs that need to be completed today. Usually, 70% of all jobs are fairly straightforward, so that the time it takes to complete them is well-described by normal distribution with average 10 minutes and standard deviation 1 minute. The rest are challenging jobs, so that completion time is well-modeled with exponential distribution with average 1 hour. i. If she starts at 8am, what is the expected time when she will finish all already assigned jobs? ii....
Module/Week 2 ASSIGNMENT (INPUT/OUTPUT) The number of permutations of a set of n items taken r...
Module/Week 2 ASSIGNMENT (INPUT/OUTPUT) The number of permutations of a set of n items taken r at a time is given by the following formulan!/r !(n- r )!: where n! is the factorial of n, r! is the factorial of r, and (n-r)! is the factorial of the result of n-r. The factorial of a number n can be solved using the following formula: n!=e-n nn √ 2πn. If there are 18 people in your class and you want to...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT