Thursday, November 23, 2017

Sieve of Eratosthenes In Java

The sieve of Eratosthenes is a famous ancient algorithm to find all prime numbers up to a given limit. We are going to implement this algorithm in Java.

Steps we will follow:

a) Lets we have an array of size 25.
b) First start with 2, remove all numbers which are divisible by 2.
c) Find the next number, which is 3. Remove all numbers which are divisible by 3.
d) Next survivor is 5 and we repeat the same procedure.
e) For the given limit 25, we don't need to find anymore survivor after 5, as 5 X 5 = 25. In other words, we only process up to the square root of the limit.
 f) All survivor numbers in the array are Prime. We save those numbers in a separate array.

Generate all possible permutations in Java using recursion.

In the previous example, we drew a pyramid in java without any loop. We used recursion to achieve our goal and hopefully, you got a very clear idea how recursion works. In the following example, I will show you how to print all permutations of a given string. If the given string is "1234", then all possible permutations are:


 A total number of permutations are 24. ( 4! = 24 )

Wednesday, November 22, 2017

Eclipse Paho Java Client (MQTT client) integration with Spring Boot REST API

MQTT is an open, lightweight, simple client-server (publish / subscriber) messaging transport protocol. It is designed with a minimal protocol overhead. On the other hand Eclipse Paho is one of the most popular client library implementations. In the following example, I will show how to integrate Spring Boot with Eclipse Paho.

Tested with the following environment and technologies:

a) JDK 1.8
b) Eclipse
c) Maven
d) Spring Boot
e) Eclipse Paho as MQTT client library.
f) Postman as REST API client (Google Chrome extension)
g) MQTTLens as client tool for MQTT (Google Chrome extension)

Monday, November 20, 2017

Printing Pyramid Without Any Loop In Java

If you are asked to write a java program to print out the following pyramid, I am pretty sure that you will accept the challenge without any doubt.  Wait! Life is not as easy as you think. If  you are given the following restrictions, are you able to solve the problem?:

1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2

Tuesday, November 14, 2017

Pascal Triangle In Java

Pascal Triangle is one of the most interesting number pattern which is a triangular array of the binomial coefficients. The following example shows how to generate this triangle in Java.

Sunday, November 12, 2017

Apache Maven installation in Window 7

Apache maven is a package manager and building tools for Java. In this tutorial, we are going to install maven in window 7. 

Tested With:
Apache maven 3.5.2
JDK 1.7
Windows 7

Note: You need to have JAVA_HOME environment setting to run maven.

If you have windows 10 and JDK 1.8,  you don’t need to anything extra, just follow the steps of configurations below as we are going to do with Windows 7 and JDK 1.7

How to Setup Java Environment in Windows 10

If you already have downloaded Java Development Kit (JDK), then you are very close to execute and run your java program. Your next step to setup java environment variable and in this tutorial we are going to configure this on Windows 10.

We tested this configure using:

a) Window 10 and Window 7
b) JDK 1.8