Tuesday, August 30, 2011

Serialization and Deserialization in java


//serializableClass.java

import java.io.Serializable;

public class serializableClass implements Serializable
{
/**
* Default serial version UID
*/
private static final long serialVersionUID = 1L;
String s;
int age;

public serializableClass(String s,int age)
{
this.s = s;
this.age = age;
}
public String toString()
{
return s+" is "+age+" year old.";
}
}



//serializationExample.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class serializationExample
{
public static void main(String args[])
{
//Serialization
try
{
serializableClass object1 = new serializableClass("Arun",26);
System.out.println("Object 1 :"+object1);

FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
//Deserialization
try
{
serializableClass object2;

FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (serializableClass)ois.readObject();

System.out.println("Object 2 :"+object2);
ois.close();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
}
}


Output =>
Object 1 :Arun is 26 year old.
Object 2 :Arun is 26 year old.

Monday, August 29, 2011

Exception Handling in java


//ExceptionHandelingExample.java

public class ExceptionHandelingExample
{
public static void main(String args[])
{
int a=10,b=0;
try
{
int c = a/b;
System.out.println(c);
}
catch (NumberFormatException ne) {
System.out.println("I am in NumberFormatException catch block and the error is "+ne.getMessage());
}
catch (ArrayIndexOutOfBoundsException are) {
System.out.println("I am in ArrayIndexOutOfBoundsException catch block and the error is "+are.getMessage());
}
catch (ArithmeticException ae) {
System.out.println("I am in ArithmeticException catch block and the error is "+ae.getMessage());
}
catch (Exception e) {
System.out.println("I am in Exception catch block and the error is "+e.getMessage());
}

finally
{
System.out.println("I am in finally block");
}
}
}

Output =>
I am in ArithmeticException catch block and the error is / by zero
I am in finally block

Interface example in java


Java don't support "multiple inheritance" i.e. in java it is not possible that a subclass has more than one super classes.
For example in java we can't extends more than one super class for a sub class like this,
"public class subclass extends superclass1,superclass2,superclass3"

Java solve this problem using "INTERFACE". Interface is just like a java class that has variable/variables and method/methods but the only different is that interface have abstract methods and final fields.
In java it is possible to implement more than one interfaces by a subclass using the keywords "implements".

In the below example subclass named "SubClass.java" is extends one super class named "SuperClass.java" and implements two interfaces named "MyFirstInterface.java" and "MySecondInterface.java".

//SuparClass.java
public class SuparClass
{
public void DisplaySuperClassStatement()
{
System.out.println("I am in super class.");
}
}

//MyFirstInterface.java
public interface MyFirstInterface
{
public void DisplayMyFirstInterfaceStatement(); //nobody because of abstract method
}

//MySecondInterface.java
public interface MySecondInterface
{
public void DisplayMySecondInterfaceStatement(); //nobody because of abstract method
}

//SubClass.java
public class SubClass extends SuparClass implements MyFirstInterface,MySecondInterface
{
public void DisplayMyFirstInterfaceStatement()
{
System.out.println("I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface");
}
public void DisplayMySecondInterfaceStatement()
{
System.out.println("I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
System.out.println("Calling SuperClass's method");
sub.DisplaySuperClassStatement();
System.out.println("\n");
System.out.println("Calling first interface's method");
sub.DisplayMyFirstInterfaceStatement();
System.out.println("\n");
System.out.println("Calling second interface's method");
sub.DisplayMySecondInterfaceStatement();
}
}


Output =>
Calling SuperClass's method
I am in super class.


Calling first interface's method
I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface


Calling second interface's method
I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface

Varags example


/*Varags is nothing but "variable length argument in a method" i.e. we can define an unspecified number of parameters in a method.
To use varags we used three dot(...) known as "ELIPSIS" as mention in below example.*/

//VaragsClass.java
public class VaragsClass
{
void SumOfArray(int... number) /* (...) is known as elipsis, number is variable name, int is variable type*/
{
int sum=0;
for(int num:number)
{
sum = sum+num;
}
System.out.println("Summation = "+sum);
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
VaragsClass vrgs = new VaragsClass();
vrgs.SumOfArray(5,10,15,20,25,30); //Unspesified number of parameter
}
}


Output =>
Summation = 105

Abstract Method Example


/*To make overridden compulsory we make the method abstract with the help of abstract keyword.
When we make a method abstract then this method must be override in sub class.*/

//SuparClass.java
public abstract class SuparClass
{
abstract void Display(); /*abstract method don't has anybody. But we must have to define it in sub class.*/
}

//SubClass.java
public class SubClass extends SuparClass
{
void Display()
{
System.out.println("I am in subclass");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
sub.Display();
}
}


Output =>
I am in subclass

Overridden Example in java


/*If we call a method from subclass having same name,same return type and same parameter list as in super class, then the method in the sub class is invoked insted of super class. This is known as method overridden*/
//SuperClass.java
public class SuperClass
{
void Display()
{
System.out.println("I am in Super class");
}
}

//SubClass.java
public class SubClass extends SuparClass
{
void Display()
{
System.out.println("I am in subclass");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
sub.Display();
}
}

Output =>
I am in subclass
[NOTE: If we want that a method of super class has never been override by the subclass then we make the method of super class final with the help of final keyword.]

Hierarchical Inheritance Example


/*Class A is a parent class of both class B and class C i.e one Parent class has many sub classes.
And this is the concept of Hierarchical Inheritance.*/
//A.java
public class A
{
void DisplayA()
{
System.out.println("I am in A");
}
}

//B.java
public class B extends A
{
void DisplayB()
{
System.out.println("I am in B");
}
}

/c.java
public class C extends A
{
void DisplayC()
{
System.out.println("I am in C");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
System.out.println("Calling for subclass C");
C c = new C();
c.DisplayA();
c.DisplayC();

System.out.println("Calling for subclass B");
B b = new B();
b.DisplayA();
b.DisplayB();
}
}

Output =>
Calling for subclass C
I am in A
I am in C
Calling for subclass B
I am in A
I am in B

Multilevel Inheritance Example

/*In my below example class C is a subclass of class B and class B is again a subclass of class A.
This in nothing but a Multilevel Inheritance concept.
Now as a Multilevel Inheritance concept we can call class A as well as class B s' members from class C easily .*/


//A.java
public class A
{
void DisplayA()
{
System.out.println("I am in A");
}
}

//B.java
public class B extends A
{
void DisplayB()
{
System.out.println("I am in B");
}
}

//C.java
public class C extends B
{
void DisplayC()
{
System.out.println("I am in C");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
C c = new C();
c.DisplayA();
c.DisplayB();
c.DisplayC();
}
}


Output=>
I am in A
I am in B
I am in C

Single Inheritance Example

//One parent class (super class) and one child class (sub class)

//SuperClass.java
public class SuperClass
{
float length,width;
SuparClass(float l,float w)
{
length = l;
width = w;
}
float area()
{
return length*width;
}
}

//SubClass.java
public class SubClass extends SuparClass
{

float hight;

SubClass(float l,float w,float h)
{
super(l,w); //super() must be the first statement in sub class. Other wise it shows error.
hight = h;
}
float volume()
{
return length*width*hight;
}
}

//MainClass.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainClass
{
public static void main(String args[]) throws IOException
{

float length,width,hight;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter length: ");
length = Float.valueOf(br.readLine()).floatValue();

System.out.print("Enter width: ");
width = Float.valueOf(br.readLine()).floatValue();

System.out.print("Enter higth: ");
hight = Float.valueOf(br.readLine()).floatValue();

SubClass sub = new SubClass(length, width, hight);
float area = sub.area();
float volme = sub.volume();

System.out.println("Area = "+area+" unit^2");
System.out.println("Volume = "+volme+" unit^3");
}
}


Output=>
Enter length: 20
Enter width: 10
Enter higth: 5
Area = 200.0 unit^2
Volume = 1000.0 unit^3


Static method example in java

//StaticMemberClass.java
public class StaticMemberClass
{
static final double pi = 3.14;
static void areaOfCircle(double r)
{
System.out.println("Area of a circle of radius "+r+" = "+pi*r*r);
}
}

//MainClass.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainClass
{
public static void main(String args[]) throws IOException
{

double radius;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter redius: ");
radius = Double.valueOf(br.readLine()).doubleValue();

//No need to create an object to call a static members.
/*StaticMemberClass s = new StaticMemberClass();
s.areaOfCircle(radius);*/

StaticMemberClass.areaOfCircle(radius);
}
}





Output=>
Enter redius: 2
Area of a circle of radius 2 = 12.56

Saturday, August 27, 2011

Method OverLoding and Polymorphism concept with example


In java it is possible to create more than one methods with same name but different parameters.
This is known as method overloading.
Now when we call that method by an object then java interpreter find out the method name first and then match the number of parameters.
This process or this technique is known as Polymorphism.

In my below example I used two methods of same name "area()" and one has two parameters and other one has one parameter.
Now from the main method we do call area() method with two parameters when we enter our choice as "1" and call area() method with one parameter when we enter our choice as "2".

//Overloading example(OverLoadingExample.java)
public class OverLoadingExample
{
int l,w;
public void area(int a,int b)
{
l=a;
w=b;
System.out.println("area of rectangle = "+(l*w)+" sq.unit");
}

public void area(int a)
{
l=w=a;
System.out.println("area of square = "+(l*w)+" sq.unit");
}
}

//Main class (MyMainClass.java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MyMainClass
{
public static void main(String args[]) throws IOException
{

int choice,a,b;
OverLoadingExample OverLoadingExampleObject = new OverLoadingExample();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try
{
System.out.print("Enter 1 to get the area of rectangle and 2 for square:");
choice = Integer.valueOf(br.readLine()).intValue();

switch (choice)
{
case 1:
{
System.out.println("Enter length:");
a=Integer.valueOf(br.readLine()).intValue();

System.out.println("Enter width:");
b=Integer.valueOf(br.readLine()).intValue();

OverLoadingExampleObject.area(a,b);
break;
}
case 2:
{
System.out.println("Enter length/width:");
a=Integer.valueOf(br.readLine()).intValue();

OverLoadingExampleObject.area(a);
break;
}
default:
System.out.println("Please enter 1 or 2 to calculate area of rectangle and square respectively.");
break;
}
}
catch (NumberFormatException ne)
{
System.out.println(ne.getMessage()+" Please enter a numeric value.");
}
}
}


Output1=>
Enter 1 to get the area of rectangle and 2 for square:1
Enter length:
12
Enter width:
9
area of rectangle = 108 sq.unit


Output2=>
Enter 1 to get the area of rectangle and 2 for square:2
Enter length/width:
9
area of square = 81 sq.unit

Java constructor example

Before going to the constructor example just take a look what is it actually?
Basically java support a special type of method called constructor which initialize itself when object is first created.
Constructor method has the same name as the class name and it don't have any return type not even "void".
In bellow example "ConstructorClass(int a,int b)" is a constructor method and it is initialized when object is created like our example as,
ConstructorClass ConstructorClassObject = new ConstructorClass(a,b);
********************************************************
//ConstructorClass.java
public class ConstructorClass
{
int l,w;
ConstructorClass(int a,int b) //constructor method
{
l=a;
w=b;
}
public void area()
{
System.out.println("area of "+l+" unit length and "+w+" unit width = "+(l*w)+" sq.unit");
}
}

//MainClass.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainClass
{
public static void main(String args[]) throws IOException
{

int a,b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter length:");
a=Integer.valueOf(br.readLine()).intValue();

System.out.println("Enter width:");
b=Integer.valueOf(br.readLine()).intValue();

ConstructorClass ConstructorClassObject = new ConstructorClass(a,b);
ConstructorClassObject.area();
}
}



Output=>
Enter length:
5
Enter width:
4
area of 5 unit length and 4 unit width = 20 sq.unit

Class Object Example in java


//ClientClass.java
/*This is the main class from where we call a method [sum(parameter1,parameter2)] of another class named [MethodSuplierClass] by creating a object of that class[MethodSuplierClass] and use the "."(dot) operator. */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ClientClass
{
public static void main(String args[]) throws IOException
{

int a,b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter first numbers:");
a=Integer.valueOf(br.readLine()).intValue();

System.out.println("Enter second numbers:");
b=Integer.valueOf(br.readLine()).intValue();

MethodSuplierClass MethodSuplierClassObject = new MethodSuplierClass();
System.out.println("Sumassion of two numbers is :");
MethodSuplierClassObject.sum(a,b);
}
}


//MethodSuplierClass.java
public class MethodSuplierClass
{
public void sum(int a,int b)
{
System.out.println(a+"+"+b+"="+(a+b));
}
}


output =>
Enter first numbers:
15
Enter second numbers:
36
Sumassion of two numbers is :
15+36=51

Friday, August 26, 2011

Perfect Square example in java


//Find out your given number is perfect square number or not.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PerfectSquareExample
{
public static void main(String args[]) throws IOException
{

int a;
float b,f1,f2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a numbers:");
f1=Float.valueOf(br.readLine()).floatValue();

b=(float)Math.sqrt(f1);
a=(int)Math.sqrt(f1);
f2=(float)a;

if(b==f2)
System.out.println(f1+" is a perfect square number.");
else
System.out.println(f1+" is not a perfect square number");
}
}

Output1 =>
Enter a numbers:
64
64.0 is a perfect square number.

Output2 =>
Enter a numbers:
35
35.0 is not a perfect square number

Switch case example in java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SwitchCaseExample
{
public static void main(String args[]) throws IOException
{

int a,b,n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try
{
System.out.println("Enter two numbers:");
a=Integer.valueOf(br.readLine()).intValue();
b=Integer.valueOf(br.readLine()).intValue();

System.out.println("Enter 1 for Add "+a+","+b);
System.out.println("Enter 2 for Subtract "+a+","+b);
System.out.println("Enter 3 for Multiply "+a+","+b);
System.out.println("Enter 4 for Divide "+a+","+b);

System.out.println("Enter Your choice?");
n=Integer.valueOf(br.readLine()).intValue();

switch (n)
{
case 1:
System.out.println(a+"+"+b+"="+(a+b));
break;
case 2:
System.out.println(a+"-"+b+"="+(a-b));
break;
case 3:
System.out.println(a+"*"+b+"="+(a*b));
break;
case 4:
System.out.println(a+"/"+b+"="+((double)a/b));
break;
default:
System.out.println("Invalid Entry.");
break;
}
}
catch (NumberFormatException ne)
{
System.out.println(ne.getMessage()+" is not a numeric value.Please try again with a numeric value.");
}
}
}

Output1 =>
Enter two numbers:
25
4
Enter 1 for Add 25,4
Enter 2 for Subtract 25,4
Enter 3 for Multiply 25,4
Enter 4 for Divide 25,4
Enter Your choice?
4
25/4=6.25

Output2 =>
Enter two numbers:
25
8
Enter 1 for Add 25,8
Enter 2 for Subtract 25,8
Enter 3 for Multiply 25,8
Enter 4 for Divide 25,8
Enter Your choice?
5
Invalid Entry.

Output3 => (Not given any choice.)
Enter two numbers:
25
6
Enter 1 for Add 25,6
Enter 2 for Subtract 25,6
Enter 3 for Multiply 25,6
Enter 4 for Divide 25,6
Enter Your choice?

For input string: "" is not a numeric value.Please try again with a numeric value.

do while loop example in java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DoWhileExample
{
public static void main(String args[]) throws IOException
{

int n,i=1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//initialize
System.out.println("Enter number of lines you want to display?");
n=Integer.valueOf(br.readLine()).intValue();

do
{
for(int j=0;j {
System.out.print(i);
System.out.print(" ");
}
System.out.println("\n");
//increment
i++;
} while(i<=n); //condition
}
}


Output =>

Enter number of lines you want to display?
10
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

8 8 8 8 8 8 8 8

9 9 9 9 9 9 9 9 9

10 10 10 10 10 10 10 10 10 10

While loop example in java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WhileExample
{
public static void main(String args[]) throws IOException
{

int n,i=1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//initialize
System.out.println("Enter number of lines you want to display?");
n=Integer.valueOf(br.readLine()).intValue();
//condition
while(i<=n)
{
for(int j=0;j {
System.out.print(i);
System.out.print(" ");
}
System.out.println("\n");
//increment
i++;
}
}
}


Output =>

Enter number of lines you want to display?
10
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

8 8 8 8 8 8 8 8

9 9 9 9 9 9 9 9 9

10 10 10 10 10 10 10 10 10 10

Two Matrixs summation in java


//Summation of two matrixs

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TwoMatrixsSummation
{
public static void main(String args[]) throws IOException
{
int matrix_one_row,matrix_one_column,matrix_two_row,matrix_two_column;
int matrix_one[][]=new int[100][100];
int matrix_two[][]=new int[100][100];
int result[][]=new int[100][100];
int r,c;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//Get First Matrix row and column number.
System.out.println("How many row in first Matrix?");
matrix_one_row=Integer.valueOf(br.readLine()).intValue();
System.out.println("How many column in first Matrix?");
matrix_one_column=Integer.valueOf(br.readLine()).intValue();

//Get Second Matrix row and column number.
System.out.println("How many row in second Matrix?");
matrix_two_row=Integer.valueOf(br.readLine()).intValue();
System.out.println("How many column in second Matrix?");
matrix_two_column=Integer.valueOf(br.readLine()).intValue();

//Condition check.
if((matrix_one_row==matrix_two_row)&&(matrix_one_column==matrix_two_column))
{
//Get First Matrix value.
System.out.println("Enter First Matrix value:");
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
System.out.print("Enter MatrixOne["+r+"]["+c+"] value = ");
matrix_one[r][c]=Integer.valueOf(br.readLine()).intValue();
}
}

//Get Second Matrix value.
System.out.println("Enter Second Matrix value:");
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print("Enter MatrixTwo["+r+"]["+c+"] value = ");
matrix_two[r][c]=Integer.valueOf(br.readLine()).intValue();
}
}


//Print First Matrix.
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
System.out.print(matrix_one[r][c]+" ");
}
System.out.println("\n");
}

//Print a "+" sign.
System.out.println(" |+| ");

//Print Second Matrix.
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print(matrix_two[r][c]+" ");
}
System.out.println("\n");
}

