Monthly Archives: October 2015

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.

[code language=”java”]
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);
}
}
}
[/code]

Exploratory search: New name for an old hat? | ACM SIGMOD Blog

Source: Exploratory search: New name for an old hat? | ACM SIGMOD Blog

Writing an Insertion Sorting Program in Java

This morning I have just woken up after dreaming of a job interview where I was asked to code an insertion sorting program in Java. I know this might probably sounds a bit crazy, but sometimes your dreams do resemble real-life situations, in particularly work-related ones. Anyway, this made me think of the difference between the Bubble and the Insertion sorting algorithm. While the Bubble sorting algorithm (also called sinking sort) repetitively compares each pair of adjacent elements and swaps them if in the wrong order, the Insertion sorting inserts element into the correct position one at the time by making incremental adjusting to the previously list.

Here is a basic Java implementation of the insertion sort algorithm:

[code language=”java”]

import java.util.Arrays;

public class InsertionSort
{

public static void main (String[] args)
{
//initialise an Array of 10 int elements
int[] myIntArray = new int[10];;
populateArray(myIntArray); // it populates the Array, although it is not sorted yet
insertionSort(myIntArray); // Here is where the insertion sort algorithm is applied
printArray(myIntArray); // It prints the sorted Array
}

private static void insertionSort(int[] arrayElements)
{
int i; //outer loop starting from [1], that is the second element,
//to the end of the list. We assume that all elements to the left of the first
//element of the list [0] are smaller than element [0]
int j; //our inner loop
int key; //our current element to compare
int temp; // temp value for swapping elements in the list

for (i = 1; i < arrayElements.length; i++) //It starts at [1] because element [0] is already sorted
{
key = arrayElements[i]; // the current element starts with [1]
j = i – 1; // element of [i] left for comparison with the current element key
while (j >= 0 && key < arrayElements[j]) //it evaluates if j is less or equal to zero and if there are elements left to be compare with the key
{
temp = arrayElements[j];
arrayElements[j] = arrayElements[j + 1];
arrayElements[j + 1] = temp;
j–;
}
}
}

public static void populateArray(int[] populatedArray)
{
for (int i = 0; i < populatedArray.length; i++)
{
populatedArray[i] = (int) (Math.random() * 100);
}
}

public static void printArray(int[] sortedArray)
{
for(int i = 0; i < sortedArray.length; i ++ );
{
System.out.println(Arrays.toString(sortedArray));
}
}
}
[/code]