implemented composite pattern
This commit is contained in:
30
src/uebung08/doc/Paragraph.java
Normal file
30
src/uebung08/doc/Paragraph.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package uebung08.doc;
|
||||
|
||||
public class Paragraph implements TextComponent {
|
||||
|
||||
private String text;
|
||||
|
||||
public Paragraph(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
|
||||
public int countWords() {
|
||||
int wordcount = 0;
|
||||
boolean isInWord = false;
|
||||
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
if(Character.isAlphabetic(text.charAt(i))){
|
||||
if (!isInWord) {
|
||||
wordcount++;
|
||||
isInWord = true;
|
||||
}
|
||||
} else {
|
||||
isInWord = false;
|
||||
}
|
||||
}
|
||||
|
||||
return wordcount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user