Sunday, July 31, 2011

Program to Draw a Random Card

Problem: Write a program that displays the name of a card randomly chosen from a complete deck of 52 cards. You program should display the complete name of the card. (Roberts ch 6, problem 1).

What it looks like (2 sample runs):

How the code looks:
/*
/*File: DrawCard.java
 * This program simulates drawing a card from a standard deck. It uses an instance of acm.util's 
 * RandomGenerator class to create two new random integers: one helping to supply the value (2-10, Ace, Jack, Queen, 
 * King), and the other integer helps determine the suit (Spades, Clubs, Hearts, Diamonds). Each integer value maps
 * to a card value or suit.
 *  * */
import acm.program.*;
import acm.util.*;

public class DrawCard extends ConsoleProgram{

    public void run() {
   
    int d1 = rgen.nextInt(1,4);
    int d2 = rgen.nextInt(1,12);
   
        String suit = null; //Declare a variable of null value. 
        //Then use switch statement to set a value
        switch (d1) {
            case 1: suit = "Spades"; break;
            case 2: suit = "Clubs"; break;
            case 3: suit = "Hearts"; break;
            case 4: suit = "Diamonds"; break;  
        }
   
        String value = null;
        switch (d2) { 
            case 1: value = "Ace"; break;
            case 11: value = "Jack"; break;
            case 12: value = "Queen"; break;
            case 13: value = "King"; break;
            default: value = Integer.toString(d2); break; // Returns a numeric value in string form
                // if a non-face card gets drawn
        }
        
    println("The " + value + " of " + suit);
   
    }
    //Instance variable for our random generator macheeeeen
    private RandomGenerator rgen = RandomGenerator.getInstance();
}

What made this interesting:

Two interesting concepts you exercise here:

1) Compartmentalizing your program. Instead of just storing 52 potential card values and randomly picking one of them, you can build something neater. There are 4 suits of equal distribution and 13 numeric values, so we can generate two random numbers for the face and numeric numbers and map them to card values. This is better than simply storing 52 potential card values because...

2) The concept of using an instance of a class to do stuff. In this case is a class of objects available to me called RandomGenerator. An instance of the RandomGenerator class is not one random number itself. It is an object that you can create in your program and then call upon to return random numbers. (It will recognize the method getInt() method). So the individual random numbers are a layer of abstraction away from the class that we get from the ACM library.

Why is this important?

This was actually familiar to me based on my work at Twilio. Twiio's REST API lets you make calls, send SMS messages, and pull information about your Twilio account. You can make a request to the API by a raw HTTP request to Twiio. Or, you can be clever and use a wrapper library that creates a "REST client". This is actually a class of objects that know how to make HTTP requests to Twilio, so instead of piecing together a big URL every time you need to communicate to Twilio, you can send commands to your instance of the REST client and communicate via methods instead of a raw URL.

Examples:

A1) Making a phone call directly via a direct HTTP POST to Twilio's API:

POST https://api.twilio.com/2010-04-01/Accounts/account123/Calls/
Auth Headers: username account123, password token456


POST parameters:
* To: 503-111-2222
* From: 503-222-3333
* Url: http://mycoolapp.renee.com


A2) Sending an SMS:


POST https://api.twilio.com/2010-04-01/Accounts/account123/SMS/Messages
Auth Headers: username account123, password token456


POST parameters:
* To: 503-111-2222
* From: 503-222-3333
* Body: Hello World

VERSUS

B) Making a phone call, then sending an SMS message using an instance of the TwilioClient class (from Sean Sullivan's Java wrapper library):

/*import twilio.client.*;
TwilioClient c = new TwilioClient("account123", "token456");

c.call("503-111-2222", "503-222-3333");
c.sendSMSMessage("503-111-2222", "503-222-3333", "Hello world");

See, isn't that better? The advantage is don't have to put together the URL every time or worry about validating each request. You just create your object, which has the account SID and auth token baked in and can recognize the methods "call" and "sendSMSMessage." Instead of needing to be familiar with the Twilio API (which is actually very simple), you can just be familiar with methods in your language of choice that let you make calls or send SMS.

10 comments:

  1. That is very fascinating, You are a very professional blogger.
    I've joined your rss feed and look forward to seeking extra of your magnificent post.
    Additionally, I've shared your web site in my social networks

    Here is my web page - digital camera advice [www.digitalcameratips.co.uk]

    ReplyDelete
  2. Hi, I do believe this is a great web site. I stumbledupon it
    ;) I'm going to revisit once again since i have saved as a favorite it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide others.


    My website: home based business

    ReplyDelete
  3. Offshore Drupal developers prefer these technology as
    it offers many rich features and benefits for various
    web developments. Drupal supports all major operating systems like UNIX,
    Linux, BSD, Solaris, Windows and Mac OS X. Installing the Drupal open source content management system locally can save
    you a lot of bother in the design and development stages of a new website based on this
    web application.

    my web page: Drupal web hosting

    ReplyDelete
  4. We're a group of volunteers and opening a new scheme in our community.
    Your website provided us with valuable info to work on. You've done an impressive job and our whole community will be grateful to you.



    Also visit my site psn cards generator

    ReplyDelete
  5. My programmer is trying to persuade me to move to .net from
    PHP. I have always disliked the idea because of the costs.
    But he's tryiong none the less. I've been using WordPress on several websites for about
    a year and am nervous about switching to another
    platform. I have heard good things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any kind of help would be greatly appreciated!

    Also visit my web site: cartier love bracelet price 2014

    ReplyDelete
  6. Remember not to put on bling dazzling shoes for the daytime formal wedding.
    Take a look at the fashion spreads found in bridal magazines.
    Even the simplest cocktail dresses can grab attention if they are accompanied by the appropriate
    accessories. Accessories are very good for distracting attention away from flaws.


    My web-site - dress

    ReplyDelete