//SumAvgMaxStrings.java import java.awt.*; import javax.swing.*; public class SumAvgMaxStrings extends JApplet { public void init() { String input, maxString, minString, previous; int num, numStrings=0, largest, smallest; int stringLength; double sum=0; String inOrder="yes"; //"priming read" to get first string input = JOptionPane.showInputDialog( "Enter a word (Enter to quit)" ); //a String's length() method returns the number of characters in it largest = input.length(); //length of longest string maxString = input; //the actual longest string smallest = input.length(); minString = input; previous = input; //remember the previously inputted string while (!input.equals("")) { //loop until empty string stringLength = input.length(); sum = sum + stringLength; //add string length to sum of string lengths. numStrings++; showStatus("sum="+sum+" word="+input+" #strings="+numStrings); if (stringLength > largest) { largest = stringLength; maxString = input; } if (stringLength < smallest) { smallest = stringLength; minString = input; } //String compareTo method to alphabetically compare two strings. //returns <0 if comes before, >0 if comes after, 0 if same if (input.compareTo(previous) < 0) inOrder = "no"; //an out-of-order word, thus entire sequence of words is not in alphabetical order previous = input; //this input will be previous for next word //get next word: input = JOptionPane.showInputDialog( "Enter a word (Enter to quit)" ); } if (numStrings > 0) { JOptionPane.showMessageDialog(null, "#words=" + numStrings+ "\nTotal number of letters=" + sum + "\nAverage letters per word=" + (double)sum/numStrings+ "\nLongest word length=" + largest + " " + maxString + "\nShortest word length=" + smallest + " " + minString + "\nWords entered in order: " + inOrder); } else JOptionPane.showMessageDialog(null, "No words entered"); } }