Tuesday 6 March 2012

Fibonacci series

class Fibonacci
{
public static void main(String args[])
{
int prev, next, sum, n;
prev=next=1
for(n=1;n<=10;n++)
{
System.out.println(prev);
sum=prev+next;
prev=next;
next=sum;
}
}
}

Prime Number or Not

import java.util.*;
class Prime
{
public static void main(String args[])
{
int n, i, res;
boolean flag=true;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter a No.");
n=scan.nextInt();
for(i=2;i<=n/2;i++)
{
res=n%i;
if(res==0)
{
flag=false;
break;
}
}
if(flag)
System.out.println(n + " is Prime Number");
else
System.out.println(n + " is not Prime Number");
}
}

Install Java On Linux

Steps

Manual Method

This is the 'generic' variant that also works with GNU/Linux clones that do not support RPM. It does not require administrator rights and allows to install multiple java versions on the same computer.

1
Download the JDK from Sun[1].
2
Click on the "Download" link in the JDK 6 section.
3
Accept the license and continue.
4
Under the "Linux Platform", select "self-extracting file".
5
Download this .bin file and save it to your GNU/Linux machine.
6
Once it has been downloaded, switch to the directory where you saved the file. You do not need to be a root and only must have the write access to the folder where you wish to install java. If your administrator is not supportive, you may need to place java into your home folder or even better on some shared network location.
7
Type sh name_of_the_downloaded_file, for instance sh jdk-6u2-linux-i586.bin. There is no need to make this file executable.
8
The license agreement should start appear on the screen. Scroll to the end of it with 'Enter' and type yes.
9
This installer will create its installation in the same folder, where the downloaded file was placed and from where you have started the installation script. But the installed java jre is rather independent and can be easily moved into another place just by copying all its files.
10
You can install multiple different jre's this way: they coexist together and can be used if some software requires the older version to run.
11
The java executable you need to launch is located in a subfolder, called 'bin'. This way of installation will not configure a default 'java' command for you: you must do this manually or always include the full path in your startup script.

Manual RPM Method

This seems a 'more civilized' way to install java: it allows the installer to check the dependencies on some system libraries that may be missing. However it does not support versioning easily and may fail even in some systems that do support RPMs. The current java installations are rather self-dependent and the required minimal requirements are usually satisfied anyway.

1
Download the JDK from Sun[2].
2
Click on the "Download" link in the JDK 6 section.
3
Accept the license and continue.
4
Under the "Linux Platform", select "RPM in self-extracting file".
5
Download this .bin file and save it to your GNU/Linux machine.
6
Once it has been downloaded, login as root and switch to the directory where you saved the file.
7
Execute './filename', where filename is the name of the file that you downloaded. The filename might be very similar to jdk-6-linux-i586-rpm.bin depending on what the latest version is. You may have to make the file executable by executing the 'chmod +x filename.bin' command.
8
You will get a license, press space bar a bunch of times until you are prompted to enter yes or no. Type in yes and hit enter.
9
This will place an .rpm file in the same directory as your .bin file with the same name (minus the .bin part).
10
Install the rpm file by executing 'rpm -i filename.rpm', where filename is the name of your .rpm file. (Such as jdk-6-linux-i586.rpm).
11
Now, if you want to be able to execute this version of Java interpretor or compiler from any directory on your GNU/Linux system you will have to create a few symbolic links:
ln -s /usr/java/jdk1.6.0/bin/java /usr/bin/java
ln -s /usr/java/jdk1.6.0/bin/javac /usr/bin/javac
12
You are done!

Monday 5 March 2012

public class StaticMethodExample {

public static void main(String[] args) {

int result = MathUtility.add(1, 2);
System.out.println("(1+2) is : " + result);
}

}

class MathUtility{
public static int add(int first, int second)
{
return first + second;
}

}