Tuesday, August 30, 2011

FizzBuzz!

FizzBuzz is a simple program that can be used to test one's ability to complete basic programming tasks. To comply with the requirements of the FizzBuzz program, the created program should print out the numbers from 1 to 100 on separate lines (one output per line) with the exception of multiples of 3, for which the program should print "Fizz", and 5 where the number should be replaced by "Buzz". If the number is a multiple of both 3 and 5, the program should print "FizzBuzz". The task here was to implement this program in Java using the Eclipse IDE in an effort to familiarize ourselves with it.



Initial Program
Below is the Java source code that I created to implement the FizzBuzz program. This implementation took me 5 minutes and 9 seconds despite the assistance of an advanced IDE like Eclipse due to an error I made while creating the FizzBuzz.java class file. When I created the class file, I tried to specify that it should be in a non-default package, but I made the mistake of doing this in the top-level project folder instead of the src folder. This caused Eclipse to create several new folders to place the new class file in instead of creating a new package as I had intended. Consequently, I could not get the created file to run until I created a new package in the src folder and moved the FizzBuzz.java file there.

What Eclipse did when I tried to make FizzBuzz.java in a non-default package in the top-level project folder.
What the non-default package should look like.
The source code for the FizzBuzz.java class may be seen below.



A Better FizzBuzz
While this code does work as intended, it is not as elegant as it could be. In this format, it is impossible to use JUnit to test the file as JUnit would not be able to capture the program's output. As a result, we would need to create a function that the JUnit test can call as shown in the modified FizzBuzz program called FizzBuzz2.java.




The JUnit Test
With FizzBuzz2, the JUnit test can call the FizzBuzz2.fizzBuzz(int) function to test the output. The following JUnit test checks the outputs of the two end cases (1 and 100) as well as the outputs when 3, 5, 15, 25, 41, 51, 75, and 94 are passed to the function.


Fortunately, this test runs successfully and helps to assure us that the output of the FizzBuzz program is correct.



Conclusion
Creating the FizzBuzz program was a good exercise in using Eclipse to create a Java project from scratch and brought up an interesting quirk of the system as specifying a non-default package for a file in the main project directory caused strange behavior. It also exposes how software engineering is not a race to get a working product out. Of course, producing a working product out as fast as possible sounds good, but revising that product could lead to a much more elegant design that will save time and effort later on. Finally, the FizzBuzz program highlighted the need to take pay attention or take notes in ICS 314. I am unfamiliar with JUnit so I had no idea how to use until I looked at the notes that I took on the first day of class. Hence, it is important to pay attention to the various course materials (non-webcast lectures included!) since knowing that material (or at least keeping a reference to look up) could really help with the assignments that we will do in the future.

Sunday, August 28, 2011

orDrumbox and the Three Prime Directives of Open Source Software

In our ICS 314 class, Dr. Johnson has established three criteria that open source software should be judged against to determine its quality.  Here we shall examine the quality of the open source Java project called orDrumbox using these three criteria.

Overview
The orDrumbox project aims to create a software drum machine with additional song composition functions.  These functions include the ability to create and modify multiple instrument tracks to create new songs and an automatic fill engine to add various patterns to the songs.  For this test, I have downloaded the setup-ordrumbox-0.9.06-win32.exe installer and the source files from the project's sourceforge.net page.

For more information, please see the project's home website.

Prime Directive #1
The first prime directive is if the system accomplishes a useful task.  orDrumbox appears to accomplish its task rather effectively with an easy-to-use interface that allows users to choose song templates and edit them using the various buttons and clicking on the step sequencer.  It also allows the user to play these songs, but the sound quality seems to be lacking as I heard a lot of static.  While the poor sound quality detracts from the user's experience, it is good enough to give the user a clear picture as to how the song sounds.  All in all, orDrumbox succeeds in fulfilling its task as a drum machine and a song composition tool.



The orDrumbox user interface with a pre-made song loaded.
A modified version of the pre-made template with extra cymbal hits and a new cowbell track.  The changes are in red.

Prime Directive #2
The second prime directive concerns the ease of the software's installation by an external user.  orDrumbox was extremely easy to install thanks to the setup executable provided which makes it so that all the user needs to do is have Java 1.6 installed and follow the installer.  The installer can even create a shortcut on your desktop for the user!  I did have some problems getting the program to launch from the shortcut, but I believe this was due to the way that my system is set up and I could easily launch it from the command line using the java -jar command or executing the Launcher script in the installation directory.  As a result, orDrumbox easily passes the second directive.

The orDrumbox installer makes installation extremely simple for anyone who has installed a Windows program before.
Finished with the installation.  The installer even includes installation progress bars like most other Windows installers!

Prime Directive #3
The third and final directive is fulfilled if it is easy for an external developer to understand and modify/enhance the system.  Unfortunately, this is where orDrumbox falls short.  The package of source files fails to include any developer level documentation for external developers which is definitely not desirable.  In addition, the source code itself suffers from a lack of commenting as seen below.  However, some of the function and variable names make their purposes rather self explanatory which might make understanding the source code a little bit easier.

public Element toXml(Document xmldoc) {
Element scaleElement = xmldoc.createElement("scale");
scaleElement.setAttribute("display_name", getDisplayName());
scaleElement.setAttribute("freq", getFreq() + " ");
scaleElement.setAttribute("rand", getRand() + " ");
scaleElement.setAttribute("order", getOrder() + " ");
scaleElement.setAttribute("lgr_segment", getLgrSegment() + " ");

for (int iNumScalenote = 0; iNumScalenote < scaleNotes.size(); iNumScalenote++) {
Scalenote scalenote = (Scalenote) scaleNotes.get(iNumScalenote);
scaleElement.appendChild(scalenote.toXml(xmldoc));
}

return scaleElement;
}

By looking at the source code, the problems with trying to work on this system becomes apparent.  For example, an external developer would probably guess that the function above is used to export a scale element to a XML file based on its name, but the lack of any commenting makes it difficult to know for certain without going through each line of the function.  Consequently, orDrumbox fails the third directive as there is no documentation and very little commenting which forces any external developers to look at every line of code to understand what the program is actually doing.

Conclusion
Overall, the orDrumbox appears to successfully fulfill two of the three prime directive of open source software.  Not only does it accomplish the tasks that it intended to, but it is also extremely easy to install on a Windows system.  On the other hand, the lack of documentation and commenting would make external development of this system rather difficult and time consuming as the only way to understand the program is to go over every single line of code.  Therefore, the orDrumbox system successfully complies with the first two prime directives, but fails to meet the third prime directive of open source software.