pa3-MadLibs CS139 Algorithm Development Software Requirements Specification Programming Assignment 3 Mad Lib Due Tuesday, October 29 by 11:59pm Corrections Corrections will be highlighted in Yellow - updated 10/18/2013 Corrections - highlighted in green - updated 10/24/2013 - cleaned up the inconsistencies Correction - highlighted in blue - updated 10/27/2013 - Added a question mark to a question without. Also a few minor changes. Introduction Purpose: This program will play a game with the user. The user will provide a series of words and numbers and the computer will use those words to produce a Mad Lib style story. Mad libs consist of a text with blanks. The player is asked for a specific series of words and the reader fills those into the story for the blanks. See the web site: http://www.eduplace.com/tales/ for some examples. Objectives - At the conclusion of this exercise the student will demonstrate that they can: Use an editor to create a java source file from scratch. Write java code to create an attractive user interface. Write java code to perform basic arithmetic calculations and String manipulation. Refactor Toolkit to begin thinking about class design. Use decision structures to manipulate data and prevent errors. Do everything necessary to create, debug, and run a java program. Use comments to delineate each section of the program. Adhere to a Style Guide. READ AND FOLLOW DIRECTIONS!!!!! Deadlines Submit: Tuesday, October 29 by 11:59pm into the WebCAT submit system. Programs will not be accepted after 6 days late. Prerequisites You have covered the material in Chapter 2, 3, and 5 of Gaddis. You will want to reference Chapter 10 on String methods as you work the PA. You have completed a Toolkit class from PA2 - See your instructor if you have not finished PA2. Starter Files MadLib.java - A driver for the application MadLibGame.java - The game play. MadLibIO.java - The IO starter Word.java - Represents a single word. Your Toolkit.java file Program Behavior Your application must do these things: Your program will prompt for and read five input values. These are: A person's name (String) Another person's name (String) A favorite city (String) A person's gender (String) A number between 1 and 10 (int) A car name (String) An animal (String) A color (String) A sport (String) A money amount (double) Your program must display the story (specified below) with the missing elements filled in by the input or calculated values. All output must be properly formatted. Output Output the heading, "Welcome to the CS139 Mad Lib game" followed by the newline character. Output a blank line. Prompt for the input values. Note: your input will be on the same line as the prompt. Your first input prompt must be the String, "What is your first name? ", and will read in a single word. Ignore anything else on the line. Your second input prompt must be the String, "What is the first name of a friend? " , and will read in a single word. Ignore anything else on the line. Your third input prompt must be the String, "What is your favorite city? ", and will read in a single word. Ignore anything else on the line. Your fourth input prompt must be the String, "What is your gender? (m/f) ", and will read in a single character. Ignore anything else on the line. Your fifth input prompt must be the String, "What is a number between 1 and 10? ", and will read in an integer. Ignore anything else on the line. Your sixth input prompt must be the String, "What is your favorite car? ", and will read in the entire line. Your seventh input prompt must be the String, "What is your favorite animal? ", and will read in the entire line. Your eighth prompt must be the String, "What is your favorite color? ", and will read in a single word. Ignore anything else on the line. Your ninth input prompt must be the String, "What is your favorite sport? ", and will read in the entire line. Your tenth input prompt must be the String, "How much money is in your wallet? ", and will read in a double value. After reading all of the input values, output a blank line then the story as follows: The values in square brackets [ ] are the substitutions. The story must be formatted as shown. NOTE: you must have newline characters after each of the lines shown here. In other words, your story should have a title, a new blank line, and then the body which is exactly 11 lines long. [name possessive (1)] Excellent Adventure It was a beautiful fall day in [city]. [name1] had just finished a big project for [his/her (gender)(2)] CS 139 class. [She/he (gender)(3)] decided to take the day off and explore [city]. [name possessive (1)] pet [animal] loved to ride in the car. [name] decided to take the [animal] out for a ride to the zoo. [She/he (gender) (3)] got into [a/an (4)] [color] [car] with the [animal]. [name] checked [his/her (gender)(2)] wallet and decided that [money (5)] was enough for the day. Imagine [his/her (gender) (2)] surprise to find [his/her (gender) (2)] best friend [friend] at the entrance to the zoo. "Did you know", asked [friend], "that there is going to be a [sport] match between two [groups of/nothing (number) (6)] [animal plural(7)]". "There are at least [number converted to a word(9)] [animal plural special(8)] on each team." [name] paid the entry fee and they spent the rest of the day watching the [sport] match between the [animal plural (7)]. Example Welcome to the CS139 Mad Lib game
What is your first name? taylor
What is the first name of a friend? Sam
What is your favorite city? Harrisonburg
What is your gender? (m/f) qw
You entered qw. Using "F"
What is a number between 1 and 10 3
What is your favorite car? Prius
What is your favorite animal? Banana slug
What is your favorite color? green
What is your favorite sport? jump roping
How much money is in your wallet? 25.64
Taylor's Excellent Adventure
It was a beautiful fall day in Harrisonburg. Taylor had just finished a big project for her CS 139 class.
She decided to take the day off and explore Harrisonburg.
Taylor's pet slug loved to ride in the car.
Taylor decided to take the banana slug out for a ride to the zoo.
She got into a green prius with the banana slug.
Taylor checked her wallet and decided that $25.64 was enough for the day.
Imagine her surprise to find her best friend Sam at the entrance to the zoo.
"Did you know", asked Sam, "that there is going to be a jump roping match between two groups of banana slugs?"
"There are at least three banana slugs on each team."
Taylor paid the entry fee and they spent the rest of the day watching the jump roping match between the banana slugs.
Explanations for numbered items above and general rules: Regardless of how they were entered, the names of the two people and the city name should be capitalized. Regardless of how they were entered, the other words should be all small letters. 1 - Name possessive - Use the name and follow the possessive rule below. 2 - his/her - If the gender is M then his, F then her. 3 - She/he - If the gender is M then He, F then She. 4 - A [n] - If the color starts with a vowel (like orange or emerald), you use the determinant "an" otherwise you use "a". 5 - money - Use currency format. There should be at least one digit to the left of the decimal and two digits to the right. ($0.59) 6 - groups of / nothing - If the number is 1, then output nothing. If the number is between 2 - 10 inclusive, output "groups of". 7 - Animals plural - Make plural using the plural rules below. 8 - Animals plural special - If the number is 1, then do not change the animal name. Otherwise, make plural using the plural rules below. So for example if the animal is "whale", and there are more than one, then you would use "whales". If there is only one you would use whale. 9 - Number converted to a word - The number should be printed with its word equivalent (small letter to start). So if the number were entered as 1, the equivalent would be "one". Plural rules If the name ends in an s, add es. (Example - rhinocerous, rhinocerouses) If the name ends in ey, just add s. (Example - monkey, monkeys) Otherwise if the name ends in y, change the y to ies (Example - butterfly, butterflies) All other names will have an s added to the name (Examples, dog-dogs, moose-mooses) Possessive rules If the name ends in s, add ' only. (Example - Agnes, Agnes') For all other cases, add 's. (Example - Herman, Herman's) Inputs and error checking Gender must be entered as M, m, F, or f. Anything else constitutes an error. You should immediately output the error message, "You entered %s. Using \"F\"", where the substitution will be the value that they entered. You should assume female if the gender is entered incorrectly. If something other than a number is entered where we are asking for a number or amount, you should immediately output the error message, "You entered %s. Using 5", where the substitution will be the value that they entered. You should assume 5 if the number is entered incorrectly. (This is both for the amount and the number.) If an integer number is entered that is outside of the range 1 to 10, you should simply use 10. All other values can be accepted as is. Program Structure You must refactor your Toolkit. Right now, your toolkit contains methods that are general utility (the read methods for example) and that operate on Strings (words). You will separate these into two different classes. MadLibIO will contain methods that do input. It should have have a Scanner object as a private attribute (global variable) and a constructor method that will build the Scanner. All input will be done using this class. You may add additional private methods if you wish to this class. See MadLibIOStarter.java for this part of the class. Word is a new class that will manipulate words. It will have a single private attribute representing the word. It will have a constructor that will accept the word as its single input parameter. The methods will then use that word to operate on. See Word.java for this part of the class and an example method. You must have a story driver file named MadLib.java. This will be given to you. It will create a MadLibGame object and call the tellStory method. tellStory controls the story. You must have methods to handle items above. Some of the methods can be added to Toolkit, others that are specific to this PA, should be added in the MadLibs.java file, and those that are manipulating words can be added to the Word class. You must use the specific methods listed here, but you may also add your own methods. Since the number transformation is not starting with a word, but a number, it would not be appropriate to add to Word...it should be a method in MadLibs.java. (add method: public String numberToWord(int number) to your MadLibGame.java. You must have a method to check if the number entered is outside of the range 1 and 10. The method should return the number that we will use for the story. (add method: public int checkIntRange(int value, int lo, int hi, int defaultVal) to your MadLibsIO. You must have a method to check if the user entered m or f only. The method should be in the MadLibIO (public char checkGender(char gender)). You must add the following methods to your Word.java file (In addition to all of the String methods from Toolkit). public String makePlural () - This method will have the decisions regarding which of the plural methods to use. It will use the global word attribute. public String makePossess() - This method will have the decisions regarding which of the plural methods to use.It will use the global word attribute. public String heShe(char gender) - This method will return He/She depending on the gender entered. public String himHer(char gender) - This method will return him/her depending on the gender. Your readInteger and readDouble methods should be altered to error check and if the user enters something other than an integer or double set the number to the default value for this story. You must add a parameter to these methods which is the default value to use for the method if the person enters a value that is not the correct type of number. public int readInteger(String prompt, int default) would be the new header for the readInteger method. Your program must include an acknowledgement section acknowledging help received from TAs or reference sources. You do not need to reference receiving help from the instructor. If you received no help your acknowledgement section should have a statement to that effect. Your program must conform to standard Java programming standards and the additional standards for this class. See the Style Guide for your class. Suggested Approach First, refactor your Toolkit into MadLibIO and Word. Use the Word starter shown above and make sure you use the right code for the Scanner and constructor for Toolkit. See ToolkitStarter.java above. In MadLibGame, start with the tellStory method. Create a variable for each of the substitutions in the story above. Initialize each to a fixed value for the particular story you want to build. Create the output of the story, substituting your variables declared in step 1 where appropriate. TEST! Look for spacing, punctuation types of problems. For each of the variables in the list that is just using what the user enters, create the prompts and reads to fill in those values. (use your Toolkit to make this easier) Remove the hardcoded initialization. TEST! Look for spacing, punctuation problems. For each other value, create new variables as needed. For example, to choose He/She, you need to have a variable for gender and a variable for the He/She word chosen. You can then write the method that will do that manipulation. As it is the most complex, save the plural method for last. Honor Code This work must conform to the JMU Honor Code and the specific requirements of this class. NO help may be provided by another student to another student. Authorized help is limited to your textbook, the TA’s for any CS139 section, and the professor for your section. You may work with your Lab Partner as long as you both are working together.You will be able to submit one program to WebCAT. Divide and conquer is not the way to produce quality code. At the same time, make sure that there is an equal sharing of both roles. It is important that both partners understand what is going on in the program. You may not receive help from any other students in the class or outside of the class. Grading Your program will be evaluated both by its correctness and conformance to the required elements. You will achieve a grade of 70 for a program that runs correctly and produces exactly the required output in the required format. The remainder (30) will be based on your conformance to the Style and other requirements of the assignment. Review the Style Guide before submitting your program and the grade sheet checklist. All grades will be based on 100 points. You may submit any number of times. The only one I will count is the last one submitted. Successfully submitted programs that are late will be graded, then the point penalty assessed for each day late. For submissions after: Oct. 29 - 10 points Oct. 30 - 20 points Oct. 31 - 30 points Nov 1 - 40 points Nov 3 - 50 points No submissions accepted after Nov 3. HINTS Begin early. This program is fairly long and you will want to work on it bit by bit. Students run into trouble by waiting too long to start the program. Understand the problem at hand. Make sure that you follow the requirements precisely. Don't add additional "flourishes". You will be downgraded. Design a top level algorithm. Don't worry as you are designing that you might not yet know how to do input or format output. Design as if you have those tools. You will get them before it is due. Design your substeps, at least the ones you can do now. Fill in the others as we cover material in class or you study it in your book. Your top level algorithm can be used as step documentation in your program. Start there.