This problem asks you to build another intelligent program -- and this time it will actually use the input that a human provides it.
The idea is this: your program should ask the user for a few -- but at least four -- words or phrases It then should use those words or phrases in building a song (or some other work, poem, story, phrase, conversation, "Mad Libs", etc.) that uses those words or phrases. Note: Imagine having to grade 160 of these programs! Be creative --- give the graders a belly laugh --- exert your imagination --- have fun!
If you wish, your program may be specific about the words or phrases it wants (and this makes it easier for your program to appear intelligent). For example, the Alanis Morissette example below asks for something the user really hates... .
Uninspired?
Then you should feel free to emulate the following
Alanis Morissette lyric generator
(http://www.brunching.com/alanislyrics.html).
Here's a sample
run -- if you read this assignment online, you will see the
user's inputs are in blue. Later, when the computer
prints them again ("madlibs style"), they are highlighted
in green.
Welcome to virtual Alanis.
In order to create a song, I will need to ask a few questions:
What is one thing you really hate?
spam
Type three plural nouns related to that thing:
Hit return after each one.
tin cans
camping trips
unwanted email
What is your favorite color? blue
What is your favorite poet? Yeats
What is the name of a significant other? John Doe
---------------------------
I Think
I think tin cans are a really huge problem
I think camping trips are too much on my mind
I think unwanted emails are bringing the world down
But what can you do?
Like a blue rain, beating down on me
Like a Yeats line, which won't let go of my brain
Like John Doe's voice, it is in my head
Blame it on spam
Blame it on spam
Blame it on spam
Be sure to submit your CS5App.java file
under Homework 3, problem 1. If your Homework 2 Problem 4 ("Artificial
Intelligence") submission satisfies these requirements, feel free
to submit it instead. Or, you can alter that
program if you'd like.
This problem asks you to write a text-menu interface to a number of "useful" mathematical computations.
Your program should start off by presenting the user with the
following menu of choices (with just a number
of H.pl statements):
(0) A friendly (and random) message
(1) Power computing!
(2) Computing Prof. Saeta's physics function.
(3) Converting from seconds
(4) Converting to seconds
(5) Find the max and min of four different integers
Choose an option (0-5):
After printing the menu, the program should get an integer from the user. The user might type an integer outside the range from 0 to 5. If this happens, your program should simply suggest that the user pay more attention in the future -- in this case, it should not actually do any of the menu options. Note that, in this situation, you are actually ensuring that the user's input is valid first. Such checks are important and are one of the main reasons why user interface programming can become an involved enterprise.
Important!
As you write the code for this problem, do
not write the entire program and only then try to compile and test it!
Instead, write small, testable pieces of the program -- then compile
and test as often as you can. Catch and fix the inevitable
bugs incrementally, so that they're not overwhelming at the end.
In particular, test each option in the above menu as you write it... .
In fact, this advice is so important (not just for this assignment,
but for all assignments) that it bears repeating:
So, here are the six things (0-5) your mathematical-menu program should do.
Option 0
If the user chooses option number 0,
the program should print a random friendly message (for
your own definition of "friendly").
What is meant by random? This means that your program may not
print the same message every time it is run. That is, there
must be at least two messages that it might print, though you're
welcome to have more than two.
But how to print a random message? To do this, you will want
to use the randInt and/or randDouble
functions provided in the HMC Support file
documented here.
For example,
will print the value of a random number from 0 to 1 (including
0, but excluding 1). You'll want to extend this example
using if or switch to make different choices about what
messages to print out.
double d = H.randDouble(0,1);
H.pl("d is " + d);
Option 1
If the user chooses option 1, the program should prompt the user for
two more pieces of information, a base named b
(of type double) and
a power named p (of type int). It should then print out the
value of
. To do this, it should use the
built-in Math.pow function. As an example of how this function
is used, Math.pow(2,3) returns 2 to the third power, or
8.0.
Option 2
If the user chooses option number 2, then the program should
prompt the user for three doubles:

This formula is the time it takes for a pendulum to make one swing back and forth -- well, almost.
Your program should print the result using three places of precision
to the right of the decimal point. (You may want to remind yourself about
the H.fmt function on the
HMCSupport page.)
You will need to use the Math.sqrt and Math.sin
functions that were mentioned in class. To take powers, you can
use Math.pow or simply use repeated multiplication.
Option 3
If the user chooses option number 3, the program should
prompt for a number of seconds (an int whose value you can assume is
positive).
Your program should then
find and print out the number of years, days, hours, minutes,
and seconds that the
input number of seconds represents. Consider a year to be 365 days, that is,
do not worry about leap years!
Of course, the number of seconds or minutes printed should not be more than 59, the number of hours should not be more than 23, and the number of days should not be more than 364. See the sample runs below for an example of a reasonable format. Remember to choose reasonable names for any variables you would like to use.
Option 4
If the user chooses option number 4, the program should
prompt for each of the following:
int.
You may also assume that valid (positive) values for years, days, hours, etc. have been
entered by the user.
Your program should then print out the number of seconds in the
total time span specified by all of those inputs.
You may assume that the result will fit into an
int data type (we'll stick to less than 60 years).
Also, you should assume (as in part 3) that
a year is 365 days -- ignore leap years.
See below for a couple of example runs.
Option 5
If the user chooses option number 5, the program should
prompt for four integers. Your program should then print out
the minimum integer of the four and the maximum integer of the
four (using if statements to determine this).
The extra credit for this week is to do this in as few
required comparisons as possible. You can assume that only
four different integers will be input.
Example Runs
Feel free to use this as a guide --
you may customize the user interface
as you wish (but be sure your program is getting the same numbers!)
Note, too, that this represents lots of separate runs of the
program: Each dashed line isolates one run from the next:
Welcome to the Menu of Mathematical Morsels
Please choose an option:
(0) A friendly (and random) message
(1) Power Computing!
(2) Computing Prof. Saeta's physics function
(3) Converting from seconds
(4) Converting to seconds
(5) Finding the max and min of four integers
Which would you like 0
Happy birthday (to me)!
--------------------------------------------------------------
Welcome to the Menu of Mathematical Morsels
Please choose an option:
(0) A friendly (and random) message
(1) Power Computing!
(2) Computing Prof. Saeta's physics function
(3) Converting from seconds
(4) Converting to seconds
(5) Finding the max and min of four integers
Which would you like 0
Isle of ewe!
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 1
You've chosen to find a power.
Please input a double for the base: 2.0
Please input an int for the power: 5
The value of 2.0 to the 5 power is 32.0
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 2
You've chosen to evaluate Prof. Saeta's crazy function.
Please input a double for L: 4.2
Please input a double for a: 1.57
Please input a double for x: 0.5
The result is 2.783
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 2
You've chosen to evaluate Prof. Saeta's crazy function.
You've chosen to evaluate Prof. Saeta's crazy function.
Please input a double for L: 9.8
Please input a double for a: 2.094395
Please input a double for x: 1.570795
The result is 7.999
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 3
Please input a number of seconds: 1000000000
1000000000 seconds is
31 years
259 days
1 hours
46 minutes
40 seconds
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 4
Please input a number of years: 31
Please input a number of days: 259
Please input a number of hours: 1
Please input a number of minutes: 46
Please input a number of seconds: 39
This is a total of 999999999 seconds
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 5
Please input 4 integers:
42
5
100
-1
The max is 100
The min is -1
--------------------------------------------------------------
<The menu has been omitted to save trees...>
Which would you like 6
This choice is not a valid option!
Be sure to submit your CS5App.java file
under Homework 3, problem 2.
Your program should welcome the user and then challenge them to a single match (not two-out-of-three or another variation). It should then choose randomly whether it will show rock, paper, or scissors. (Of course, it shouldn't tell its choice to the user!)
Next, it should prompt the opponent for their choice. The opponent should then be able to type in her/his choice. The program may assume that an appropriate choice was made, i.e., that the user will only type "rock", "paper", or "scissors" (without the quotes).
After the user inputs her or his choice, your program should echo that choice (for confirmation) and then reveal its own choice (rock, paper, or scissors). Finally, it should print out who won the round or indicate that it was a draw.
As a guide to a reasonable user interface, here are three runs of
my program. You're free to copy it (but may create your
own user interface, if you like). If you're reading
online, user input is displayed in blue:
Welcome! I am ready for a game of Rock-Paper-Scissors
What do you show - rock, paper, or scissors ? scissors
You show scissors
I show rock
I win!
Welcome! I am ready for a game of Rock-Paper-Scissors
What do you show - rock, paper, or scissors ? paper
You show paper
I show paper
It's a draw!
Welcome! I am ready for a game of Rock-Paper-Scissors
What do you show - rock, paper, or scissors ? paper
You show paper
I show rock
You win!
Hints
String
variable with another (variable or literal) String,
you need to use the .equals method. For example,
if ( yourchoice.equals("rock") && mychoice.equals("paper") )
{
// appropriate code goes here
// namely, that the computer wins...
}
Be sure to submit your CS5App.java file
under Homework 3, problem 3.
There are two parts (each worth 10%) to this week's extra credit.
Part One
The first part of the EC asks you to use as few comparisons as possible to find the minimum and the maximum of the four integers (Option 5 in Problem 4). Since this task involves part of this week's pair programming problem, it too should be done in pairs.
A comparison is any code that compares two values, e.g.
consists of two comparisons. Since you don't know
about the order in which the elements are input,
you'll need to use some comparisons to find the min and max.
if ( a < b && c < d )
Also, it's not the number of comparisons that appear
in your code that get counted -- after all, with
if statements, lots of them may not be used at all.
Instead what is counted is the maximum number of comparisons that
could execute for any pair of four numbers.
Finding the minimum & maximum efficiently in lists of items, and the related problem of sorting a list's items efficiently, are two widely studied problems in CS. They are some of the few problems for which a lower bound on the number of comparisons that are needed is known. That is, we know how difficult sorting and finding the min/max are. (In this context, how have we defined "difficult"?).
At the moment, we can't say that about a lot of other important problems (e.g., factoring numbers). In general, however, one could argue that creating efficient algorithms is one of the fundamental contributions that CS makes to other sciences.
Part Two
Whereas the first part of the EC dealt with minimizing the number of computations needed, this part asks you to minimize the complexity of your code in another sense: make it as elegant and simple as possible. In particular, you should minimize the number of if statements and/or switch cases needed, asking more "general" questions so that fewer specific cases need to be handled in determining the game's outcome. To summarize: write a program that is short, concise, and whose structural logic is crystal clear.Why is concise program design a good idea in general? If you place all the pieces that can be handled in a block into one block, what typically results is shorter (our version requires about 1 screen to view), easier to understand code. If more cases can be collapsed into the same block, there are also fewer blocks of code that might be incorrect (due to cutting and pasting incorrectly or whatever). My main motivation for assigning this part of the EC is that, to do it well, you'll need to think carefully about the overall structure of your code, as opposed to simply "sitting down and hacking it up, dorking with it until it works" (a common, albeit unrecommended, approach!).
There is nothing extra to submit for these extra credit problems. Just indicate that you've done the EC in the comments at the top of the appropriate files.