Name Popularity Search Tool Based on the Social Security Administration Data in Java

NameSurfer is a tool to search and graph popularity of names based on data provided by the Social Security Administration in the last century. As you can see in the picture, names can be entered in the names field which are graphed either by enter key or the graph button. Graphs are shown on a scale of a thousand. Zero means the name didn’t show up on the thousand most popular names list. One is the most popular and a thousandth is the least popular in the list.

I have made satisfactory progress in the course CS106a. I have learned about the graphics class GObject, BufferedReader class, RandomGenerator class, and, ACM and Java utilities. Interactors were something new in this section of the course. Although I used similar Canvas method in Yahtzee, I am not so sure about subtle difference between the ConsoleProgram and the Program class. As always following the videos and instructions on the assignment sheet pays well in the course of finishing an assignment. A mix of more doing and less theoretical knowledge definitely assuages the boredom of theoretical knowledge. And in due time theory makes more sense.

With the positive energy after Yahtzee, I started implementing NameSurfer as soon as I finished the last assignment. I tend to batch watch the lectures, then read the assignment and then work on it. I improved up on my workflow and started with reading the assignment. A good number of problems were already making sense which resulted in a clean mental picture and basic implementation of the NameSurfer. Some of the methods like BufferedReader to read the names-text file, store information in a HashMap, and store per graph information in an ArrayList, were pretty clear even before the start of the program. Also lectures were making more sense as I was watching them along with implementing the program. I tend to watch a single lecture per day but this time I was watching two lectures per day. As soon as I got done watching four lectures I sat down and worked crazily on the project. I finished it in record two days. And it took me a night of sleep and a 20 minutes next morning to fix some last moment bugs.

toString() didn’t make much sense when I read it the first time. It was very helpful to implement it to make data human readable in NameSurferEntry class. Similar is true for two Integer class methods that I had forgotten about. Both Integer.parseInt(String) and Integer.toString(int) came handy in converting String to int and int to String, respectively. Life would have been much more problematic if I had to parse integers out of a String manually.

Working with five different classes, NameSurfer, NameSurferEntry, NameSurferDataBase, NameSurferConstants, and, NameSurfergraph was interesting. NameSurfer played the role of assembling all other classes in a comprehensive program. I got to use StringTokenizer class in NameSurferEntry Class. It broke down the string into name and year and stored this information in an Array. I broke down the names file in NameSurferDataBase class. Passes single entry in NameSurferEntry to be stored in the array. Then I created a HashMap with template String and NameSurferEntry. Name as a String was used as a key and NameSurferEntry was stored as value.

NameSurferGraph was the trickiest to implement. It took me a whole day to implement it. The NameSurferGraph was to be responsive. The starter package included the code for the responsiveness. ACM graphics aren’t my favorite part. The whole class was written in a very old graphic toolkit. I tend not to mess too much with it. The grid layout was easy to implement with GLine and GLabel. The trick was aligning the graph lines. It took me several trials to get it right. I had clear idea how I was going to do that but implementation could get messed up. There were two important constants, the array had 12 cells and number of decade value were 11. I had to be very picky about when to let the loop run a less, equal or a more than NDECADES. I also decided to restrict the graph between the top and bottom horizontal line which required me to convert the thousand scale to in-between space of the graph. It is always interesting to see the vast difference generated by using int instead of double or a using long mathematical formula in a line.

Implementing colors to cycle between black, red, blue, and magenta was quite mind bending. I thought about various ways to implement it, for examples, using array, loop, if-for statements, etc. None worked as I had thought. In the end, it was as simple as taking a mod of user’s count in the array list by 4 which the number of colors we want to cycle through. I also made sure that empty strings and same names didn’t end up in the array list.

At the end of the day, it was very hard to get anything done when I had myself exhausted but sleeping on it payed well the next day. A fresh mind is what you need to solve the problems of programming.

As always you can check the code in my Github repo.

Writing Yahtzee Game in Java

Yahtzee in Java.

To steal from Wikipedia, Yahtzee was originally created by Milton Bradley which is now owned by Hasbro. It is a 1 to 4 player dice board game. It is categorized as luck, probability and strategy game. There are 5 dices and 13 categories that you can score in. Evidently, I missed the space and time in which Yahtzee became a household timepass.

Mehran Sahami of Google and Stanford and many other fame is a marvelously smart guy and a great teacher. This is the fifth assignment of Stanford’s CS106a Programming Methodology which I have completed without help of any book. I have watched and rewatched his videos until they started making sense. I did read the first 4 chapters of The Art and Science of Java while I was downloading lecture videos. Believe me it did take two weeks to download 28 videos at 0.256 mbps connection on the-state-owned-internet-provider’s connection. I have taken miniscule or no help in doing the assignments. Why I say miniscule I would discuss later.

I finished the Hangman Assignment in middle of a wedding. But this time I had enough time to properly follow object oriented programming paradigm. The graphical part of the game was already provided in with the starter package. It also included two vital classes that proved to be critical in timely finishing the program. These two classes are YahtzeeDisplay and YahtzeeMagicStub. YahtzeeDisplay did all the graphical heavy lifting work providing methods to show information and interact with mouse click events. YahtzeeMagicStub played the vital role of making sure that dice configuration matches with user asked categories.

I spent a day or two planning the game using pen and paper. I knew I had to explicitly use object oriented programming. I decided anytime run() got clumsy or any set of instructions that would take more than a few lines would be better served writing as a separate method. I think I did a good job.

