Sunday, 24 January 2010

Ubuntu Linux: Install Sun's Java JDK

If you're using Debian (this works with Ubuntu as well, and may work with other Debian derivatives) and want to install Sun's Java Development Kit, and ensure it is set properly.
First, let's start by opening a terminal. Remember to use su or sudo for admin tasks.
sudo apt-get install sun-java6-jdk -y
You'll be asked to accept the accept the license terms before installing the JDK.
After all is done, type the following to test your JDK:
java -version
javac -version
Many Linux distributions come with other versions of the JRE (runtime environment). If this is the case, use the following terminal commands to choose which want to use as default:
update-alternatives --config java
update-alternatives --config javac
The first line lets you choose a JRE and the second, a compiler (provided more than one alternative is installed).

Notes: You need the nonfree/multiverse repository(-ies) enabled. OR, if you're using Ubuntu 10.04 onwards, the "partner" repository (Sun's/Oracle's Java packages have been moved to partner). Via command line, you may easily do this by pasting the following line in your terminal:

sudo add-apt-repository “deb maverick partner”
sudo apt-get update
 If you're using Ubuntu 10.10, the word in bold should be maverick; for 10.4, use lucid.

Wednesday, 6 January 2010

Java programming fun

This is an interesting problem solved using Java, but without using any fancy extra methods.

public class TestIsSubString {

* Write a isSubString method that searches for a specific string within
* another string; the method must return true if the former exists in the
* latter string. Otherwise, the method return false.

public static boolean isSubString(String part, String whole) {
// The hard way, with nested loops instead of whole.indexOf(part)!= -1
// Note to self: local variables MUST be init'd
int hits = 0; // flag type of variable
boolean isFound = false;
for (int i = 0; i <= whole.length()-part.length(); i++) {
hits = 0;
//isFound = false;
for (int j = 0; j < part.length(); j++) {
if( part.charAt(j) == whole.charAt(i + j) ) {
hits ++;
System.out.println("Hits = " + hits + " | len = " + part.length());
if( hits == part.length() ) {
isFound = true;
return isFound;

public static void main(String[] args) {
String text = "The cat in the hat.";

System.out.println("isSubString(\"cat\", \"The cat in the hat.\") "
+ isSubString("cat", text));

System.out.println("isSubString(\"bat\", \"The cat in the hat.\") "
+ isSubString("bat", text));

System.out.println("isSubString(\"The\", \"The cat in the hat.\") "
+ isSubString("The", text));

System.out.println("isSubString(\"hat.\", \"The cat in the hat.\") "
+ isSubString("hat.", text));


OOP Principles and UML

These are my favourite explanations (so far) regarding the principles of OOP: Abstraction, Encapsulation, Inheritance and Polymorphism. They...