240 likes | 828 Vues
Lab Exercise 4: Mad Libs. Tarzan: Java Programmer Tarzan was late to work when his vine ran into a large . “Whoa there, fella!” He exclaimed, “Don’t mess up my hair. Tarzan took a from his loin cloth and fixed his flowing locks. Animal.
E N D
Lab Exercise 4: Mad Libs Tarzan: Java Programmer Tarzan was late to work when his vine ran into a large . “Whoa there, fella!” He exclaimed, “Don’t mess up my hair. Tarzan took a from his loin cloth and fixed his flowing locks. Animal Adjective Power Tool
Create a program that reads a Mad Lib file and prompts the user for words to fill in for each word. Then print the completed story. Take the filename as the first command line argument Use the “MadLib.txt” example file (shown next) Fill-in words are given between underscores Design the code with expansion in mind (think GUI)
MadLib.txt From the "Best of Mad Libs" by Roger Price and Leonard Stern LITTLE RED RIDING HOOD One day, Little _color_ Riding Hood was going through the forest carrying a basket of _plural noun_ for her grandmother. Suddenly, she met a big _adjective_ wolf. "_exclamation_!" said the wolf. "Where are you going, little _silly word_?" "I'm going to my grandmother's house," she said. Then the wolf _verb (past tense)_ away. When Miss Riding Hood got to her grandmother's house, the wolf was in bed dressed like her grandmother. "My, Grandmother," she said. "What big _plural noun 1_ you have." "The better to _verb 1_ you with," said the wolf. "And, Grandmother," she said, "what big _plural noun 2_ you have." The wolf said, "The better to _verb 2_ you with." And then she said, "What big _plural noun 3_ you have, Grandmother." But the wolf said nothing. He had just died of indigestion from eating Grandmother.
Hints MadLib class Read the file in the constructor List<String> getPrompts() String makeStory(Map<String,String> fillins) Maybe put “main” in the MadLib class too Static helper function to “askUser” for input
Solution 4: Mad Libs Tarzan: Java Programmer Tarzan was late to work when his vine ran into a large . “Whoa there, fella!” He exclaimed, “Don’t mess up my hair. Tarzan took a from his loin cloth and fixed his flowing locks. Goat Freakish Drill
Simple API for the MadLibs object: Constructor takes filename “getPrompts” to return a list of inputs “makeStory” takes the user inputs and returns a string importjava.util.List; importjava.util.Map; publicclassMadLib { publicMadLib(String filename) { } public List<String> getPrompts() { returnnull; } public String makeStory(Map<String,String> fillins) { return""; } publicstaticvoid main(String [] args) { System.out.println("Welcome to Mad Libs!"); } }
The “main” logic is easy. privatestatic Map<String,String> askUser(List<String> prompts) { Map<String,String> ret = newHashMap<String,String>(); return ret; } publicstaticvoid main(String [] args) { System.out.println("Welcome to Mad Libs!"); MadLib mad = newMadLib(args[0]); List<String> prompts = mad.getPrompts(); Map<String,String> fillins = askUser(prompts); String story = mad.makeStory(fillins); System.out.println(story); }
Start with the constructor and “makeStory”. importjava.io.FileInputStream; importjava.io.IOException; importjava.io.InputStream; importjava.util.HashMap; importjava.util.List; importjava.util.Map; publicclassMadLib { private String story; /** * This creates a new MadLib object and reads the * story from the given file. * @param filename the name of the MadLib file * @throwsIOException if something goes wrong */ publicMadLib(String filename) throwsIOException { InputStreamfis = newFileInputStream(filename); // TODO: I know better than this byte [] data = newbyte[fis.available()]; fis.read(data); fis.close(); story = new String(data); } public String makeStory(Map<String,String> fillins) { // TODO just for now returnstory; }
Next in the flow: “getPrompts()” /** * This method returns the list of prompts from the * given story. * TODO: cache this ... only need to do this once * TODO: check for errors in format * @return the prompts */ public List<String> getPrompts() { List<String> ret = newArrayList<String>(); intpos = 0; while(true) { inti = story.indexOf('_',pos); if(i<0) return ret; int j = story.indexOf('_',i+1); String prompt = story.substring(i+1,j); ret.add(prompt); //System.out.println(prompt); pos = j + 1; } }
Now the user input. privatestatic Map<String,String> askUser(List<String> prompts) { Map<String,String> ret = newHashMap<String,String>(); Scanner scan = new Scanner(System.in); for(String prompt : prompts) { System.out.print("Give me a word for '"+prompt+"' :"); String inp = scan.nextLine(); ret.put(prompt, inp); //System.out.println("::"+prompt+":"+inp+"::"); } scan.close(); return ret; }
Task list shows “makeStory” is left publicString makeStory(Map<String,String> fillins) { intpos = 0; String ret = ""; // TODO use a string builder while(true) { inti = story.indexOf('_',pos); if(i<0) { ret = ret + story.substring(pos); return ret; } ret = ret + story.substring(pos,i); int j = story.indexOf('_',i+1); String prompt = story.substring(i+1,j); String inp = fillins.get(prompt); ret = ret + inp; pos = j + 1; } }
Test it! Create files with broken format Duplicate prompts No “closing” underscore on last prompt Task list: