Differences between Arrays and ArrayLists in Java

CIDOC CRM at ResearchSpace (British Museum)

Alternative way to install Apache Jena in Eclipse…with a but!

Being a long-term Semantic Web aficionado, I have used Apache Jena with Eclipse several times for manipulating and parsing RDF models and originally installed it by downloading the Windows-compatible distro from http://jena.apache.org/download/index.cgi and and import the libs into my Eclipse project. I have recently found an Eclipse Library Plugin for Jena 2.0  and I was rather excited to be able to work with Jena  without fetching the latest Jena distro and adding it to your Build Path, but the excitement did not last long….The plugin is unfortunately for Jena 2.0 and it does not seem to be compatible with most recent Eclipse versions (e.g. Mars 4.5). Oh well, back to the known route, then!:)

(Related) Interesting resources:

Using Jena with Eclipse

JenaTools

Video: Comparable Interface

Great Introduction to JUnit

How to add shortcut keys for java code in eclipse – Stack Overflow

Say I type “sout”, the intellisense should expand it to “System.out.println()”. Is there a way to adding such templates?

Source: How to add shortcut keys for java code in eclipse – Stack Overflow

Reverse a String: 4 potential approaches

This is probably one of the most common Java programming interview question and there are at least 4 different approaches that can be taken to tackle it. We will illustrate them by presenting some examples in Java.

1) Use the built-in StringBuffer reverse() method:

public class ReverseStringBuffer
{
    public static void main(String args[])
    {
        String originalString = "MyAwesomeString";
        String reversedString = new StringBuffer(originalString).reverse().toString();
        System.out.println("This is the original String " + originalString +
        "n" + "This is the reversed String " + reversedString);
    }
}

2) Use the built-in StringBuilder reverse() method:

public class ReverseStringBuilder
{
    public static void main(String args[])
    {
        String anotherOriginalString = "AmazingString";
        StringBuilder anotherReversedString = 
            new StringBuilder(anotherOriginalString).reverse();
        System.out.println("This is the original String " + anotherOriginalString + 
            "n" + "This is the reversed String " + anotherReversedString);
    }
}

3) Convert a String into a Char array and loop through each character.

public class ReverseStringCharArray
{
    public static void main(String args[])
    {
        String myString = "ThisIsMyAwesomeString";
        char[] stringToChar = myString.toCharArray();
        System.out.println(stringToChar.length);
        for(int i = stringToChar.length - 1; i >= 0; i--)
        {
	        System.out.print(stringToChar[i]);
        }
A similar implementation can be re-written as an utility:
 
public class ReverseStringCharArray
{
    public static void main(String args[])
    {
        String term = "HappyString";
        //Here we are calling the reverse utility method
        String reversedTerm= reverse(term);
        System.out.println("This is the original String " + term + 
            "n" + "This is the reversed String " + reversedTerm);
    }

    public static String reverse(String sourceTerm)
    {
        if(sourceTerm == null || sourceTerm.isEmpty())
        {
	        return sourceTerm;
        }       
	    String reversedString = "";
	    for(int i = source.length() -1; i>= 0; i--)
        {
	        reversedString = reversedString + source.charAt(i);
	        System.out.println("This prints the reversed string " + 
                reversedString + " n " + 
                " whereas this prints the individual character " + source.charAt(i));
	    }     
	    return reversedString;
    }    
}

4) Use recursion to reverse the entered String. This method returns the reverse of the string passed in by appending the first character (sourceTern.charAt(0)) to the remainder of the String itself (str.substring(1)). If the String is less or equal to 1 character, then the recursion is halted.

public class ReverseStringRecursiveMethod
{
    public static void main(String args[])
    {
        String term = "AnotherString";
        //prints the result of the call to the recursive method
        System.out.println(recursiveMethod(term)); 
    }

