Rock and randomness w/Java

  • Don't forget to begin reviewing for the mid-term Wed. Mar. 12
  • A solution to last Monday's assignment.

  • Using the Java Random class, you can generate a bunch of pseudo-random numbers. Eg.:
    import java.util.*;
    class randomnum {
    	public static void main(String arg[]){
    	  int i = 20;
    	  Random r = new Random();

    So execution on the command line yields:
    # java randomnum

    For a bounded number, you could use the %, or modulo operator, which gives you the remainder of a division operation. A random number r operated upon by modulo, r%10, for example, will not be greater than 9. Try adding %100 to the print statement:


    We might also want to make the numbers positive. The Java Math class has an absolute value method that makes any negative number positive:

    int posNum = Math.abs(r.nextInt());

    So now suppose we want to write a program that will write rock music lyrics. First we want an array of possible words, which generally are limited to:

    String lyric[] = {"I", "you", "love", "hurt", "sad", "rock", "do", "baby", "man", "car", "sex", "rockin", "smokin", "forever" };

    Next we would need a way to generate the index numbers, between 0 and lyric.length.

    Random r = new Random();

    int i = Math.abs(r.nextInt()%lyric.length);

    Here, the things inside the Math.abs() parentheses will be evaluated prior to returning the number to the i variable.

    The end program requires also a nested loop to give us sentences of 5 words, and periods at the end of each.


    With such a nested loop, we can do a repetitive task, such as printing a sentence, within the while(h>0){} loop, and do it as long as i>0 (outer loop). Remember to increment both! If you don't, it will get stuck on one or the other.
    The source for our rock music generator.

    Several samples:

    # !!
    java rocklyric
     smokin baby you do car.
     I (Guitar solo) rock ooo! (Guitar solo).
     sad forever sex smokin sex.
     sad forever ooo! you smokin.
     sad uh-huh I uh-huh ooo!.
    # !!
    java rocklyric
     sad sad do hurt man.
     (Guitar solo) hurt baby sex smokin.
     rock rockin hurt I rockin.
     sad sad smokin love smokin.
     hurt do sex sex do.

    For Mon. Mar. 10, 
    Improve upon the rock music
    generator, perhaps making it
    produce other genres of music
    when given input strings, then
    convert it into a text/html utility 
    that is invoked from a .cgi script.
    You might use a form to invoke
    it so that the person on the other
    end chooses the genre of music
    that she/he would like to see
    Note: You're free to work on
    other text-based Java projects
    too, if you have an idea that
    appeals to you more.
  • 1. Initialize a word array for another genre
  • 2. Create an if(){ } else{ } branch that evaluates the input from argv[0] (remember to check its .length property so we know it's not zero)
  • 3. place genre specific tasks within these branches of the if statement
  • 4. Remember to add the Content-type print statements at the beginning of the program, since this will be a CGI program.
  • 5. Create a script, like j2_invoker.cgi, that will invoke the program
  • 6. Create a form: