Java: Rediscovering the basics

It’s been a while since I did some Java but now it seems to be the right tool for the job at hand so I am rediscovering Java with a masochistic twist, ‘look boss no IDEs’.

I am experimenting with the SSJ Java library for Stochastic Simulation as I mentioned in an earlier post and here is some sample source code:

import umontreal.iro.lecuyer.randvar.*;
import umontreal.iro.lecuyer.probdist.*;
import umontreal.iro.lecuyer.rng.*; 

public class GenerateDemandArrivals {
  private RandomStream randomStream;
  private RandomVariateGen expArrival;
  private RandomVariateGen poissonArrival;
  private double lambda;
  private int times;

  public GenerateDemandArrivals() {
    randomStream = new MRG32k3a();
    lambda = 10.0;
    times = 5;
    expArrival = new RandomVariateGen(randomStream, new ExponentialDist(lambda));
    poissonArrival = new PoissonGen(randomStream, new PoissonDist(lambda));

  }

  public void demandArrival() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next random int : " + randomStream.nextInt(0, 10));
    }
  }

  public void demandArrivalExpDist() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next exponential dist double : " + expArrival.nextDouble());
    }
  }

  public void demandArrivalPoissonDist() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next poisson dist double : " + poissonArrival.nextDouble());
    }
  }

  public static void main(String&#91;&#93; arg) {
    new GenerateDemandArrivals().demandArrival();
    new GenerateDemandArrivals().demandArrivalExpDist();
    new GenerateDemandArrivals().demandArrivalPoissonDist();
    
  }
}
&#91;/sourcecode&#93;

I was tried of doing the following everytime I changed the source code in my favorite editor Vim:

<pre>
javac -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar;." GenerateDemandArrivals.java
java -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar;." GenerateDemandArrivals
</pre>


I already had ant installed on my Windows machine but so I said why not take advantage of it, I looked for <a href="http://ant.apache.org/manual/using.html">simple ant file</a> and found something quite close in apache ant manual.

After little bit more search and refinement here is what I came up with:


<project name="Sim" default="run" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="."/>
  <property name="build" location="."/>
  <path id="project.class.path">
    <pathelement path="C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar"/>
    <pathelement path="."/>
  </path>

  <target name="run"
    description="run the program" 
    depends="compile">
    <java classname="GenerateDemandArrivals">
      <classpath refid="project.class.path"/>
    </java>
  </target>

  <target name="compile"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}">
    <classpath refid="project.class.path"/>
    </javac>
  </target>

</project>

Now I can simple type ant (while I am in the directory containing the build.xml file above) after any changes to the file and it compiles and run my code.

I feeling ashamed of feeling good and writing about such basic stuff but I am thinking there would be another person like me (could be myself at some future point where I have to leave my lovely Ruby world and come back to Java out of necessity).