PA2
James Madison University, Spring 2017 Semester
PA2: Madison Men (Methods and Testing)

Due: Friday, Feb 3 at 11:59 PM
- -20% on Saturday, Feb 4
- -40% on Sunday, Feb 5
- Not accepted afterwards
Objectives
- Organize input/output code into multiple methods.
- Write proper documentation comments for methods.
- Format numerical data to fixed precision for display.
- Use the command line to test program correctness.
Honor Code
This assignment should be viewed as a take-home exam and must be completed individually. Your work must conform to the JMU Honor Code. Authorized help is limited to general discussion on Piazza, the lab assistants assigned to CS 149/159, and the instructor. Copying work from another student or the Internet is an Honor Code violation and will be grounds for a reduced or failing grade in the course.
Background
The Madison Men ad agency (Profs. Mayfield and Chao) has contracted with you to help create unique and compelling one-line phrases for the JMU homepage. Building upon the idea of Madison Libs used in PA1, you will implement "Mad Lib style" overlay text that could be used in the slide show (see example of a percentage slide below).

Getting Started
In contrast to the first assignment, much of your code for PA2 will not be in the main method. Create a file named MadisonMen.java and make sure to add proper Javadoc commenting including a description, @author, @version, and acknowledgements (if applicable). Then add the following methods to your program, in this order, above main:
-
public static double inputDecimal(String prompt)
-
public static int inputInteger(String prompt)
-
public static String inputLine(String prompt)
-
public static void outputFact1(int percent, String school, double number)
-
public static void outputFact2(int count, String noun, String phrase)
-
public static void outputFact3(int year, double amount, String thing)
You will still have a main method as before, but it will primarily call the other methods to do most (but not all) of the work. In particular, main should NOT use a Scanner at all. As before, many variables (but not all) will be declared in main. Make sure you do not duplicate any effort between your main method and the supporting methods.
A major part of this assignment will be to write the method documentation comments, with short descriptions, including @param
and @return
tags where applicable. The next section explains what each individual method does; carefully read these instructions and rewrite them in your own words in the documentation comments.
Method Details
The input methods (1) display the given prompt followed by a colon and a tab character, (2) use a Scanner to read in the appropriate type of data, and (3) return the resulting user input. In addition, inputDecimal and inputInteger automatically consume the rest of the line (i.e., the \n
character). In the following example, inputInteger returns the value 18 (which causes 18 to be assigned to the variable age), and the Scanner is ready to read the next line of input (not shown).
Example Code | Output/Input |
---|---|
int age; age = inputInteger("Enter your age"); |
Enter your age:
<tab>
18
<newline>
|
The output methods print a single sentence after filling in the applicable details. All double values need to be formatted to one decimal place. The three "facts" are:
-
[percent]% are employed, in [school], or have internships [number] months after graduation.
-
Over [count] JMU students do research, take care of [noun], or [phrase] on campus.
-
The class of [year] accepted [amount] jobs (or [thing] offers) prior to graduation.
Example Code | Output |
---|---|
outputFact2(149, "stray cats", "tap dance"); |
Over 149 JMU students do research, take care of stray cats, or tap dance on campus. |
The main method should call the other methods to gather user input and display the resulting facts. Each fact method will be called twice; see the test input and output files below for the specific prompts and the order and format of all input and output. Declare all variables at the top of main (before calling any of the methods), and write at least two inline comments to break up the code.
One Caveat
To divide input across multiple methods, you will need to create a single Scanner that is shared across the entire class. Copy/paste the following line directly beneath the class declaration (i.e., make it the first line inside the class, above all methods):
private static Scanner input = new Scanner(System.in);
The private
keyword means the variable is visible only to this class. The static
keyword means the variable will be shared by all instances of the class. (We'll learn more about these concepts later in the semester.)
As in the previous assignment, there is no need to handle invalid user input (e.g., the user typing in a string when you're expecting a double).
Submission
Before submitting your MadisonMen.java file, be sure to complete the following steps carefully.
-
Test your solution with
diff
and/ormeld
using the following input and expected output files:test1.in
test1.exp
-
Run Checkstyle and eliminate ALL warnings about your code. Review and update comments as needed.
-
Make sure constants and variables are declared at the beginning of each method, before other statements.
Your submission will be graded using the following criteria:
Requirement Points Readiness quiz 10 Input methods 25 Print methods 25 Passes diff testing 20 Passes Checkstyle 20