functions

icon picker
length and round

counting the number of characters in text and controlling the number of decimal places

program requirement

The program should calculate the average length of sentences in a page of text. The user should be able to type in the number of sentences in the sample. The the program should allow each sentence to be entered one at a time. The program should count the number of characters in each sentence and use the data to calculate the total of characters in all sentences. Then the average should be calculated and displayed. The average should be rounded to 2 decimal places. Two text files are provided in the project with extracts from children’s stories that can be compared for average sentence length. The user can copy and paste sentences from the files into the program console. (The strange case of Dr Jekyll and Mr Hyde, Peter Pan)

learning point

using the length function to find the number of characters in a string
this result will include punctuation marks and spaces in the total, not just letters
using the round function to round a value to a set number of decimal places
rounding in java is a little complicated so
there are lots of other functions for rounding, only the basic round function is explored here

introducing the code

String text = "ABC";
int characters = text.length();

int totalcharacters = 1000;
int sentences = 33;
double averageLength = ((double)totalcharacters / (double)sentences);
averageLength = (Math.round(averageLength * 100.0) / 100.0);

System.out.println(characters);
System.out.println(averageLength);

> 3
> 30.30
understanding the code
length
the length of a string is simply the number of characters in the string
notice that the length will be an integer value
the length of the empty string “” is 0
length is very useful in processing text
round
Math.round(3.14) would be 3.0 and Math.round(1.62) would be 2.0 since the basic function rounds to the nearest whole number
the result is a real number (double)
in the example shown above includes data about 33 sentences which have a total of 1000 characters so the average length of a sentence would be 1000 divided by 33, try this on a calculator!
the results is 30.3030303 ......
to round to a specific number of decimal places a rather clunky formula is needed
the value is multiplied by a power of 10 and then divided by the same value
for example to round 30.3030303 to 2 decimal places
30.3030303 * 100 would be 3030.30303
then round would get 3030
then divide by 100 to get 30
to round to 3 decimal places: multiply by 1000, round the result then divide by 1000
to round to 1 decimal places: multiply by 10 round the result then divide by 10
rounding is important in programs that need to report statistics accurately
for example the average or mean of a list of numbers is very likely to produce a fraction which is typically rounded to 2 decimal places

developer task

First you need to be clear about how to use the demonstration program and to try it out.
You will find two story files in the project. View the contents of both files, they contain part of the first chapter of two well known books. Each text has about half a dozen sentences. The program can be used to find the average sentence length so that the stories can be compared.
The program will ask you how many sentences are to be processed for one story so count them.
Then you can copy and paste each sentence one by one and input the text. Copy each sentence carefully, exactly the characters in the sentence from first letter to full stop. Press enter to submit the sentence and then move on to the next sentence.
Once all the sentences are input the program will display the average sentence length.
Then repeat for the other story.

program code

Loading…

PRIMM

predict
This program is rather long to predict and anyway the notes have already explained what the output should be.

run
go ahead and run the program, enter the sentences of the first story.
run the program again this time for the second story, compare the average sentence length, what comments could you now make about the two stories?
try to find one of your own favourite books online and use the program to analyse the sentence length
what is the sentence length is you press enter without typing or pasting any text?
can you explain the result?
look at the results displayed by the program, these are called the actual output
check that the expected output is the same as the actual output

investigate
Here is the line that rounds the average to 2 decimal places
averageLength = (Math.round(averageLength * 100.0) / 100.0);
change both the 100.0 values to 1000.0 and run the program again, notice the result
change both the 1000.0 values to 10.0 and run the program again, notice the result
change both the 10.0 values back to to 100.0
now you know how to choose the number of decimal places

modify
You have already modified the code during the investigation!

make

Password apps will warn the user when the user tries to create a password that is too short.
Create a program that ask the user to enter a password.
The program should then display one of these messages:
your password is too short
your password is OK
your password is long enough to make it hard to guess
What variables will you need?
How will the program know how many characters are in the password?
How long should the password be for each message?
you will need to decide the threshold values that set a minimum or maximum length
How will you get the program to choose which message to display?
Make sure you text your program for short passwords, very long passwords and passwords with a length that is exactly on the threshold values

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.