I understand the basic idea of enumeration but this is the first time I have used enumeration. It didn’t make any sense until I started implementing the program. I still feel I lack intuitive grasp to how-to of enumeration. I built up on a provided enumeration but it would be hard to start from scratch and implement it. Without enumeration it would have been very difficult to refer to 13 scoring and other categories.

I have written a lot of methods but have previously avoid writing type-return kind of methods. It finally clicked. Most of the methods that I wrote in this program are pretty simple and I fed them parameters and returned some. I passed around almost all the variables. I initially implemented a single player game. As a result of this strategy, turning a single player game into a multiplayer game only took 2-3 hours.

Yahtzee was all about arrays. I used both single array and multi-dimensional arrays. What I enjoyed most was cleverly, at least I think so, using a single dice array to use and discard for every single player turn. And storing the overall game score in the multi-dimensional score array. I did have to adjust the game for the fact that user prefer numbering system starting 1 and computer counts from 0. It mostly comprised of subtracting 1 from arrays. It became very tricky as to where change the value while converting game from single player to multiplayer.

In one of the methods, my task was to find the biggest number. My initial gut response was to draw a tricky diagram comparing each member with all others. You could see what would that mean for 4 numbers. I looked for what Java provided for comparing numbers. I couldn’t find anything that came bundled with acm library. One fine gentlemen on Stackoverflow, suggested a simple loop and comparing two numbers, only keeping the largest. I should have come to this conclusion but you could do little when you have seen even a glimpse of possible solution. That was the miniscule help I have taken from the fine folks of internet.

I decided to skip implementing YahtzeeMagicStub on my own. I am sure it is very hard and it would have stretched my logic muscle. But personally I think I can see what kind of programming would solve the magic stub problem and it mostly involves lot of logic gates and loops. I think I can solve it without learning anything new and vital for the moment. I would be better served moving along and learning new stuff.

It has been a nice experience finishing 5 out of 7 assignments. Makes me feel like a programmer.

Checkout the code on Github.

Writing Hangman Game in Java.

Writing Hangman during India’s January chills and of course an Indian wedding has been a hell of an experience. Up until Breakout game, which is assignment #4 of Stanford’s Introduction to Computer Science course, it felt like walking with crutches. A lot of structure and program snippets were provided indirectly by iconic professor Mehran Sahami in his lectures. Not so much in Hangman.

Hangman taught me several aspects of programming. I have had particular troubles with managing the size of window in the acm library backed course. There are two type of Java programs I have encountered. Although I am technically unaware of their subtleties but Application always opened in a 200px square window whereas Applet had parameters to define size of the window. canvas which is an object of class HangmanCanvas() created a dual pane for console and graphical aspect of the program.

Due to weariness of the great Indian wedding I didn’t decompose my implementation from beginning. It was a mistake. After certain point of time it became impossibly hard for me. I understand how primitive types are passed by value and objects are passed by reference but my dash object kept behaving like a primitive type which prevented me from applying more objected oriented programming to my program.

Intuitively, I think it would be much more simpler to ask for input at one single point. It is always tricky to trigger something without asking any input before hand. Consequently, I had to ask for user-guessed-letter at several instances checking for illegal guesses. Although it requires more reading of other people’s code to learn how things are done in wild for both what is simpler and appropriate object-orientedness of a program.

Most of the run method and rest of the program was pretty straight forward. It comprised of following all the instructions of the Hangman Game.

I could also have better implemented HangmanCanvas class but I opted for mere copy pasting coordinating the lines to make it run perfectly well. I could have added a little more invars to make my program more readable. Oh! well. I had to finish the program as soon as possible so that I can get to Android programming which is my primary goal.

Overall being able to solve problems is a very self-affirming experience. I particularly enjoyed solving how to make letter’s show in right order and using null pointer.

In the end, I have a strong urge to say what Woody Allen said in Annie Hall,”..there goes another novel..”

Code can be found in my Github repo.

The Best Way to Download Coursera Study Material on Fedora 20

MOOCs have completely changed how education works in modern society. The Coursera is my favorite MOOC platform. They have an excellent set of courses designed for beginners and experts alike.

In this tutorial I am going to discuss the best and the easiest way to download videos and other material from Coursera with help of a simple setup and an alias for quick download.

I am using Fedora 20 for following tutorial. I personally don’t like to use sudo command unless not needed and neither do I like to use alternate package installer other than supplied by the distribution. Hence, I am going to create a Python virtual environment and install the Coursera script in it.

Install package required for creating python virtual environment.

yum install python-virtualenv.noarch

Create a directory and virtual environment.

We will create Coursera folder which will contain both all your courses and virtual environment.

mkdir -p Coursera/coursera-env
virtualenv Coursera/coursera-env

Activate and install the python script using pip.

We will be using a python script coursera-dl by Dirk Gorissen.

source Coursera/coursera-env/bin/activate
pip install coursera-dl

For ease of use, create a ~/.netrc and save your username and password in following format.

machine coursera-dl

Download and deactivate when done.

Now we are ready to download our favorite Coursera course. Make sure you log-in in your Coursera course and accept the honor code for first time before using the script.

coursera-dl -d Coursera androidapps101-001

-d download to folder
Course version can be found in the link androidapps101-001.

Setting up .bashrc alias.

nano ~/.bashrc
alias apps101='source oursera/coursera-env/bin/activate && coursera-dl -d Coursera androidapps101-001 && deactivate'

Next time you want to update your Android course all you have to do is run apps101 in your favorite terminal.

If you face any problems do make sure to open a bug report at Github Coursera-dl page.

Special thanks to Dirk Gorissen for working on Coursera-dl.