Difference between revisions of "Mad Lib"

From CompSciWiki
Jump to: navigation, search
(Added some content. More writing to be done of course.)
(more text)
Line 23: Line 23:
 
|Solution=
 
|Solution=
 
Start by creating the Class. We're going to call our class "MadLib".
 
Start by creating the Class. We're going to call our class "MadLib".
<pre>
+
<pre>public class MadLib
public class MadLib
+
 
{
 
{
}
+
}</pre>
</pre>
+
  
 
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.  
 
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.  
<pre>
+
<pre>public static void main(String [] args)
public static void main(String [] args)
+
 
{
 
{
}
+
}</pre>
</pre>
+
 
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.
  
Line 43: Line 39:
 
* Get the user's input
 
* Get the user's input
 
* Show output (the completed Mad Lib)
 
* Show output (the completed Mad Lib)
<pre>
+
<pre>public static void main(String []args)
public static void main(String []args)
+
 
{
 
{
 
// Create variables
 
// Create variables
Line 53: Line 48:
 
 
 
// End of program
 
// End of program
}
+
}</pre>
</pre>
+
 
 +
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 [Variables_and_Literals#Naming_Guidelines|naming standards] we'll name each variable something meaningful and easy to differentiate from one-another.
 +
<pre>String pluralNoun;
 +
String country;
 +
String verb;
 +
String noun;</pre>
 +
 
 +
Next, we need to get some input from the user. We're going to use the [JOptionPane Methods|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.
 +
<pre>pluralNoun = JOptionPane.showInputDialog("Please enter a plural noun:");</pre>
 +
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:"
 +
 
 +
We're not finished yet, but now would be a good time to compile and run the program.
 
}}
 
}}

Revision as of 21:01, 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:

  • JOptionPane with input and output.
  • Input with Strings.
  • Commenting our code.

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

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.

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
}

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 [Variables_and_Literals#Naming_Guidelines

Code

Solution Code

Back to the Program-A-Day homepage