Mad Lib

From CompSciWiki
Jump to: navigation, search

Back to the Program-A-Day homepage

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:

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."

Solution

The Basics

Start 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 plan

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
  • Show output (the completed Mad Lib)
 public static void main(String []args)
{
	// Create variables

	// Get input
	
	// Show output
	
	// 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 naming standards, name each variable something meaningful and distict.

 String pluralNoun;
String country;
String verb;
String noun; 

Getting user input

Next, 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:

  • 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.

 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

Solution Code

Back to the Program-A-Day homepage