Download presentation
Presentation is loading. Please wait.
Published byBrendan Wood Modified over 9 years ago
1
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.
2
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. SentenceFirst Word Rest of Sentence This is a sentence. This is a sentence. is a sentence. isa sentence. a sentence. asentence.
3
DESIGN: UML class diagram class imported from javax.swing package WordExtractorApp main() JOptionPane from javax.swing readDouble() readInt() … WordExtractor +WordExtractor (String) +void setSentence(String) +String getFirst() +String getRest() +String toString() -Sentence: String class name data field (attribute) methods (operations) worker class application class System.out println() … class imported automatically from standard library
4
DESIGN of Class WordExtractor Instance variables/ fields String sentence Methods void setSentence() String getFirst() String getRest() String toString() Classes used StringAttribute 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.
5
Algorithms for WordExtractor methods Algorithm for getFirst() 1. Find the position of the first blank in the sentence. sentence. 2.Return the characters up to the first blank. 2.Return the characters up to the first blank. Algorithm for getRest() 1.Find the position of the first blank in the sentence. sentence. 2.Return the characters after the first blank.
6
IMPLEMENTATION /* * WordExtractor.java Author: Koffman and Wolz * WordExtractor.java Author: Koffman and Wolz * Represents a class that extracts a word * Represents a class that extracts a word */ */ public class WordExtractor { private String sentence; //source sentence private String sentence; //source sentence // Methods // Methods public WordExtractor(String str) { public WordExtractor(String str) { sentence = str; sentence = str; } public void setSentence(String str) { public void setSentence(String str) { sentence = str; sentence = str; }
7
// precondition - sentence has at least two words // precondition - sentence has at least two words // postcondition - returns the first word of sentence // postcondition - returns the first word of sentence public String getFirst() { public String getFirst() { // Find the position of first blank // Find the position of first blank int posBlank = sentence.indexOf(" "); int posBlank = sentence.indexOf(" "); // Return the characters up to the first blank. // Return the characters up to the first blank. return sentence.substring(0, posBlank); return sentence.substring(0, posBlank); } // precondition - sentence has at least two words // precondition - sentence has at least two words // postcondition - gets sentence after first blank public String getRest() { public String getRest() { // Find the position of the first blank // Find the position of the first blank int posBlank = sentence.indexOf(" "); int posBlank = sentence.indexOf(" "); // Return the characters after the first blank. // Return the characters after the first blank. return sentence.substring(posBlank + 1); return sentence.substring(posBlank + 1); }
8
// postcondition: Returns string sentence. public String toString() { public String toString() { return sentence; return sentence; }}
9
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.
10
UML sequence diagram WordExtractor App JOptionPane showInputDialog() getFirst() System.out wE1: WordExtractor new() println() get sentence create wE1, get first word and display it display rest of the sentence getRest() getFirst() wE2: WordExtractor new() println() getRest() wE3: WordExtractor getFirst() new() println() getRest() println() create wE2, get next word and display it create wE3, get next word and display it
11
Application Class /* WordExtractorApp.java Author: Koffman and Wolz WordExtractorApp.java Author: Koffman and Wolz Extracts 3 words from a sentence. Extracts 3 words from a sentence. */ */ import javax.swing.*; public class WordExtractorApp { public static void main(String[] args) { public static void main(String[] args) { // Read in a sentence, create object and output 1 st word // Read in a sentence, create object and output 1 st word String sent = JOptionPane.showInputDialog("Enter at least 4 words"); String sent = JOptionPane.showInputDialog("Enter at least 4 words"); WordExtractor wE1 = new WordExtractor(sent); WordExtractor wE1 = new WordExtractor(sent); System.out.println("first word: " + wE1.getFirst()); System.out.println("first word: " + wE1.getFirst());
12
// use rest of sentence to create new object and repeat // use rest of sentence to create new object and repeat WordExtractor wE2 = new WordExtractor(wE1.getRest()); WordExtractor wE2 = new WordExtractor(wE1.getRest()); System.out.println("second word: " + wE2.getFirst()); System.out.println("second word: " + wE2.getFirst()); // one more time // one more time WordExtractor wE3 = new WordExtractor(wE2.getRest()); WordExtractor wE3 = new WordExtractor(wE2.getRest()); System.out.println("third word: " + wE3.getFirst()); System.out.println("third word: " + wE3.getFirst()); //output remainder of the sentence //output remainder of the sentence System.out.println("rest of sentence: " + wE3.getRest()); System.out.println("rest of sentence: " + wE3.getRest()); }}
13
Sample Run
14
// What is different here?? import javax.swing.*; public class WordExtractorApp { public static void main(String[] args) { public static void main(String[] args) { // Read in a sentence, create object and output 1 st word // Read in a sentence, create object and output 1 st word String sent = JOptionPane.showInputDialog("Enter at least 4 words"); String sent = JOptionPane.showInputDialog("Enter at least 4 words"); WordExtractor wE1 = new WordExtractor(sent); WordExtractor wE1 = new WordExtractor(sent); System.out.println("first word: " + wE1.getFirst()); System.out.println("first word: " + wE1.getFirst()); wE1.setSentence(wE1.getRest()); wE1.setSentence(wE1.getRest()); System.out.println("second word: " + wE1.getFirst()); System.out.println("second word: " + wE1.getFirst()); // one more time // one more time wE1.setSentence(wE1.getRest()); wE1.setSentence(wE1.getRest()); System.out.println("third word: " + wE1.getFirst()); System.out.println("third word: " + wE1.getFirst()); //output remainder of the sentence //output remainder of the sentence wE1.setSentence(we1.getRest()); wE1.setSentence(we1.getRest()); System.out.println("rest of sentence: " + wE1.toString() ); System.out.println("rest of sentence: " + wE1.toString() ); } } *optional
15
WHY design a class to solve this problem rather than just writing an application program?? WHY design a class to solve this problem rather than just writing an application program?? REUSABLE CODE REUSABLE CODE Another design for same problem might have been to create a class which provided String utililities.. Another design for same problem might have been to create a class which provided String utililities..
16
DESIGN: UML class diagram WordExtractorApp main() JOptionPane from javax.swing readDouble() readInt() … WordExtractor -WordExtractor() +static String getFirst(String) +static String getRest(String) worker class application class System.out println() …
17
public class WordExtractor { private WordExtractor() { } private WordExtractor() { } public static String getFirst(String source) { public static String getFirst(String source) { // Find the position of first blank // Find the position of first blank int posBlank = source.indexOf(" "); int posBlank = source.indexOf(" "); // Return the characters up to the first blank. // Return the characters up to the first blank. return source.substring(0, posBlank); return source.substring(0, posBlank); } public static String getRest(String source) { public static String getRest(String source) { // Find the position of the first blank // Find the position of the first blank int posBlank = source.indexOf(" "); int posBlank = source.indexOf(" "); // Return the characters after the first blank. // Return the characters after the first blank. return source.substring(posBlank + 1); return source.substring(posBlank + 1); } Why not store the position of the blank in an instance variable, for use by both methods??
18
// WordExtractorApp.java // WordExtractorApp.java import javax.swing.*; public class WordExtractorApp { public static void main(String[] args) { public static void main(String[] args) { // Read in a sentence and output 1 st wor // Read in a sentence and output 1 st wor String sent = JOptionPane.showInputDialog("Enter at least 4 words"); String sent = JOptionPane.showInputDialog("Enter at least 4 words"); System.out.println("first word: " + WordExtractor.getFirst(sentence) ); System.out.println("first word: " + WordExtractor.getFirst(sentence) ); // reduce sentence and output 2 nd word // reduce sentence and output 2 nd word sentence = sentence.substring( sentence.indexOf(“ “) +1); sentence = sentence.substring( sentence.indexOf(“ “) +1); System.out.println("second word: " + WordExtractor.getFirst(sentence) ); System.out.println("second word: " + WordExtractor.getFirst(sentence) ); // one more time // one more time sentence = sentence.substring( sentence.indexOf(“ “) +1); sentence = sentence.substring( sentence.indexOf(“ “) +1); System.out.println("second word: " + WordExtractor.getFirst(sentence) ); System.out.println("second word: " + WordExtractor.getFirst(sentence) ); //output remainder of the sentence //output remainder of the sentence sentence = sentence.substring( sentence.indexOf(“ “) +1); sentence = sentence.substring( sentence.indexOf(“ “) +1); System.out.println("rest of sentence: " + sentence); System.out.println("rest of sentence: " + sentence); }} * sentence.toString() is executed. This time it is the toString method of the String class * sentence.toString() is executed. This time it is the toString method of the String class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.