// Program 6.4 on page 109 of Brinch Hansen class readability extends basic { public static void main (String param[]) throws Exception { // establish keyboard and screen input and output input keyboard = new input(); output screen = new output(); screen.writeln("Type file name"); // establish input and output text files input in = new input(keyboard.readword()); output out = new output("results"); in.readblanks(); assume(in.more(), "empty text"); // "assume" checks that the file was // successfully opened and is nonempty int sentences = 0, words = 0; // Text: read a series of sentences until the file is empty while(in.more()) { String word = in.readname(); in.readblanks(); words = words + 1; // Sentence: read a sentence while ((in.next()!=':') && (in.next()!='?') && (in.next()!='.')) // AnotherWord: read the next word { if (in.next()==',') { in.readnext(); in.readblanks(); } word = in.readname(); in.readblanks(); words = words + 1; } in.readnext(); in.readblanks(); sentences = sentences + 1; } // Compute average sentence length and display results int length = words / sentences; if (length <= 8) out.write ("4th grade"); else if (length <= 11) out.write ("5th grade"); else if (length <= 14) out.write ("6th grade"); else if (length <= 17) out.write ("7th grade"); else if (length <= 24) out.write ("High school"); else out.write ("College"); out.writeln (" level: " + length + " words per sentence"); in.close(); out.close(); keyboard.close(); screen.close(); } }