Regular expressions!

I’m taking Theory of Computation (text by Sudkamp) this semester and one of the immediate benefits is being able to understand & interpret regular expressions. java.util.regex is a package that contains “classes (Pattern and Matcher) for matching character sequences against patterns specified by regular expressions.”

From the API specifications, here’s how you use the Pattern class:

Pattern p = Pattern.compile(“a*b”); //Matches just 1 “b” as well as *any* number of “a”s before that one b. Creating a Pattern like this allows you to reuse this object.

Matcher m = p.matcher(“aaaaab”); //Matches what’s in the parentheses against the Pattern object referenced by p.

boolean b = m.matches();  //Returns true if the user’s input matched the pattern.

If you only want to match just once or twice, you can use an expression like this (which evaluates to True or False).

Pattern.matches(“a*b”,”aaaaab”) –> In this case, “aaaaab” fits the pattern so it will return true.

There are several websites you can test out any regex expression you have in mind e.g., RegExr, etc.

This has actually come in handy for my first project of the semester in my Java class. Our task is to create a Hanging With Friends/Hangman cheater of sorts. A sample run of the program should look like this:

jane@Jumanji:~$ java Hangman "??arm" "c"Here are all of the possible words:inarmswarmtharmunarmh: 1i: 1n: 2s: 1t: 1u: 1w: 1Your best guess is: 'n.'
jane@Jumanji:~$ java Hangman "??art" "MEHY"Here are all of the possible words:boartquartscartswartb: 1c: 1o: 1q: 1s: 2u: 1w: 1Your best guess is: 's.'

The syntax is: java Hangman param1 param2.

Parameter 1 – is a string that should only contain question marks and/or letters. This is a mandatory argument that must be present before the program can proceed. Word matching will be done based on Parameter 1. However, Parameter 2 is an optional argument which allows the user to specify characters that are definitely NOT in this list of matched words. The plot thickens. 🙂

In any case, I have a version 1 of this program running with the necessary checks & balances. I made extensive use of helper methods and it’s not very efficient but that’s why I tried to have it running so I could step back and ‘refine’ as best as I can.

Minor edit: updated my code to remove a bug where characters already guessed were showing up. Cheers!

Buggy output jane@Jumanji:~$ java Hangman ??art SHapart  --> should not be there because A was already guessed.boartlyartpeartquarta: 1b: 1e: 1l: 1o: 1p: 2q: 1u: 1y: 1Your best guess is: 'p.'

Die Rich Craps game in Java

For one of my programming projects last year, our task was to create a version of the Die Rich Craps game. I finally decided to put my code for that project (and others) on Github.

My submitted project looks, in retrospect, a little unreadable but I did try to comment the code appropriately. My Die Rich craps implementation is contains:

  1. 2 while loops
  2. 1 for loop
  3. 4 if/else statements
  4. 2 if statements
  5. 4 if-else-if laddered statements.

So, I am in the process of rewriting it to be a little shorter and slightly more testable. I have a rough & incomplete sketch which goes:

1) Create a Players class which will contain key variables that are unique to each instance of the Players class and a final static class variable which applies to all members of the Players class.

2) Create the DieRich class file (which includes the main method)

That’s as far as I’ve gotten. So far, it’s a good experiment to think about breaking the program/project into tasks and writing code accordingly.

I visited a software product company recently and I learned that tests (regression and unit tests) are exceedingly important & take up just as much time as coding does! The developer I ‘shadowed’ walked me through the process of unit testing which I will try to implement in my projects this semester.