Showing posts with label Beginner java. Show all posts
Showing posts with label Beginner java. Show all posts

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:

1234
1243
1324
1342
1324
1342
1423
1432
 .
 .
 .
4321

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

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
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

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