Word Extractor class

 PROBLEM: We want to write a program that
gets a sentence and displays the first three
words in the sentence on separate lines,
followed by the rest of the sentence. To simplify
the task, we assume that the sentence has at
least 4 words.
ANALYSIS
 We need to get the first word of a sentence and also to get
the rest of the sentence that follows the first word. Once we
do this, we can get the first word from the rest of the sentence
which will give us the second word and a sentence that
begins with the third word, and so on.
 Sentence
This is a sentence.
is a sentence.
a sentence.
First Word
This
is
a
Rest of Sentence
is a sentence.
a sentence.
sentence.
DESIGN: UML class
diagram
class imported from
javax.swing package
JOptionPane
WordExtractorApp
main()
readDouble()
readInt()
…
class imported
automatically from
standard library
application class
from javax.swing
System.out
println()
…
worker class
WordExtractor
class name
-Sentence: String
data field (attribute)
+WordExtractor
(String)
+void
setSentence(String)
+String getFirst()
+String getRest()
+String toString()
methods (operations)
DESIGN of Class
WordExtractor
Instance variables/ fields
String sentence
Methods
void setSentence()
String getFirst()
String getRest()
String toString()
Classes used
String
Attribute
Reference to the string whose
first word is to be extracted
Behavior
Sets instance variable to new
value
Gets the first word in sentence.
Gets the rest of the string
referenced by sentence.
Represents object's state.
Algorithms for
WordExtractor methods
 Algorithm for getFirst()
1.
Find the position of the first blank in the
sentence.
2.
Return the characters up to the first blank.
 Algorithm for getRest()
1.
Find the position of the first blank in the
sentence.
2.
Return the characters after the first blank.
IMPLEMENTATION
/*
* WordExtractor.java
Author: Koffman and Wolz
* Represents a class that extracts a word
*/
public class WordExtractor {
private String sentence; //source sentence
// Methods
public WordExtractor(String str) {
sentence = str;
}
public void setSentence(String str) {
sentence = str;
}
// precondition - sentence has at least two words
// postcondition - returns the first word of sentence
public String getFirst() {
// Find the position of first blank
int posBlank = sentence.indexOf(" ");
// Return the characters up to the first blank.
}
return sentence.substring(0, posBlank);
// precondition - sentence has at least two words
// postcondition - gets sentence after first blank
public String getRest() {
// Find the position of the first blank
int posBlank = sentence.indexOf(" ");
// Return the characters after the first blank.
}
return sentence.substring(posBlank + 1);
// postcondition: Returns string sentence.
public String toString() {
return sentence;
}
}
Application Class Algorithm
 Algorithm for main()
1.
Read in a sentence.
2.
Create a WordExtractor object that stores the input
sentence.
3.
Write the first word to the console window.
4.
Create a WordExtractor object that stores the sentence
starting with the second word.
5.
Write the second word to the console window.
6.
Create a WordExtractor object that stores the sentence
starting with the third word.
7.
Write the third word to the console window.
8.
Write the rest of the sentence to the console window.
UML sequence diagram
WordExtractor
App
get sentence
System.out
showInputDialog()
new()
create wE1,
get first word
and display it
JOptionPane
wE1:
WordExtractor
getFirst()
println()
getRest()
new()
create wE2,
get next word
and display it
wE2:
WordExtractor
getFirst()
println()
getRest()
new()
create wE3,
get next word
and display it
display rest of
the sentence
getFirst()
println()
getRest()
println()
wE3:
WordExtractor
Application Class
/*
WordExtractorApp.java
Author: Koffman and Wolz
Extracts 3 words from a sentence.
*/
import javax.swing.*;
public class WordExtractorApp {
public static void main(String[] args) {
// Read in a sentence, create object and output 1st word
String sent = JOptionPane.showInputDialog("Enter at least 4 words");
WordExtractor wE1 = new WordExtractor(sent);
System.out.println("first word: " + wE1.getFirst());
// use rest of sentence to create new object and repeat
WordExtractor wE2 = new WordExtractor(wE1.getRest());
System.out.println("second word: " + wE2.getFirst());
// one more time
WordExtractor wE3 = new WordExtractor(wE2.getRest());
System.out.println("third word: " + wE3.getFirst());
//output remainder of the sentence
System.out.println("rest of sentence: " + wE3.getRest());
}
}
Sample Run
// What is different here??
import javax.swing.*;
public class WordExtractorApp {
public static void main(String[] args) {
// Read in a sentence, create object and output 1st word
String sent = JOptionPane.showInputDialog("Enter at least 4 words");
WordExtractor wE1 = new WordExtractor(sent);
System.out.println("first word: " + wE1.getFirst());
wE1.setSentence(wE1.getRest());
System.out.println("second word: " + wE1.getFirst());
// one more time
wE1.setSentence(wE1.getRest());
System.out.println("third word: " + wE1.getFirst());
//output remainder of the sentence
wE1.setSentence(we1.getRest());
System.out.println("rest of sentence: " + wE1.toString() );
}
}
*optional
WHY design a class to solve this problem
rather than just writing an application
program??
REUSABLE CODE
Another design for same problem
might have been to create a class
which provided String utililities..
DESIGN: UML class
diagram
JOptionPane
WordExtractorApp
application class
from javax.swing
main()
readDouble()
readInt()
…
System.out
println()
…
worker class
WordExtractor
-WordExtractor()
+static String
getFirst(String)
+static String
getRest(String)
public class WordExtractor {
private WordExtractor() { }
public static String getFirst(String source) {
// Find the position of first blank
int posBlank = source.indexOf(" ");
// Return the characters up to the first blank.
}
return source.substring(0, posBlank);
public static String getRest(String source) {
// Find the position of the first blank
int posBlank = source.indexOf(" ");
// Return the characters after the first blank.
}
return source.substring(posBlank + 1);
Why not store the position of the blank in an instance variable, for use by
both methods??
// WordExtractorApp.java
import javax.swing.*;
public class WordExtractorApp {
public static void main(String[] args) {
// Read in a sentence and output 1st wor
String sent = JOptionPane.showInputDialog("Enter at least 4 words");
System.out.println("first word: " + WordExtractor.getFirst(sentence) );
// reduce sentence and output 2nd word
sentence = sentence.substring( sentence.indexOf(“ “) +1);
System.out.println("second word: " + WordExtractor.getFirst(sentence) );
// one more time
sentence = sentence.substring( sentence.indexOf(“ “) +1);
System.out.println("second word: " + WordExtractor.getFirst(sentence) );
//output remainder of the sentence
sentence = sentence.substring( sentence.indexOf(“ “) +1);
System.out.println("rest of sentence: " + sentence);
}
}
* sentence.toString() is executed. This time it is the toString method of the String class