//Print a "=" sign.
System.out.println(" = ");

//Summation of two matrixs
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
result[r][c]=matrix_one[r][c]+matrix_two[r][c];
}
}

//Print summation result of two Matrixs.
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print(result[r][c]+" ");
}
System.out.println("\n");
}
}
else
{
System.out.println("Summation is not possible as two matrixs have different order of rows and columns.");
}
}
}


Output1 =>

How many row in first Matrix?
2
How many column in first Matrix?
2
How many row in second Matrix?
2
How many column in second Matrix?
2
Enter First Matrix value:
Enter MatrixOne[1][1] value = 5
Enter MatrixOne[1][2] value = 5
Enter MatrixOne[2][1] value = 5
Enter MatrixOne[2][2] value = 5
Enter Second Matrix value:
Enter MatrixTwo[1][1] value = 6
Enter MatrixTwo[1][2] value = 4
Enter MatrixTwo[2][1] value = 3
Enter MatrixTwo[2][2] value = 9
5 5

5 5

|+|
6 4

3 9

=
11 9

8 14


Output2 =>

How many row in first Matrix?
2
How many column in first Matrix?
2
How many row in second Matrix?
3
How many column in second Matrix?
3
Summation is not possible as two matrixs have different order of rows and columns.

Summation of n numbers in java


