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:
 * 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.


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

Auth Headers: username account123, password token456

POST parameters:
* To: 503-111-2222
* From: 503-222-3333
* Url:

A2) Sending an SMS:

Auth Headers: username account123, password token456

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


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");"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.


  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 []

  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

  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

  4. (one) Mike should slow down, both his ideas and his body.
    Type "" and touch on"Add Supply" to include this repo if you haven't currently. Back up your gadget to your iTunes library, and sync it.

    Also visit my weblog Cydia ()

  5. 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

  6. 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
    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

  7. 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

  8. Greetings! Very useful advice in this particular article!
    It's the little changes that produce the most significant
    changes. Thanks for sharing!

    Feel free to visit my web site buy viagra

  9. Melanjutkan tradisi demonstrasi berlayar tahunan dalam melintasi lautan Indonesia yang luas, Sail Indonesia 2017 Rally akan kembali berlangsung mulai tahun ini mulai bulan Agustus sampai Desember. Tahun ini, puncak seri Sail Indonesia 2017 adalah Sail Sabang 2017 yang akan diadakan di ujung barat laut paling utara Sabang di Pulau Weh, di Provinsi Aceh.

    Yuk ramaikan event internasional Sail Sabang 2017, Sabang Sebagai Pelabuhan Hub Wisata Bahari Internasional yang diadakan pada tanggal 28 Nov – 5 Des 2017. Sail Sabang adalah rangkaian dari Sail Indonesia 2017 yang tahun ini diadakan di Sabang.

    Pelaksanaan Sail sabang 2017 di Sabang diwacanakan akan digelar sejumlah atraksi, diantaranya Yacht Rally, Seminar dan Pameran Iptek memperingati 20 tahun Jambore Iptek Sabang, Kapal Pemuda Nusantara, Pekan Kebudayaan Aceh, Parade dan Karnaval Sail Sabang, Work Parachute Championship, International Aerobatic Show, kedatangan Cruice Ship International, dan International Diving Competition.

    Jadi jangan lewatkan ya event internasional Sail Sabang 2017 ini. Nah buruan masukkan Sabang dalam list daftar liburan Anda selanjutnya.

  10. Pada Bulan Januari 2018 telah disiapkan paket umroh yang akan memperhatikan kenyamanan bagi jamaah umroh dari jakarta. Pada paket umroh januari 2018 ini, fasilitas yang dipilih berorientasi kepada kenyamanan jamaah umroh dari jakarta saat melaksanakan ibadah umroh. Baik dari segi pesawat yang digunakan dan hotel yang ditempati di mekkah dan madinah berdasarkan keinginan untuk memberikan kenyamanan bagi jamaah umroh bulan januari 2018 dari jakarta. Selengkapnya dapat dilihat di musti5. Terima kasih.