Java Reference
In-Depth Information
4.2
The model part
The model is implemented in class TextAnalysisModel which is listed below.
The class uses the string variable currentText to store the text to be analysed in
upper-case letters. The integer variable currentNumberOfEs is used to store the
number of 'E's in the current text. The two other variables totalNumberOfEs and
totalNumberOfTexts will only be used in the exercises to this chapter.
The actual analysis is performed by method analyse(String str) . This con-
verts the string str to upper case and stores the result in currentText . Then the
number of occurrences of the letter 'E' in currentText is computed by looking at
every character in the string. The last two lines of analyse update the variables
to be used in the exercises. The four get -methods return the respective values of
the variables.
File: its/TextAnalysisGUI/TextAnalysisModel.java
1. package its.TextAnalysisGUI;
2.
3. public class TextAnalysisModel {
4.
5.
private int totalNumberOfEs;
6.
private int currentNumberOfEs;
7.
private int totalNumberOfTexts;
8.
private String currentText;
9.
10.
public TextAnalysisModel() {
11.
totalNumberOfEs = 0;
12.
totalNumberOfTexts = 0;
13.
currentText = "";
14.
}
15.
16.
public void analyse(String str){
17.
currentText = str.toUpperCase();
18.
currentNumberOfEs = 0;
19.
for ( int i=0;i<currentText.length(); i++) {
20.
if (currentText.charAt(i) == 'E'){
21.
currentNumberOfEs++;
22.
}// if
23.
}// for i
24.
totalNumberOfEs += currentNumberOfEs;
25.
totalNumberOfTexts++;
26.
}// analyse
27.
28.
public int getCurrentNumberOfEs(){
Search WWH ::




Custom Search