Homework 3



Due: 11:59 pm on Friday, September 24, 2004

Reading: online text, week 3

3 problems, 50 points total (10 points for #1, 20 points each for #2,#3)

Problem 1     the lyricist     [topic: using variables]

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.

If you wish, your program may be specific about the words or phrases it wants. 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 emails

What is your favorite color?  blue
What is your favorite poet?  Yeats
What is the name of a significant other?  Rita

---------------------------

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 Rita'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.







Problem 2     Menu of Mathematical Morsels ?     [topic: "if" and mathematical functions]

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.

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:


Test every new behavior you add to your program immediately after adding it!
Don't wait until your program is "finished" to compile and test it!


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 prvided function H.randInt(A,B). This function returns an int between A and B, inclusive. For example,

  int r = H.randInt(1,3);
  H.pl("r is " + r);
will print the value of a random integer out of 1, 2, or 3. You can then use this value, along with an if - else if - else chain to create different messages.

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:

  1. a double named L (representing the length of a pendulum)
  2. a double named a (representing the angle of a pendulum)
  3. a double named x (just another variable)
Then, your program should print out the value of the following.

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 available from 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). 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. Feel free to choose reasonable names for any variables you might use.

Option 4
If the user chooses option number 4, the program should prompt for each of the following:

You should store each of these inputs into variables of type int. Feel free to choose reasonable names for the variables you use.

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 datatype (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.


    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

    You look tired... how about a nap?!

--------------------------------------------------------------

    <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

Be sure to submit your CS5App.java file under Homework 3, problem 2.







Problem 3     rock, paper, scissors     [topic: using "if" and conditional statements]

For this problem, you'll write the game of rock-paper-scissors or "Rochambeau" (spelled in various ways). There is a surprisingly active rock-paper-scissors community online (e.g., try the World RPS Society). The important computational piece of this program is the use of conditionals ("if" statements) to determine who wins or loses the game.

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 "scsissors" (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 iterface, if you like). If you're reading online, the user's input in these cases is 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 ?  rock

You show  paper
  I show  rock

  You win!

Hints

Be sure to submit your CS5App.java file under Homework 3, problem 3.





Extra Credit

There are two parts (each worth 10%) to this week's extra credit.

Part One

The first part is to use as few comparisons as possible to find the minimum and the maximum of the four integers in problem #4. A comparison is any code that compares two values, e.g.

  if ( a < b && c < d )
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.

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 we will count the maximum number of comparisons that are actually executed for any legal input.

Finding the minimum & maximum efficiently in lists and, as a related problem, sorting lists efficiently are two widely studied problems. They are some of the few problems for which a lower bound on the number of comparisons needed is known. That is, we know how difficult sorting && finding the min/max are.

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 the fundamental contribution of CS to other sciences.

Part Two

This week's second part to the extra credit is to write an extended rock-paper-scissors program called rock-paper-scissors-spock-lizard. The relationship among these five adversaries is summarized in the following diagram:



There is nothing extra to submit for these extra credit problems -- we will check your submissions for problems #2 and #3 to see if you've tried the extra credit.