180 likes | 200 Views
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.
E N D
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 First Word Rest of Sentence This is a sentence. This is a sentence. is a sentence. is a sentence. a sentence. a sentence.
WordExtractorApp JOptionPane from javax.swing application class class imported from javax.swing package main() readDouble() readInt() … worker class class name WordExtractor System.out class imported automatically from standard library data field (attribute) -Sentence: String +WordExtractor (String) +void setSentence(String) +String getFirst() +String getRest() +String toString() println() … methods (operations) DESIGN: UML class diagram
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. DESIGN of Class WordExtractor
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() • 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.
WordExtractor App System.out JOptionPane showInputDialog() get sentence wE1: WordExtractor new() create wE1, get first word and display it getFirst() println() getRest() wE2: WordExtractor new() create wE2, get next word and display it getFirst() println() getRest() wE3: WordExtractor new() create wE3, get next word and display it getFirst() println() getRest() display rest of the sentence println() UML sequence diagram
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()); } }
//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..
WordExtractorApp JOptionPane from javax.swing application class main() readDouble() readInt() … worker class WordExtractor System.out -WordExtractor() +static String getFirst(String) +static String getRest(String) println() … DESIGN: UML class diagram
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