Write your name or username here: _______________________

Do this when you receive the paper.

Write a program that counts the number of words in a sentence. It should also report the average number of words per sentence as you type (by taking into account the lengths of all sentences typed thus far). The input is a String (a line of text entered by the user). Feel free to use ConsoleReader to read lines from the user.

Here's how your program might work:

frilled.cs.indiana.edu%java One
Type> I am here
3 words, average is now 3.0 words  sentence.
Type> done
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
Type> Once upon a time there was a princess. 
8 words, average is now 8.0 words per sentence.
Type> And the princess had a hat.
6 words, average is now 7.0 words per sentence.
Type> A red hat.
3 words, average is now 5.666666666666667 words per sentence.
Type> It was a Finnish princess and she was working with Linux. 
11 words, average is now 7.0 words per sentence.
Type> Oh, boy. 
2 words, average is now 6.0 words per sentence.
Type> Scary. 
1 words, average is now 5.166666666666667 words per sentence.
Type> 
0 words, average is now 4.428571428571429 words per sentence.
Type> 
0 words, average is now 3.875 words per sentence.
Type> done
frilled.cs.indiana.edu%
Feel free to use StringTokenizer to collect the words. When you're done turn the program in OnCourse under the Dropbox for the time and location of the exam even if you're from another section. Then write the program by hand on the back of this page and return it to the AI before you leave. I will grade this paper but I might refer to your posted code in OnCourse as well so please do both.

You can if you want turn in additional materials such as: pseudocode, flow charts, additional explanations of the code and/or the approach chosen. Be sure to solve as much as you can and to turn in a program in OnCourse at the end of the lab in addition to turning in the paper with your exam.

Good luck and do well.

import java.util.*; 

class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("Type> "); 
	String line = c.readLine(); 
	int totalWords = 0; 
	int sentences = 0; 
	while (! line.equals("done")) {
	    StringTokenizer st = new StringTokenizer(line); 
	    int words = 0;
	    while (st.hasMoreTokens()) {
		st.nextToken(); 
		words += 1; 
	    }
	    sentences += 1; 
	    totalWords += words; 
	    System.out.println(words + " words, average is now " + 
			       (double)totalWords/sentences + 
			       " words per sentence.");
	    System.out.print("Type> ");
	    line = c.readLine();
	}
    }
}