Question
Chapter 9. PC #2. Word Counter (page 608) Write a (JAVA) method that accepts a String object as an argument and returns the number of
Chapter 9. PC #2. Word Counter (page 608)
Write a (JAVA) method that accepts a String object as an argument and returns the number of words it contains. For instance, if the argument is Four score and seven years ago the
method should return the number 6. Demonstrate the method in a program that asks the user to input a string and then passes it to the method. The number of words in the string should be displayed on the screen.
View required output
Question 2 100 points
Chapter 9. PC #8. 8. Sum of Numbers in a String (page 610)
Write a program that asks the user to enter a series of numbers separated by commas. Here are two examples of valid input:
7,9,10,2,18,6
8.777,10.9,11,2.2,-18,6
The program should calculate and display the sum of all the numbers.
Input Validation
1. Make sure the string only contains numbers and commas (valid symbols are '0'-'9', '-', '.', and ',').
2. If the string is empty, a messages should be displayed, saying that the string is empty.
View required output
Test Case 1
Standard Input |
---|
7,9,10,2,18,6ENTER ENTER 8.777,10.9,11,2.2,-18,6ENTER quitENTER
|
Output: Enter a series of numbers separated only by commas or type QUIT to exit: The sum of those numbers is 52.00 Enter a series of numbers separated only by commas or type QUIT to exit: Empty string. Enter a series of numbers separated only by commas or type QUIT to exit: The sum of those numbers is 20.88 Enter a series of numbers separated only by commas or type QUIT to exit:
Test Case 2
Standard Input |
---|
invalid_input_hereENTER 1,2,3ENTER quitENTER |
Output: Enter a series of numbers separated only by commas or type QUIT to exit: Invalid input. Enter a series of numbers separated only by commas or type QUIT to exit: The sum of those numbers is 6.00 Enter a series of numbers separated only by commas or type QUIT to exit:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started