//Adding 'n' number of input values.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SumUsingArray
{
public static void main(String args[]) throws IOException
{
String s;
int i,n,sum=0;
int number[]=new int[100];

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("How many number you want to adding?");
s = br.readLine();
n = Integer.valueOf(s).intValue();

for(i=1;i<=n;i++)
{
System.out.print("Enter number "+i+" = ");
number[i]=Integer.valueOf(br.readLine()).intValue();
sum=sum+number[i];
}
System.out.println("Total of the above numbers:");
for(i=1;i<=n;i++)
{
System.out.print(number[i]);
if(i!=n)
{
System.out.print("+");
}
}
System.out.print("="+sum);
}
}




Output =>

How many number you want to adding?
5
Enter number 1 = 10
Enter number 2 = 12
Enter number 3 = 14
Enter number 4 = 15
Enter number 5 = 19
Total of the above numbers:
10+12+14+15+19=70

Find out minimum number in java


//Find out Minimum number from the given three input.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MinNumber
{
public static void main(String args[]) throws IOException
{
String s1,s2,s3;
int n1,n2,n3;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//Get first number
System.out.print("Enter first number =");
s1 = br.readLine();
n1 = Integer.valueOf(s1).intValue();

//Get second number
System.out.print("Enter first number =");
s2 = br.readLine();
n2 = Integer.valueOf(s2).intValue();

//Get third number
System.out.print("Enter first number =");
s3 = br.readLine();
n3 = Integer.valueOf(s3).intValue();

//Logic to find out Minimum number.
if(n1 {
if(n1 System.out.println("Minimum number = "+n1);
else
System.out.println("Minimum number = "+n3);
}
else
{
if(n2 System.out.println("Minimum number = "+n2);
else
System.out.println("Minimum number = "+n3);
}
}
}


Output=>

Enter first number =25
Enter first number =36
Enter first number =9
Minimum number = 9

Find out maximum number in java


//Find out Maximum number between three input number.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MaxNumber
{
public static void main(String args[]) throws IOException
{
String s1,s2,s3;
int n1,n2,n3;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//Get first number
System.out.print("Enter first number =");
s1 = br.readLine();
n1 = Integer.valueOf(s1).intValue();

//Get second number
System.out.print("Enter first number =");
s2 = br.readLine();
n2 = Integer.valueOf(s2).intValue();

//Get third number
System.out.print("Enter first number =");
s3 = br.readLine();
n3 = Integer.valueOf(s3).intValue();

//Logic to find out Maximum number.
if(n1>n2)
{
if(n1>n3)
System.out.println("Maximum number = "+n1);
else
System.out.println("Maximum number = "+n3);
}
else
{
if(n2>n3)
System.out.println("Maximum number = "+n2);
else
System.out.println("Maximum number = "+n3);
}
}
}


Output=>
Enter first number =25
Enter first number =69
Enter first number =5
Maximum number = 69

InputStream example in java


//Enter a number and find out is your given number is +Ve or -Ve or ZERO.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamExample
{
public static void main(String args[]) throws IOException
{
String s;
int n;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter value of n =");
s = br.readLine();
n = Integer.valueOf(s).intValue();

if (n > 0)
System.out.println("("+n+") is a +Ve number");
else if (n<0)
System.out.println("("+n+") is a -Ve number");
else
System.out.println("Value you entered is ZERO");
}
}


Output 1=>
Enter value of n =12
(12) is a +Ve number


Output 2=>
Enter value of n =-15
(-15) is a -Ve number

Output 3=>
Enter value of n =0
Value you entered is ZERO

TypeCasting in java


//Type casting example .
public class TypeCasting
{
public static void main(String args[])
{
int i = 19;
float f = (float) i;

System.out.println("i=" + i);
System.out.println("f=" + f);
}
}


Output=>
i=19
f=19.0

Thursday, August 25, 2011

Java data type example


//Use of java data types .
public class JavaVariables
{
public static void main(String args[])
{
int i = 10;
float f = 20;
double d = 10.55;
char c = 'C';
String s = "Hi! I am a string.";


System.out.println("i = "+i);
System.out.println("f = "+f);
System.out.println("d = "+d);
System.out.println("c = "+c);
System.out.println("s = "+s);
}
}

Output =>
i = 10
f = 20.0
d = 10.55
c = C
s = Hi! I am a string.

Java println example with for loop


//With out type n-number of print statement, using "for " loop we can create *-triangle like PrintStar1.java
public class PrintStar2
{
public static void main(String args[])
{
for(int i=1;i<10;i++) //for loop
{
for(int j=0;j {
System.out.print("*");
}
System.out.print("\n"); //new line
}
}
}

Out Put =>
*
**
***
****
*****
******
*******
********
*********

Java println example


//This program print "*" in a triangle shape (PrintStar1.java)
public class PrintStar1
{
public static void main(String args[])
{
System.out.println(" *");
System.out.println(" ***");
System.out.println(" *****");
System.out.println(" *******");
System.out.println(" *********");
System.out.println(" ***********");
System.out.println("*************");
}
}

OUT PUT =>
*
***
*****
*******
*********
***********
*************

HelloWorld.java

//This program just print "Hello world! This is my first java program."


public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello world! This is my first java program.");
}
}

Nothing special ......

There is nothing special.
Just to re-brushing myself I do play with JAVA.
But today I thought why not to store all my programming in a BLOG in a sequence order so that it will be helpful to all the guise who want to do practice java programming.
I think it will be helpful to all the beginner who want to learn java as well as experience one who want to re-brushed themselves.