Date Assigned: Monday, February 11, or Tuesday,
February 12, 2008.
Due Date: Monday, February 18, or Tuesday, February 19, 2008, before
the beginning of your lab section.
Objective: The main objective of this assignment is to practice
using conditional statements.
Programming assignment: Use JOptionPane to design a
simple English language sentence generator with a random component, so that the
user never knows quite what to expect for the output sentence, unless one
“special” name is entered. This sentence generator should read expressions
composed of a person’s name followed by a positive integer between 1 and 9999
enclosed in parentheses, such Cupid(1442). For output, it should produce a complete
sentence about the person, such as “Cupid is a great Java programmer!” The
procedure for completing the sentence is as follows:
·
The program
should pass the input integer as a “seed” to a pseudo-random number generator
object (see below).
·
The random number
generator will use the seed to produce a single-digit integer between 0 and 9
as output.
·
Write your
program so that the sentence completion is based upon the output random
integer, or a match with the “special” name.
·
To reduce the
number of sentences that you have to code, you may use one sentence completion
for multiple random integers or “special” name match, as shown in the examples
below:
Random number is 0, 1, or
2: “ is new to Java,
and so his code is sometimes off target.”
Random number is 3, 4, or
5: “ is right
on the mark with his Java conditional statements.”
Random number is 6, 7, or
8: “ really
knows how to pair up a great algorithm with great code!”
Input name matches defined
“special” name, or random number is 9:
“, you’re the best!”
Hints: You already know most of the tricks that you need to
build such a sentence generator:
To Add: For this lab add the following new concepts:
import java.util.Random;
·
Add these statements to your code to create a new
pseudo-random number generator object, using class Random:
Random rand = new Random(number); //number should be assigned the input integer value
randNum = rand.nextInt(9); //.nextInt(9)
will return an integer from 0-9
Homework
assignment: on a separate sheet of paper, solve Ex. 10 from p. 218 and
Ex. 12 from p. 219.
Deliverables:
as instructed by your TA.
Extra Credit: In addition to this assignment, write the program using a switch statement rather than if-else statements for the sentence completions, and add a check for valid input integers between 1 and 9999. Display an error message if the input number is out of range.