Problem Set 3
Up Problem Set 0 Problem Set 1 Problem Set 2 Problem Set 3 Problem Set 4 Problem Set 5 Problem Set 6 Problem Set 7 Problem Set 8 Problem Set 9 Problem Set 10

 

More Control Structures

Due 11:59pm Monday October 2

Note the correction in red (9/29/06).

  1. Use a for loop to solve Program Design Exercise 17 in Chapter 4 of the text.
     
    Solution.
     

  2. A random number can be generated as follows.  First, create a random number generator like this:
     
        Random randGen = new Random();
     
    Then if we do:
     
        int r = randGen.Next(10);
     
    We will get a random number between 0 and 9, inclusive.  Every time we call randGen.Next(10) we will get a new random number between 0 and 9.  If we replace 10 with a different integer n, then the random number generated will be between 0 and n-1, inclusive.

    bullet

    Use this idea, along with a few switch statements, to write a method that generate a random sentence every time that it is called,  that is grammatically (if not semantically) correct.  Make this as fancy as you wish.  For example, you might use the sentential form "At that moment <subject> <verb> the <adjective> <object>" to generate sentences like "At that moment Paul ate the green pencil", or "At that moment Suzie loved the fast chair", or whatever.
     

    bullet

    Create at least three kinds of sentential forms such as the above (i.e. create at least three different methods), and then invoke them randomly to generate random paragraphs (not unlike you see in certain kinds of email spam).  For example, the output of your overall program might generate the following different results:
     
        Suzie loved the chair.  Then out of nowhere a dog talked to the wall.  Being green, Paul then became cold.
     
        Being angry, Fred then became red.  Tom ate the paper.  Then out of nowhere a chair barked at the microwave.

    Note that the problem does not require any user input, but you may wish to add a query such as "Are you ready for another random paragraph?" to which the user replies yes or no, thus generating a new paragraph, or not.

    Have fun with this one -- show your creative side! :-)
     
    Solution.