Computer Science 202
Introduction to Programming
Fall 2013, The College of Saint Rose
In this lab, you will write a program to play something like the popular children's word game called "Mad Libs". A few minutes with the results from your favorite search engine will explain the concept for those unfamiliar.
We will start by going step-by-step to develop a Java program to play a very simple and boring Mad Lib, then you will be on your own to create a more fun and interesting Mad Lib in a second Java program.
You may work alone or with a partner on this lab.
Getting Set Up
Follow the usual procedure to create a new BlueJ project called Lab3. Create a new "Application" class within the project, called LittleMadLib.
A Simple Mad Lib
We will begin by writing a program in class LittleMadLib to play the following simple Mad Lib:
I never thought I'd see <positive whole number> <plural form of a kind of animal> in <a place name>!
The idea is that you ask for the type of information in each item inside the angle brackets without giving the context, and then print the (ideally hilarious) sentence that results from substituting in the responses.
So in this case, if someone enters 12 when asked for a <positive whole number>, "elephants" when asked for a <plural form of a kind of animal>, and "Albertus Hall" when asked for <a place name>, the hilarious sentence would be:
I never thought I'd see 12 elephants in Albertus Hall!
Most of what you need to do will follow class examples. You'll need a Scanner to read input from the keyboard, you'll need to prompt for and read information from the Scanner and save the responses in variables, and then you'll need to print out the final sentence using the saved responses in their appropriate positions.
There is one thing you will probably want to use that we haven't seen yet in class examples. In some cases (like when I used "Albertus Hall" above), you want to read in an entire line of input as a String rather than a single word.
We previously used the Scanner's next method to read in a single word. For example, in the HelloYou example, we have the line:
String name = input.next();
If someone types in multiple words, the variable name will get only the first word typed in.
There is another Scanner method that will help us here. If instead we say
String name = input.nextLine();
the variable name will get the entire line typed in at the keyboard (our Scanner in this case is called input), even if it includes multiple words (you might try this out by modifying the HelloYou example).
Unfortunately, there is one complication if we want to make use of the nextLine method in a program that also uses some of the other Scanner methods, in particular nextInt and nextDouble. If we try to use a nextLine right after, say, a nextInt, we will find that the nextLine call will return to us anything on the previous input line after the number returned by nextInt up to the new line, even if there is nothing there! While this is a perfectly reasonable behavior for the Scanner (it has no reason to believe that anything else you may have happened to type on the previous input should be ignored), it is pretty annoying for us in this case.
Fortunately, there is an easy solution. We can add an extra line to our program after each line that uses nextInt or nextDouble that will cause the Scanner to "consume" any remaining input on the same line as our numeric input. We can just add another nextLine call!
So anywhere in your program you have nextInt or nextDouble, you can add
input.nextLine();
(assuming the Scanner is named input) on the line immediately following any Scanner method call that does not consume the entire line. This will read in the rest of the line, but not store it in any variable. While this may seem strange, it is exactly what we want to do. Then when the next Scanner method is called, it will use the brand new line you type in in response to the next prompt.
So this program will require you to declare and construct a Scanner, declare three local variables, one int and two Strings, each of which will get its value from the result of a Scanner method call, and use one or more System.out.println calls to print the resulting sentence.
Demonstrate your working LittleMadLib.java program before proceeding to the next section. You will not need to submit your code for this part, however.
A More Interesting and Fun Mad Lib
Now that you have the idea how to write a program like this, develop your own Mad Lib with more words or phrases to be filled in and write a second Java program to play the game. To get started with it, create a new "Application" class in your project called MadLibs. This class, in the file MadLibs.java, is the only one you will need to submit at the end.
You can make your Mad Lib as long and complicated as you wish, but it must meet these minimum complexity requirements:
Bonus points will be available for especially creative or potentially funny Mad Libs.
Submitting
Before 11:59 PM, Thursday, October 10, 2013, submit your Java program for grading. There are three things you need to do to complete the submission: (i) upload a copy of your Java program (the .java file only) using Submission Box under assignment "MadLibs", (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor (2-day grace period for demos).
Don't forget to check your programs for compliance with the Style Guide for CSC 202 Programs before you submit!
Grading
This assignment is worth 25 points, which are distributed as follows:
> Feature | Value | Score |
MadLibs.java prompt for and read in items | 4 | |
MadLibs.java at least 8 items | 4 | |
MadLibs.java at least 5 different "kinds" of items | 3 | |
MadLibs.java at least 2 numeric values | 2 | |
MadLibs.java print resulting story | 4 | |
Appropriate comments | 3 | |
Good variable names | 3 | |
Correct file name and good formatting | 2 | |
Creativity bonus | up to 2 | |
Total | 25 | |