Saturday, August 27, 2011

Probability by Counting

Related Articles

Poker Probability

Suppose you draw five cards from a shuffled deck, what is the probability of getting a pair ?

Card Representation

Let's represent the five drawn cards in an array of String. For example :
String[] cards = {"S5,"SA","D6","H7","CT"};
Which means the five of Spade, the ace of Spade, the six of Diamond, the seven of Heart and the ten of Club. In the related article section, you may see some practical use of this representation in shuffling and displaying the cards.

Sorting the Cards

if we sort the card array, then it is easy to determined whether it contains a pair. All we have to do is just to compare consecutive cards.
boolean pair=false;
if (samePoint(cards[0],cards[1])) pair=true;
if (samePoint(cards[1],cards[2])) pair=true;
if (samePoint(cards[2],cards[3])) pair=true;
if (samePoint(cards[3],cards[4])) pair=true;

Array sorting is easy and you may see the series of "Sorting in Java" in the related articles section.

You may write four compare statements as above, or you may write a for loop as follows :

boolean pair=false;
for (int i=0;i<cards.length-1;i++)
  if (samePoint(cards[i],cards[i+1])) pair=true;

Strict Definition of a "Pair Pattern"

In the game of poker, typically we don't consider a triple as a pair. We don't consider full house as a pair. We don't consider "two pairs" as a pair. Therefore we must refine the above implementation to handle the following patterns :
AAABCThis is known as a triple and is not considered as a pair
AAABBThis is known as a "full house" and is not considered as a pair
AAAABThis is known as "four of a kind" and is not considered as a pair
AABBCThis is known as "two pairs" and is not considered as a pair
AABCDThis is known as "a pair" and is what we want

Fortunately, it is easier than we think. All we have to do is to ensure there is one and only one pair.

Counting the probability

Just generate all five card combinations out of a deck of 52 cards. If it is pair, increase the counter by one. Then the probability is calculated by :
pair_count / total_combination

Combination generation can be found in the related article section. You should read that article first. A modified version that handles String array is provided as follows :

Testing the combination program

java Combination


S2 S3 S4
S2 S3 S5
S2 S3 S6
S2 S4 S5
S2 S4 S6
S2 S5 S6
S3 S4 S5
S3 S4 S6
S3 S5 S6
S4 S5 S6

The Probability Counting Program

We are now going to write a probability counting program with the following output :
javac *.java
java ProbabilityCount

Probability of getting a pair = 1098240/2598960 = 0.4225690276
Probability of getting two pairs = 123552/2598960 = 0.0475390156
Probability of getting three of a kind = 54912/2598960 = 0.0211284514
Probability of getting four of a kind = 624/2598960 = 0.0002400960
Probability of getting full house = 3744/2598960 = 0.0014405762

Source Code

Saturday, August 13, 2011

Screen Capture

Related Articles


In this article, you will see a simple program that captures the screen and saves the image to a jpeg file. It consists of only a few lines of Java code and yet it is doing something meaningful.

Getting the Screen Dimension

Save it as and run it, a sample run is shown below :
java A
screen dimension = 1280x800

Screen Capture

Once we get the screen dimension and save it in a Rectangle structure, we can define a capture() method that takes the rectangle as a parameter. Compile and run the following program, a jpeg file screen.jpg would be created.

Monday, August 8, 2011

Popup menu in Java

Popup menu

It is also known as context menu. It is typically triggered by the right mouse button. For system with only one mouse button, it is triggered by pressing and holding the primary mouse button.

Mouse Event

In Java, the MouseListener interface is used for capturing and handling the mouse event. The mousePressed() and the mouseReleased() methods must be implemented in order to trigger a popup menu.

To implement a full MouseListener, we must also implements the following method :
  public void mouseEntered(java.awt.event.MouseEvent e)
  public void mouseExited(java.awt.event.MouseEvent e)
  public void mouseClicked(java.awt.event.MouseEvent e)

Since we are not interested in the above events, we will implement the above with empty methods.

Adding popup menu

We will need to use these two classes.
A single popup menu may contain multpile menu items, the following method create a simple popup menu with 3 menu items.


Whenever a menu item is clicked, an action event is triggered. We need to implement an ActionListener in order to capture the action event. For the ActionListener interface, the only method that required to be implemented is actionPerformed.


The following program will create a panel with a text field inside it. Type anything into it.
Right click the text field, click "select all", click "copy", then click "paste" several times.

Friday, August 5, 2011

Text Processing - html to text

Two days ago, I was asked to write a method htmlToText(). The method would take an html filename as parameter. It would simply copy the content of the html file to a text file, with all the html tags removed.

For example, consider the following html file :

With all the tags removed, the output would be :
Hello, there

I wrote this method with pen and paper. Yes, I was actually "WRITING" program with pen, not "TYPING" program in front of a computer. Here is the version I wrote :


The above program read the input file line by line, remove the tags for each line, and then append the line to the output file.

There is a major drawback of the above implementation. Since the removeTag() method is applied on a line by line basis, it doesn't work if the open tag and the close tag are on separated lines, such as :

Alternative : Reading the whole file into a content buffer

When I finally arrived home and got access to a computer, I rewrote the method.

This method consumes more memory. However, it is a completely feasible method. Most html files are not very big. You won't find a html with a file size of 100 megabytes.

Regular Expression

Finally, I simplified the method further with the help of regular expression.

Lesson Learned

Nowadays, good programmers are not necessary good at "writing" program, the are good at "typing" program in front of a computer, with google as their friends.

Thursday, August 4, 2011

Set Operations in Java

What is a Set

Mathematically, a set is just a collection of objects, for example
let set=["apple","orange","banana"]

For a simple set, the order of the element is not important.

You can add element to a set.
set.add("strawberry") is ["apple","orange","banana". "strawberry"]

You can remove element from a set
set.remove("orange") is ["apple","banana","strawberry"]

You can ask whether a set contains a certain element.
set.contains("banana") is true

The following is a simple example.

Compile and run :
java SetDemo

[banana, orange, apple]
[banana, orange, apple, strawberry]
[banana, apple, strawberry]
apple in the set=true
orange in the set=false

Set Operations : union, intersection, difference, subset

  • Union: set1.addAll(set2);
  • Intersection: set1.retainAll(set2);
  • Difference: set1.removeAll(set2);
  • subset test: set1.containsAll(set2);

Sample :
Suppose a person has three friends :
friends = ["Helen","Mary","John"];

Suppose he has three classmates :
classmates = ["John","Peter","Mary"];

"Peter" belongs to classmates but not friends, because he treats "Peter" as enemy.

intersection = set of people who is a friend as well as a classmate
             = ["John","Mary"];

union = set of people who is either a friend or a classmate
      = ["John", "Mary", "Peter", "Helen"];

subset test :
  friends.containAll(classmates) would return false, 
  because not all classmates are friends.
  That is, classmates is not a subset of friends

The following is the program that demonstrated the above operations.


java SetOperation

friends=[Mary, John, Helen]
classmates=[Mary, Peter, John]
intersection=[Mary, John]
union=[Mary, Peter, John, Helen]
'classmates' is a subset of 'friends' : false