Tuesday, August 30, 2011

Saying Hello in Java: "FizzBuzz"

My first programming assignment, after 5 years away from Java, is the FizzBuzz program.  The program is suppose to print out 1 to 100, and replace the numbers that are divisible by 3, 5, and 15 with the strings "Fizz", "Buzz", and "FizzBuzz" respectively.  This task took me about 8 minutes and 30 seconds to accomplish:

package edu.hawaii.ics613;

public class FizzBuzz {
  public static String generateOutput(int i) {
    if(i%15 == 0)
      return "FizzBuzz";
    else if(i%3 == 0)
      return "Fizz";
    else if(i%5 == 0)
      return "Buzz";
    else
      return String.valueOf(i);
  }
  public static void main(String[] args) {
    for(int i=1; i<=100; i++) {

      System.out.println(generateOutput(i));
    }
  }
}

Something new which I didn't learn about 5 years back is the JUnit test facility.  Here is a simple test case for the FizzBuzz program above:

package edu.hawaii.ics613;

import static org.junit.Assert.*;
import org.junit.Test;
public class FizzBuzzTest {
  @Test

  public void testGenerateOutput() {
    assertEquals("Testing 1", "1", FizzBuzz.generateOutput(1));
    assertEquals("Testing 3", "Fizz", FizzBuzz.generateOutput(3));
    assertEquals("Testing 5", "Buzz", FizzBuzz.generateOutput(5));
    assertEquals("Testing 15", "FizzBuzz", FizzBuzz.generateOutput(15));
  }
}




Conclusion: I am still very rusty... need more Java polishing!

Monday, August 29, 2011

Proteus Cross Compiler and the LLVM Compiler Infrastructure

There is a new (6 days old) project on SourceForge that really piqued my interest: the Proteus Cross Compiler.  The project boasts the ability to generate Java code from GCC compatible languages such as C, C++, and Fortran.  Having had experience compiling C to MIPS assembly by hand, I began poring through the project home page and documentation.

At first, I was doubtful.  Many would-be language converters that I have looked at in the past failed at the insurmountable task.  What could make this project an exception?  I was pleasantly surprised to find that the task was reduced in complexity by taking advantage of another interesting, well-established project: the Low Level Virtual Machine (LLVM) compiler infrastructure.  The LLVM project takes care of the various GCC language front-ends, and spits out an optimized intermediate form which is then used by Proteus to generate Java code.  Thus, in a way, Proteus is just an LLVM to Java converter.

After being convinced of the reduced complexity of the task, I was quick to start setting up my environment to test out the system!  Looking through the worked-out examples, I figured it was best to try it on my Ubuntu virtual machine instead of directly on my OS X Lion, which would require me to manually compile many dependent packages.  On Ubuntu, installing the supporting LLVM and GCC front-end was really easy:

sudo apt-get install llvm-2.7 llvm-gcc-4.5

Finally, it is time to play!  I clicked on the "files" section to download the project, and the only thing there is a "readme.txt" file.  The 6-day old project does not have the files uploaded yet...  I guess we will just have to check it out next time!

One closing trivia: is anyone aware that Apple is using LLVM?  Check it out: http://developer.apple.com/technologies/tools/