Questions:
Write a program that allows a user to enter a line of text, counts number of words and number of vowels in the sentence, and gives the average number of vowels per word in the message (vowels are letters a, e, i, o, u or A, E, I, O , U). Your program should contain a class SentenceCounter and a graphical user interface called SentenceCounterGUI.
1. The class SentenceCounter can be used to count words, vowels and the average number of vowels per word in the sentence. It has the following methods:
-
- a constructor accepts a sentence as a parameter, and calls relevant methods to evaluate instance variables;
- a method countWords counts number of words in the sentence;
- a method countVowels calculates a number of vowels in the sentence;
- a method averageVowels calculates the average number of vowels per word in the sentence;
- accessor (get) methods return a number of words, vowels and average number of vowels;
- the method toString returns a full information about an object of the SentenceCounter class.
The following UML diagram might be of assistance:
SentenceCounter
|
- sentence: String
- numberOfWords: int
- numberOfVowels: int
- average: double
|
+ SentenceCounter(sentence : String)
+ countWords ( ) : void
+ countVowels ( ) : void
+ averageVowels ( ) : void
+ getNumberOfWords( ) : int
+ getNumberOfVowels( ) : int
+ getAverage( ) : double
+ toString( ) : String
|
2. Write a graphical user interface class called SentenceCounterGUI. It should be able to manipulate the sentence of words and have the following behaviour:
-
- label "Enter a sentence";
- text field to enter a sentence and create a SentenceCounter object;
- button "Count words" and the label to display it;
- button "Count vowels" and the label to display it;
- button "Average vowels" and the label to display it;
The class should include a main method that prepares a frame in which the panel is displayed.