Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2016



ASSIGNMENT 2

The goal of this assignment is to use basic input/output commands (Sytem.out.print and println, and the Scanner class for interactive user input), and in the process practice using conditionals and good programming style in Java. Just like last time, be sure you pay attention to your header comment and other comments, your indentation and variable names, and careful use of whitespace and curly braces. You can refer back to the style slides if you have questions on the details.


Part 1: MadLib
The first part of your assignment is to create a program that takes interactive user input to fill in a story, called a mad lib. You should name your program MadLib.java.

The user should be prompted for a word given certain characteristics. This input will be used to fill in the blanks of a paragraph.

For example, how boring is the first sentence on this assignment? Your program can make it more interesting by taking out some of the words and prompting the user for input. Let's see what we can do with this...

The goal of this assignment is to _______(verb) basic _________(noun) commands (____________(command) and __________(command), and the ___________(noun) class for ___________(adjective) input), and in the process practice using ____________(plural noun) and ___________________(adjective) programming style in Java.

You'll need to prompt the user for the words to fill in the blanks, and tell him/her what kind of word is expected. The expected kind of word is in parentheses for each blank above. Your output should follow the wording and format shown in the sample run below (although, of course, the user input words and the resulting output will be different if different words are entered):

Enter a verb: run
Enter a noun: school
Enter a command: stop
Enter another command: go
Enter a noun: horse
Enter an adjective: bad
Enter a plural noun: eggs
Enter an adjective: smelly
  
The goal of this assignment is to run basic school commands 
(stop and go, and the horse class for bad input), and in the
process practice using eggs and smelly programming style in Java.
  
Another run:
Enter a verb: run
Enter a noun: turnip
Enter a command: stop
Enter another command: don't go
Enter a noun: rubber ducky
Enter an adjective: yellow
Enter a plural noun: eggs
Enter an adjective: gross

The goal of this assignment is to run basic turnip commands
(stop and don't go, and the rubber ducky class for yellow input), and in the
process practice using eggs and gross programming style in Java.
Notice how the user is always prompted for input, and that the input shows up on the same line as the prompt. Your program should do the same.

My program's output isn't quite the same as yours for the example input above. Is that okay? No. If the exact same words are entered by the user, the output should be exactly the same.

Eclipse is giving me a warning that I have a resource leak with the variable I'm using as a scanner. What's up with that? In this small program it's not much of an issue, but it is good form to close an input source when your program is done with it. For example, if I declared my input (Scanner) variable like this:

Scanner keyboard = new Scanner(System.in);
At the end of my code, but before the closing curly brace for the main method, I could close it like this:
keyboard.close();
This will get rid of any complaints eclipse might have about resource leaks.

Part 2: Guessing Game
The second part of your assignment is to create a program that chooses a random number and gives the user up to three tries to guess the number. You should name your program GuessingGame.java. If the user guesses the correct number before the third guess, you should let him/her know and not make them guess again. If the user guesses either high or low, you should also let them know high or low. Finally, you should let the user know which of his/her guesses are correct, or, if they fail to guess the correct answer, your program needs to tell them the answer. (Uh-oh. This really does sound like a nested if problem, doesn't it?)

Your output should follow the wording and format shown in the four sample runs below:

First run:

I'm thinking of a number between 1 and 10.
You'll get three chances to guess my number.
Enter your first guess: 5
Too low. Try again: 8
You got it on your second try! 
Second run:
I'm thinking of a number between 1 and 10.
You'll get three chances to guess my number.
Enter your first guess: 5
Too high. Try again: 4
Still too high. Try again: 3
Sorry, you didn't guess the number. It was 1 
Third run:
I'm thinking of a number between 1 and 10.
You'll get three chances to guess my number.
Enter your first guess: 5
You won on the first try! 
Fourth run:
I'm thinking of a number between 1 and 10.
You'll get three chances to guess my number.
Enter your first guess: 5
Too low. Try again: 9
Too high. Try again: 7
You got it on your third try! 
Notice how the game is first explained to the user, the user is always prompted for input, and that the input shows up on the same line as the prompt. Your program should do the same.

I forgot. How do I generate a random number between 1 and 10? Use the following line to get a random number in that range (pay attention to all the parentheses in this):
int num = (int) ((Math.random( )* 10) + 1);
The random numbers that I get are not the same as in your examples. Is this OK? Yes, that is very likely to happen. In fact, it would be strange if your randomly generated numbers were the same as mine. And the user response will also likely be different from my examples.

My program's output isn't quite the same as yours for the example input above. Is that okay? Yes and no. The wording and formatting should follow the examples, but the user input and the flow of the guessing game will likely be different.


Submission. Submit both programs GuessingGame.java and MadLib.java via Moodle. Be sure each submitted source file has the required header with your name, email address, the date, and a description of the program. Also be sure to submit the .java source files and not the .class files!

Page last updated: September 09, 2016