5.7. Case Study - Computing Taxes with Methods

 
[Page 145]

5.10. Case Study: Generating Random Characters

Computer programs process numerical data and characters. You have seen many examples that involve numerical data. It is also important to understand characters and how to process them. This section presents an example for generating random characters.

As introduced in §2.9, every character has a unique Unicode between and FFFF in hexadecimal ( 65535 in decimal). To generate a random character is to generate a random integer between and 65535 using the following expression (note that since 0.0 <= Math.random() < 1.0 , you have to add 1 to 65535 ):

 (   int   )(Math.random() * (   65535   +   1   )) 

Now let us consider how to generate a random lowercase letter. The Unicodes for lowercase letters are consecutive integers starting from the Unicode for ' a ,' then for ' b ', ' c ', ... , and ' z .' The Unicode for ' a ' is

 (   int   )   'a'   

So a random integer between (int)'a' and (int)'z' is

 (   int   )((   int   )   'a'   + Math.random() * ((   int   )   'z'   - (   int   )   'a'   +   1   ) 

As discussed in §2.9.4, all numeric operators can be applied to the char operands. The char operand is cast into a number if the other operand is a number or a character. Thus the preceding expression can be simplified as follows :

   'a'   + Math.random() * (   'z'   -   'a'   +   1   ) 

and a random lowercase letter is

 (   char   )(   'a'   + Math.random() * (   'z'   -   'a'   +   1   )) 

To generalize the foregoing discussion, a random character between any two characters ch1 and ch2 with ch1 < ch2 can be generated as follows:

 (   char   )(ch1 + Math.random() * (ch2  ch1 +   1   )) 


[Page 146]

This is a simple but useful discovery. Let us create a class named RandomCharacter in Listing 5.6 with five methods to get a certain type of character randomly . You can use these methods in your future projects.

Listing 5.6. RandomCharacter.java
 1   public class   RandomCharacter {  2  /** Generate a random character between ch1 and ch2 */  3    public static char   getRandomCharacter(   char   ch1,   char   ch2)  {  4   return   (   char   )(ch1 + Math.random() * (ch2 - ch1 +   1   ));  5    }  6  7  /** Generate a random lowercase letter */  8    public static char   getRandomLowerCaseLetter()  {  9   return   getRandomCharacter(   'a'   ,   'z'   ); 10    } 11 12  /** Generate a random uppercase letter */  13    public static char   getRandomUpperCaseLetter()  { 14   return   getRandomCharacter(   'A'   ,   'Z'   ); 15    } 16 17  /** Generate a random digit character */  18    public static char   getRandomDigitCharacter()  { 19   return   getRandomCharacter(   '0'   ,   '9'   ); 20    } 21 22  /** Generate a random character */  23    public static char   getRandomCharacter()  { 24   return   getRandomCharacter(   '\u0000'   ,   '\uFFFF'   ); 25    } 26  } 

Listing 5.7 gives a test program that displays one hundred lowercase letters.

Listing 5.7. TestRandomCharacter.java
 1   public class   TestRandomCharacter {  2  /** Main method */  3   public static void   main(String args[]) {  4   final int   NUMBER_OF_CHARS =   175   ;  5   final int   CHARS_PER_LINE =   25   ;  6  7  // Print random characters between '!' and '~', 25 chars per line  8   for   (   int   i =     ; i < NUMBER_OF_CHARS; i++) {  9   char   ch =  RandomCharacter.getRandomLowerCaseLetter()  ; 10   if   ((i +   1   ) % CHARS_PER_LINE ==     ) 11          System.out.println(ch); 12   else   13          System.out.print(ch); 14      } 15    } 16  } 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net