Difference between revisions of "Mad Lib"

From CompSciWiki
Jump to: navigation, search
(Added a (weak) conclusion. I'll talk to Penner about this later.)
Line 4: Line 4:
 
|Problem=Lets create a program which takes some text input from a user and then makes a Mad Lib out of it.
 
|Problem=Lets create a program which takes some text input from a user and then makes a Mad Lib out of it.
 
We're going to look at making this solution with the following:
 
We're going to look at making this solution with the following:
* 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:

Revision as of 21:56, 5 April 2010

Back to the Program-A-Day homepage

Problem

Lets create a program which takes some text input from a user and then makes a Mad Lib out of it. We're going to look at making this solution with the following:

The Mad Lib will follow this format:

"The ___________ in _______ like to ____ the ____."
     plural noun    country         verb     noun
 

...by students

An image or By Students section

Solution

The Basics

Start by creating the Class. We're going to call our class "MadLib".

public class MadLib
{
}

Next, we can make our main method. Put the main method between the curly-braces of the MadLib class. When we run our finished program, whatever code we write into the main method will run.

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.

Now that we have the bare basics for our program to compile and run. Keep in mind, running doesn't necissarily mean doing any work. This would be a good time to plan out what we intend to do with our program. We'll do this by adding some inline Comments to the code. This will help us remeber what we intend to do and when we're finished, will improve the readability of our code later.

Make a plan

Using comments lets outline the overall steps to our application:

  • Create our 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

We want to store our words for the Mad Lib in strings. To do that, let's declare some strings. We need to give the strings some logical names. Remeber we need one string for each of the following:

  • a plural noun
  • a country
  • a verb
  • a noun

Using our naming standards we'll name each variable something meaningful and easy to differentiate from one-another.

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

Getting user input

Next, we need to get some input from the user. We're going to use the JOptionPane library so we can use Input Dialogs to get user input. In this example, we're going to use two methods from JOptionPane: showInputDialog and showMessageDialog.

Getting input from the showInputDialog is easy. It will automatically construct a popup 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("Please enter a plural noun:");

What we're saying here is:

  • We're referencing the JOptionPane library and specifically 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:"

Lets do the same thing three more times for the other

Lets see what we've done so far

We're not finished yet, but now would be a good time to compile and run the program. All we should do now is ask the user a bunch of questions but not do anything with the answers.

Showing output

Similar to the way we set up the showInputDialog, we'll use JOptionPane's showMessageDialog method to show the user the Mad Lib we've created. Since showMessageDialog doesn't output anything, we can just call it and tell it what text to show.

JOptionPane.showMessageDialog("The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + ".");

Note how we can use a plus (+) to combine strings into one. Remember to account for spaces between each of the parts of the phrase and the string variables.

We're done!

Finally lets compile, run and test the Mad Lib program.

Code

Solution Code

Back to the Program-A-Day homepage