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();
    
    		while(i>0){
    		  System.out.println(r.nextInt());
    		  i--;
    		}
    	}
    }
    

    So execution on the command line yields:
    # java randomnum
    -1710192698
    372834272
    561813874
    964075960
    -543878181
    -518949113
    -201238297
    562882756
    -658754567
    2144570707
    -1321395011
    -273056203
    -234056158
    -1293542699
    -1337926200
    265104174
    1380461933
    588544298
    -736641255
    1568232921
    

    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:

    System.out.println(r.nextInt()%100);

    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.

    	while(i>0){
    	   ...
    		while(h>0){
    		   ...
    		}
    	   ...
    	}
    

    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
    generated.
    Note: You're free to work on
    other text-based Java projects
    too, if you have an idea that
    appeals to you more.
    
    Steps:
  • 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:

    Thrash
    Scat