Difference between revisions of "Mad Lib"
(Added some content. More writing to be done of course.) |
(Added quotations to student quotes) |
||
(26 intermediate revisions by 6 users not shown) | |||
Line 2: | Line 2: | ||
|ProblemName=Mad Lib | |ProblemName=Mad Lib | ||
− | |Problem= | + | |Problem=Create a program which takes some text input from a user and then makes a Mad Lib out of it. |
− | + | Consider making this solution with the following guidelines: | |
− | * JOptionPane with input and output. | + | * [[JOptionPane Methods|JOptionPane]] with input and output. |
− | * Input with Strings. | + | * Input with [[Strings]]. |
− | * Commenting our code. | + | * [[Comments|Commenting]] our code. |
The Mad Lib will follow this format: | The Mad Lib will follow this format: | ||
− | + | {{OutputBlock | |
− | plural noun country verb noun | + | |Code= |
− | + | "The ___________ in _______ like to ____ the ____." | |
− | + | plural noun country verb noun | |
+ | }} | ||
+ | |SolutionCode=/* | ||
+ | This is a program that uses the JOptionPane input dialogs to create a Mad Lib from some user input. | ||
− | |SideSectionTitle=... | + | The Mad Lib layout: |
+ | "The ___________ in _______ like to ____ the ____." | ||
+ | plural noun country verb noun | ||
+ | */ | ||
+ | |||
+ | import javax.swing.JOptionPane; | ||
+ | |||
+ | public class MadLib | ||
+ | { | ||
+ | public static void main(String []args) | ||
+ | { | ||
+ | // Create variables | ||
+ | String pluralNoun; | ||
+ | String country; | ||
+ | String verb; | ||
+ | String noun; | ||
+ | |||
+ | // Get input | ||
+ | pluralNoun = JOptionPane.showInputDialog("Please enter a plural noun:"); | ||
+ | country = JOptionPane.showInputDialog("Please enter a name of a country:"); | ||
+ | verb = JOptionPane.showInputDialog("Please enter a verb:"); | ||
+ | noun = JOptionPane.showInputDialog("Please enter a noun:"); | ||
+ | |||
+ | // Show output | ||
+ | JOptionPane.showMessageDialog(null, "The " + pluralNoun + " in " + country + " like to " + | ||
+ | verb + " the " + noun + "."); | ||
+ | // End of program | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |SideSectionTitle=...By Students | ||
|SideSection= | |SideSection= | ||
− | + | "I'm not ashamed to admit that while testing my version of the Mad Lib program, I put some of my favorite curse words I was willing to commit to text into my sample fields. Try it yourself!" | |
+ | |||
+ | "Testing is of course important. It's the only way to figure out if your program works. But, you'll soon find that testing your programs can be a bore. With a little bit of creativity it doesn't have to be so boring. Try to entertain yourself. Think of new and exciting ways to break your program with some input. Believe it or not, every time you manage to break your program gives you the opportunity to make it better." | ||
|Solution= | |Solution= | ||
− | Start by creating the Class. | + | ===The Basics=== |
− | + | Start by creating the Class. Name the class "MadLib". | |
+ | {{CodeBlock | ||
+ | |Code= | ||
public class MadLib | public class MadLib | ||
{ | { | ||
} | } | ||
− | + | }} | |
− | Next, | + | Next, make the main method. Put the main method between the curly-braces of the MadLib class. |
− | + | {{CodeBlock | |
+ | |Code= | ||
public static void main(String [] args) | public static void main(String [] args) | ||
{ | { | ||
} | } | ||
− | + | }} | |
+ | |||
For now it's not important to know just what each word means when writing the main method, but it must look like the code above in order to work properly. | For now it's not important to know just what each word means when writing the main method, but it must look like the code above in order to work properly. | ||
− | + | With these bare basics, the program will compile and run. Keep in mind, running doesn't necessarily mean doing any work. | |
− | Using comments | + | ===Make a plan=== |
− | * Create | + | This would be a good time to plan out how the program will run. Do this by adding some inline [[Comments]] to the code. This will help remember what we intend to do and when we're finished, will improve the readability of our code later. |
+ | |||
+ | Using comments outline the overall steps to the solution: | ||
+ | * Create the variables | ||
* Get the user's input | * Get the user's input | ||
* Show output (the completed Mad Lib) | * Show output (the completed Mad Lib) | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
public static void main(String []args) | public static void main(String []args) | ||
{ | { | ||
Line 54: | Line 98: | ||
// End of program | // End of program | ||
} | } | ||
− | + | }} | |
+ | |||
+ | ===Declare the variables=== | ||
+ | Store the words for the Mad Lib in strings. To do that, declare some strings and give the strings some logical names. | ||
+ | Using the [[Variables_and_Literals#Naming_Guidelines|naming standards]], name each variable something meaningful and distict. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | String pluralNoun; | ||
+ | String country; | ||
+ | String verb; | ||
+ | String noun; | ||
+ | }} | ||
+ | |||
+ | ===Getting user input=== | ||
+ | Next, get some input from the user. Use the [[JOptionPane Methods|JOptionPane]] library so the application can use Input Dialogs. In this example, use two methods from JOptionPane: showInputDialog and showMessageDialog. | ||
+ | |||
+ | At the top of the MadLib file, outside of the MadLib class, import the JOptionPane library | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | import javax.swing.JOptionPane; | ||
+ | }} | ||
+ | |||
+ | Getting input from the showInputDialog is easy. It will automatically construct a pop-up dialog with a text field. Also, whatever text is put into the text field will be returned as a string. All you need to do is pass showInputDialog the text you intend to show the user. Lets use showInputDialog to get a plural noun from the user. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | pluralNoun = JOptionPane.showInputDialog(null, "Please enter a plural noun:"); | ||
+ | }} | ||
+ | What the code is saying here: | ||
+ | * Reference the JOptionPane library and within it the showInputDialog method. | ||
+ | * The text from the text field will be stored in the variable pluralNoun which we created earler. | ||
+ | * The text to appear in the dialog box will be "Please enter a plural noun:" | ||
+ | |||
+ | Do the same thing three more times for the other strings. | ||
+ | |||
+ | ===Showing output=== | ||
+ | Similar to the way the showInputDialog is setup, use JOptionPane's showMessageDialog method to show the user the Mad Lib. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | JOptionPane.showMessageDialog(null, "The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + "."); | ||
+ | }} | ||
+ | Note the usage of a plus (+) to combine several strings into one. Remember to account for spaces between each of the parts of the phrase and the string variables. | ||
+ | |||
+ | ===We're done!=== | ||
+ | Finally compile, run and test the Mad Lib program. | ||
}} | }} |
Latest revision as of 15:14, 8 December 2011
Back to the Program-A-Day homepage
ProblemCreate a program which takes some text input from a user and then makes a Mad Lib out of it. Consider making this solution with the following guidelines:
The Mad Lib will follow this format: "The ___________ in _______ like to ____ the ____." plural noun country verb noun |
...By Students"I'm not ashamed to admit that while testing my version of the Mad Lib program, I put some of my favorite curse words I was willing to commit to text into my sample fields. Try it yourself!" "Testing is of course important. It's the only way to figure out if your program works. But, you'll soon find that testing your programs can be a bore. With a little bit of creativity it doesn't have to be so boring. Try to entertain yourself. Think of new and exciting ways to break your program with some input. Believe it or not, every time you manage to break your program gives you the opportunity to make it better." | |
---|---|---|
SolutionThe BasicsStart by creating the Class. Name the class "MadLib". public class MadLib { } Next, make the main method. Put the main method between the curly-braces of the MadLib class. public static void main(String [] args) { } For now it's not important to know just what each word means when writing the main method, but it must look like the code above in order to work properly. With these bare basics, the program will compile and run. Keep in mind, running doesn't necessarily mean doing any work. Make a planThis would be a good time to plan out how the program will run. Do this by adding some inline Comments to the code. This will help remember what we intend to do and when we're finished, will improve the readability of our code later. Using comments outline the overall steps to the solution:
public static void main(String []args) { // Create variables // Get input // Show output // End of program } Declare the variablesStore the words for the Mad Lib in strings. To do that, declare some strings and give the strings some logical names. Using the naming standards, name each variable something meaningful and distict. String pluralNoun; String country; String verb; String noun; Getting user inputNext, get some input from the user. Use the JOptionPane library so the application can use Input Dialogs. In this example, use two methods from JOptionPane: showInputDialog and showMessageDialog. At the top of the MadLib file, outside of the MadLib class, import the JOptionPane library import javax.swing.JOptionPane; Getting input from the showInputDialog is easy. It will automatically construct a pop-up dialog with a text field. Also, whatever text is put into the text field will be returned as a string. All you need to do is pass showInputDialog the text you intend to show the user. Lets use showInputDialog to get a plural noun from the user. pluralNoun = JOptionPane.showInputDialog(null, "Please enter a plural noun:"); What the code is saying here:
Do the same thing three more times for the other strings. Showing outputSimilar to the way the showInputDialog is setup, use JOptionPane's showMessageDialog method to show the user the Mad Lib. JOptionPane.showMessageDialog(null, "The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + "."); Note the usage of a plus (+) to combine several strings into one. Remember to account for spaces between each of the parts of the phrase and the string variables. We're done!Finally compile, run and test the Mad Lib program. | ||
Code |