JAVA code for basic algorithm