    public static String recursiveMethod(String sourceTerm)
    {
        if ( (null == sourceTerm) || (sourceTerm.length() <= 1) )
        {
            return sourceTerm;
        }
        
        return recursiveMethod(sourceTerm.substring(1)) + sourceTern.charAt(0);
    }
}

Keyboard Shortcuts – RStudio Support

Print this and have it in front of your when using RStudio. It is priceless! Source: Keyboard Shortcuts – RStudio Support

Introduction to Predictive Analytics – YouTube

The joy of the Builder Pattern (A Java example)

I was recently introduced to the beauty of the Builder Pattern, a true ‘medicament’ to reduce the number of parameters required for a constructor. The Builder pattern enables the construction of complex objects by splitting the individual steps into separate methods of a Builder hierarchy in which a Director object controls the order of execution.

Here is an example of the Builder Pattern for the object Cat in which the Builder object is a nested class inside the Cat Class. An enhanced version of the builder can take into account the use of Custom Types (for example type Breed), which in turns are employed as parameter objects with their own builders.

public class Cat
{
    private final String breed;
    private final String name;
    private final int age;
    private final double weight;
    private final String furColour;
    private final boolean isFemale;
    private final boolean isMale;
    private final boolean isDeclawed;
    private final boolean isSpayed;
    private final boolean isNeutered;
    private final boolean isStray;
    private final boolean isMicrochipped;
    private Cat(
    final String newBreed,
    final String newName,
    final int newAge,
    final double newWeight,
    final String newFurColour,
    final boolean newIsFemale,
    final boolean newIsMale,
    final boolean newIsDeclawed,
    final boolean newIsSpayed,
    final boolean newIsNeutered,
    final boolean newIsStray,
    final boolean newIsMicrochipped)
    {
        this.breed = newBreed;
        this.name = newName;
        this.age = newAge;
        this.weight = newWeight;
        this.furColour = newFurColour;
        this.isFemale = newIsFemale;
        this.isMale = newIsMale;
        this.isDeclawed = newIsDeclawed;
        this.isSpayed = newIsSpayed;
        this.isNeutered = newIsNeutered;
        this.isStray = newIsStray;
        this.isMicrochipped = newIsMicrochipped;
    }

    public static class CatBuilder
    {
        private String nestedBreed;
        private String nestedName;
        private int nestedAge;
        private double nestedWeight;
        private String nestedFurColour;
        private boolean nestedIsFemale;
        private boolean nestedIsMale;
        private boolean nestedIsDeclawed;
        private boolean nestedIsSpayed;
        private boolean nestedIsNeutered;
        private boolean nestedIsStray;
        private boolean nestedIsMicrochipped;
        public CatBuilder(
        final String newName,
        final String newBreed,
        final int newAge)
        {
            this.nestedName = newName;
            this.nestedBreed = newBreed;
            this.nestedAge = newAge;
        }

        public CatBuilder withBreed(String newBreed)
        {
            this.nestedBreed = newBreed;
            return this;
        }

        public CatBuilder withName(String newName)
        {
            this.nestedName = newName;
            return this;
        }

        public CatBuilder withAge(int newAge)
        {
            this.nestedAge = newAge;
            return this;
        }

        public CatBuilder withWeight(double newWeight)
        {
            this.nestedWeight = newWeight;
            return this;
        }

        public CatBuilder withFurColour(String newFurColour)
        {
            this.nestedFurColour = newFurColour;
            return this;
        }

        public CatBuilder isFemale(boolean newIsFemale)
        {
            this.nestedIsFemale = newIsFemale;
            return this;
        }

        public CatBuilder isMale(boolean newIsMale)
        {
            this.nestedIsMale = newIsMale;
            return this;
        }

        public CatBuilder isDeclawed(boolean newIsDeclawed)
        {
            this.nestedIsDeclawed = newIsDeclawed;
            return this;
        }

        public CatBuilder isSpayed(boolean newIsSpayed)
        {
            this.nestedIsSpayed = newIsSpayed;
            return this;
        }

        public CatBuilder isNeutered(boolean newIsNeutered)
        {
            this.nestedIsNeutered = newIsNeutered;
            return this;
        }

        public CatBuilder isStray(boolean newIsStray)
        {
            this.nestedIsStray = newIsStray;
            return this;
        }

        public CatBuilder isMicrochipped(boolean newIsMicrochipped)
        {
            this.nestedIsMicrochipped = newIsMicrochipped;
            return this;
        }

        public Cat createCat()
        {
            return new Cat(
            nestedName, nestedBreed, nestedAge, nestedWeight, nestedFurColour,
            nestedIsFemale, nestedIsMale, nestedIsDeclawed, nestedIsSpayed,
            nestedIsNeutered, nestedIsStray, nestedIsMicrochipped);
        }
    }
}
%d bloggers like this: