Saturday, November 12, 2011

Program to Create Five Random "Words"

Problem: Write a method randomWord that returns a randomly constructed “word” consisting of randomly chosen letters. The number of letters in the word should also be chosen randomly by picking a number between the values of the named constants MIN_LETTERS and MAX_LETTERS. Write a ConsoleProgram that tests your method by displaying five random words. (Roberts ch 10, problem 2).

What it looks like:



/* File: fiveRandomWords
 * --------------
 * This program generates random "words," just random letters stuck together. The characters of the word are 
 * random, but must be an upcase letter-- these are integers that correspond to the ASCII characters of the 
 * upcase alphabet. The length of the word is also random, but bounded by the constants MIN_LETTERS and
 * MAX_LETTERS.
 * 
 * The run method of the program simply starts of with blank String instance and keeps pulling random letters
 * out of the random generator until the word length is reached. It relies on a private method to generate the 
 * random characters and to set the word length.
 */

import acm.program.*;
import acm.util.RandomGenerator;

public class FiveRandomWords extends ConsoleProgram { 
    public void run() {
        for (int i = 0; i < 5; i++) {
            int wordLength = rgen.nextInt(MIN_LETTERS, MAX_LETTERS);
            String myRandomWord = " ";
            for (int j = 0; j < wordLength; j++) {
                myRandomWord += randomLetter();
            }
            println(myRandomWord);
        }
    }
    
    private char randomLetter() { 
        return (char) rgen.nextInt((int) 'A', (int) 'Z');
    }
    private RandomGenerator rgen = RandomGenerator.getInstance();
    private static final int MIN_LETTERS = 3;
    private static final int MAX_LETTERS = 10;
}
What made it tough:

This simple problem is all about getting comfortable with scalar types: data values that can be represented as numbers. The key snippet in the program is this:

 private char randomLetter() { 
        return (char) rgen.nextInt((int) 'A', (int) 'Z');
    }

How do you return a *character* that's random but bounded by the *integers* A and Z? Because the "char" data type in Java maps directly to Unicode and ASCII. Unicode and ASCII are both systems that match characters (letters, punctuations marks, etc) to integers so that it's easier for the computer to understand. 

Fun facts I learned: 
  • Precursors to ASCII literally mapped 'A' to 1, 'Z' to 26, and everything in-between, but ASCII didn't do so since there are additional characters that precede letters. 
  • The ASCII system came first, Unicode second. ASCII characters don't encode most non-Western characters, so Unicode was created with thousands of new characters to account for these.
  • Unicode contains everything that's in ASCII, and to be completely backwards-compatible, the first 128 digits of Unicode map directly to ASCII


Because characters are mapped this way, you can perform some numeric operations on *letters* as specifying a range like I did above, or taking a letter 'A' and adding 5 to get 'F.'  When specifying the range of characters, you could either provide the character or the number equivalent-- the computer can handle each one and in fact the (int) cast isn't necessary- it's there to make things clearer for humans so they don't think, "Why are you bounding nextInt with characters?" You must specify the (char) cast however to make sure that when you do rgen.nextInt, you get a letter back, not a number.


12 comments:

  1. Wow, that's what I was exploring for, what a information! present here at this web site, thanks admin of this web site.

    My website ... friedrich nietzsche quotes

    ReplyDelete
  2. I like what you guys are usually up too. This
    type of clever work and coverage! Keep up the awesome works
    guys I've you guys to my own blogroll.

    Review my web page ... ignorance quotes

    ReplyDelete
  3. I like what you guys tend to be up too. This sort of clever work and reporting!

    Keep up the superb works guys I've incorporated you guys to our blogroll.

    Have a look at my webpage brother and sister quotes

    ReplyDelete
  4. This text is priceless. How can I find out more?

    Also visit my weblog - feelings quotes

    ReplyDelete
  5. I read this article completely concerning the resemblance of hottest and previous
    technologies, it's amazing article.

    Feel free to surf to my page :: rolling stones songs

    ReplyDelete
  6. Everyone loves it when folks come together and share thoughts.
    Great website, stick with it!

    my web-site :: struggle quotes

    ReplyDelete
  7. I absolutely love your blog and find most of your post's to be what precisely I'm looking for.
    can you offer guest writers to write content available for you?
    I wouldn't mind composing a post or elaborating on a few of the subjects you write concerning here. Again, awesome site!

    My blog post; stephen hawking quotes

    ReplyDelete
  8. My developer is trying to persuade me to move to
    .net from PHP. I have always disliked the idea because of the
    expenses. But he's tryiong none the less. I've been using Movable-type on a number of websites for about a year and am anxious about switching to
    another platform. I have heard good things about blogengine.
    net. Is there a way I can transfer all my wordpress
    posts into it? Any kind of help would be greatly
    appreciated!

    Here is my web blog genghis khan quotes

    ReplyDelete
  9. Every weekend i used to pay a quick visit this
    site, because i wish for enjoyment, for the reason that this this web page conations genuinely fastidious funny stuff too.


    my webpage ... nora ephron quotes

    ReplyDelete
  10. It's going to be ending of mine day, but before end I am reading this impressive article to improve my knowledge.

    Also visit my blog post - country flags

    ReplyDelete
  11. I got this website from my pal who informed me concerning this
    website and at the moment this time I am browsing this
    website and reading very informative articles or reviews at this place.


    my weblog ... depressing quotes

    ReplyDelete