1. Simple example of if-else statement in Java.
class testif
{
public static void main(String A[])
{
int a=10,b=12;
if(a>b)
{
System.out.println("Greater is "+a);
}
else
{
System.out.println("Greater is "+b);
}
}
}
2. Simple example of while loop in Java.
class testwhile
{
public static void main(String A[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
3. Simple example of do-while loop in Java.
class testdo
{
public static void main(String A[])
{
int i=1;
do
{
System.out.println(i);
i++;
}
while(i<=10);
}
}
4. Simple example of for loop in Java.
class testwhile
{
public static void main(String A[])
{
int i;
for(i=1;i<=15;i++)
{
System.out.println(i);
}
}
}
5. Switch statement in Java
class TestSw
{
public static void main(String a[])
{
char ch;
ch='e';
switch(ch)
{
case 'A':
case 'a':
{
System.out.println(ch+" is a vowel ");
break;
}
case 'E':
case 'e':
{
System.out.println(ch+" is a vowel ");
break;
}
case 'I':
case 'i':
{
System.out.println(ch+" is a vowel ");
break;
}
case 'O':
case 'o':
{
System.out.println(ch+" is a vowel ");
break;
}
case 'U':
case 'u':
{
System.out.println(ch+" is a vowel ");
break;
}
default:
{
System.out.println(ch+" is not a vowel ");
}
}
}
}
6. An example of command line arguments.
class TestArg
{
public static void main(String a[]) // Here a[] is a command line argument
{
String s=a[0]; // First command line stored on 's' variable
int x=Integer.parseInt(a[1]); // Second command line argument stored on x variable
int y=Integer.parseInt(a[2]); // Third command line argument stored on y variable.
int c=x+y;
System.out.println("String Enter by You : "+s);
System.out.println("Sum of numbers : "+c);
}
}
How to Run
/> java TestArg Java 3 4
7. Simple program to define functions and objects and accessing member function using objects
class MyClass
{
int x,y;
void getData()
{
x=10;
y=20;
}
void putData()
{
System.out.println("X = "+x);
System.out.println("Y = "+y);
}
public static void main(String arg[])
{
MyClass m=new MyClass();
m.getData();
m.putData();
}
}
8. Simple program to define functions with arguments and accessing using objects
class MyClass
{
int x,y;
void getData(int a,int b)
{
x=a;
y=b;
}
void putData()
{
System.out.println("X = "+x);
System.out.println("Y = "+y);
}
public static void main(String arg[])
{
MyClass m=new MyClass();
m.getData(4,5);
m.putData();
}
}
9. Simple Program to overload a method.
class MetOver
{
void show()
{
System.out.println("Greetings.......!");
}
void show(int x)
{
System.out.println("Integer Value - X : "+x);
}
void show(int x,int y)
{
System.out.println("Integer Values - X : "+x+" Y : "+y);
}
void show(float n)
{
System.out.println("Floating Values - N : "+n);
}
public static void main(String a[])
{
MetOver f=new MetOver();
f.show();
f.show(4);
f.show(5,6);
f.show(4.5f);
}
}
10. Program to overload area method to calculate area different shapes.
class TestArea
{
void area(float r)
{
float p=(float)3.14;
float a=p*r*r;
System.out.println("Area of Circle is "+a);
}
void area(float l, float b)
{
System.out.println("Area of Rectangle is "+l*b);
}
void area(double x)
{
System.out.println("Area of Square is "+x*x);
}
public static void main(String arg[])
{
TestArea t=new TestArea();
t.area(3.0f);
t.area(4.3f,3.5f);
t.area(4.2);
}
}
11. A program to illustrate the default constructor.
class ConsExam
{
int x,y;
ConsExam() //Default constructor
{
x=10;
y=100;
}
void display()
{
System.out.println("X = "+x+" Y = "+y);
}
public static void main(String a[])
{
ConsExam c=new ConsExam(); //Calling Constructor
c.display();
}
}
12. Program to illustrate the use of constructor with arguments.
class ConsArg
{
int x,y;
ConsArg(int a,int b)
{
x=a;
y=b;
}
int sum()
{
return(x+y);
}
public static void main(String a[])
{
ConsArg c=new ConsArg(4,8);
int s=c.sum();
System.out.println("Sum is "+s);
}
}
13. Example for Overloading Constructor
class ConsOvr
{
int x,y;
ConsOvr() //Default Constructor without arguments
{
x=2;
y=4;
}
ConsOvr(int n) //Constructor with one argument
{
x=y=n;
}
ConsOvr(int a,int b) //Costructor with two arguments
{
x=a;
y=b;
}
void show()
{
System.out.println("X = "+x+" Y = "+y);
}
public static void main(String a[])
{
ConsOvr c1=new ConsOvr(); //Calling first constructor
c1.show();
ConsOvr c2=new ConsOvr(4); //Calling second Constructor
c2.show();
ConsOvr c3=new ConsOvr(6,8); //Calling third Constructor
c3.show();
}
}
14. An example of java static member variables.
class StatVar
{
static int x;
int a;
void increment()
{
x++;
a++;
}
void show()
{
System.out.println("Static variable : "+x);
System.out.println("Non-Static variable : "+a);
}
public static void main(String a[])
{
StatVar s1=new StatVar();
StatVar s2=new StatVar();
StatVar s3=new StatVar();
s1.increment();
s2.increment();
s3.increment();
s1.show();
s2.show();
s3.show();
}
}
15. Illustrating the use of Static Methods in java
class StatMeth
{
static int count;
static void sum(int x,int y)
{
count++;
System.out.println("Sum : "+x+y);
}
static int multiply(int a,int b)
{
count++;
return (a*b);
}
public static void main(String a[])
{
StatMeth.sum(4,5);
int mul=StatMeth.multiply(2,5);
System.out.println("Multiplication : "+mul);
System.out.println("Count : "+StatMeth.count);
}
}
16. Nesting of Methods. Calling a method within another method
class MethNes
{
int square(int n)
{
return (n*n);
}
int mul(int m,int n)
{
return (m*n);
}
void display()
{
int a=2,b=4;
int r=square(a)+square(b)+mul(2,mul(a,b)); //Nesting Method
System.out.println("Answer is "+r);
}
public static void main(String a[])
{
MethNes m=new MethNes();
m.display();
}
}
17. An Example of Single Inheritance.
class Base
{
int square(int n)
{
return (n*n);
}
}
class Derived extends Base
{
int mul(int a,int b)
{
return (a*b);
}
}
class TestInh
{
public static void main(String arr[])
{
Derived d=new Derived();
int a=10,b=5;
int s=d.square(a)+d.square(b)+d.mul(2,d.mul(a,b));
System.out.println("Result is "+s);
}
}
18. Calling Base class constructor with derived class constructor using Super keyword.
class Base
{
int a;
Base(int x)
{
a=x;
}
void display()
{
System.out.println("Base - A : "+a);
}
}
class Derived extends Base
{
int a,b;
Derived(int x, int y)
{
super(x); //call to base constructor- one argument is passed
a=x;
b=y;
}
void show()
{
System.out.println("Derived - A : "+a+" - B : "+b);
}
}
class TestSup
{
public static void main(String ar[])
{
Derived d=new Derived(4,5);
d.display();
d.show();
}
}
19. Program to implement Multilevel Inheritance in Java.
class TestA
{
void display()
{
System.out.println("This Method is in class A");
}
}
class TestB extends TestA
{
void show()
{
System.out.println("This Method is in class B");
}
}
class TestC extends TestB
{
void greet()
{
System.out.println("Good Morning ..... !");
}
}
class Test
{
public static void main(String a[])
{
TestC c=new TestC();
c.display();
c.show();
c.greet();
}
}
20. an example of Hierarchical Inheritance.
class TestSq
{
float square(float x)
{
return (x*x);
}
}
class TestArea extends TestSq
{
float r=3.2f;
void area()
{
float ar=(float)3.14*square(r);
System.out.println("Area is "+ar);
}
}
class TestMath extends TestSq
{
float a=2.43f,b=4.12f;
void formula()
{
float r=(float)square(a)+square(b)+2*a*b;
System.out.println("Result "+r);
}
}
class TestAll
{
public static void main(String arg[])
{
TestArea A=new TestArea();
A.area();
TestMath M=new TestMath();
M.formula();
System.out.println("Square : "+A.square(3.3f));
System.out.println("Square : "+M.square(3.0f));
}
}
21. Overriding a Method in Java
class Base
{
void display()
{
System.out.println("Base Class Method");
}
}
class Derived extends Base
{
void display()
{
System.out.println("Derived Class Method");
}
}
class TestOver
{
public static void main(String a[])
{
Derived D=new Deived();
D.display();
}
}
22. Declaring a constant using Final Keyword.
class TestCons
{
public static void main(String a[])
{
final int n=100; //Constant-cannot be changed
n=200;
System.out.println("N = "+n);
}
}
23. Final Keyword stops overriding a Method.
class Super
{
final void display() //can not be overrided
{
System.out.println("Super class Method");
}
}
class Sub extends Super
{
void display()
{
System.out.println("Super class Method");
}
}
class TestFinal
{
public static void main(String a[])
{
Sub s=new Sub();
s.display();
}
}
24. A Final class cannot be inherited.
final class Super //class cannot be inherited
{
void display()
{
System.out.println("Super class Method");
}
}
class Sub extends Super
{
void display()
{
System.out.println("Super class Method");
}
}
class TestFinal
{
public static void main(String a[])
{
Sub s=new Sub();
s.display();
}
}
25. Destructor in java- Finalize method
class TestDes
{
TestDes()
{
System.out.println("Initialization");
}
public void finalize()
{
System.out.println("Finalize Called - Destroyed");
}
public static void main(String a[])
{
TestDes t=new TestDes();
t=null;
System.gc();
}
}
26. Program to illustrate static is called before every thing even constructor.
class TestSt
{
static
{
System.out.println("Static is called");
}
TestSt()
{
System.out.println("Constructor is called");
}
void display()
{
System.out.println("Display Method is Called");
}
public static void main(String arg[])
{
System.out.println("Main is Called");
TestSt t=new TestSt();
t.display();
}
}
27. A Simple Example of Array in Java.
class TestArr
{
public static void main(String arg[])
{
int arr[]=new int[10];
int i;
for(i=1;i<=10;i++)
{
arr[i-1]=i;
}
for(i=0;i<10;i++)
{
System.out.println(arr[i]);
}
}
}
28. Storing, Sorting and Displaying elements of Arrays using Methods and Objects in Java.
import java.io.*;
import java.util.*;
class ArrCls
{
int arr[]=new int[10];
static DataInputStream d=new DataInputStream(System.in); //creating a stream object for input
static StringTokenizer st;
void inpArr() throws IOException //Exception handling for input (readLine())
{
int i;
for(i=0;i<10;i++)
{
System.out.print("Enter a Number : ");
st=new StringTokenizer(d.readLine()); //reading data from command line
arr[i]=Integer.parseInt(st.nextToken());
}
}
void outArr()
{
int i;
for(i=0;i<10;i++)
{
System.out.print(" "+arr[i]+" ");
}
System.out.println();
}
void sort()
{
int i,j,swap;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(arr[j]>arr[i])
{
swap=arr[i];
arr[i]=arr[j];
arr[j]=swap;
}
}
}
}
public static void main(String arg[]) throws Exception
{
ArrCls A=new ArrCls();
A.inpArr();
A.outArr();
A.sort();
A.outArr();
}
}
29. An Example of Two Dimensional Array.
class TestMar
{
public static void main(String ar[])
{
int i,j, arr[][]=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
arr[i][j]=1;
}
else
{
arr[i][j]=0;
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
30. An Example of Dynamic Array (Variable Size Array).
class TestDArr
{
public static void main(String arg[])
{
int a[][]=new int[4][];
int i,j,c=1;
a[0]= new int[2];
a[1]= new int[4];
a[2]= new int[2];
a[3]= new int[4];
for(i=0;i<4;i++)
{
if(i%2==0)
{
for(j=0;j<2;j++)
{
a[i][j]=c;
}
}
else
{
for(j=0;j<4;j++)
{
a[i][j]=c;
}
}
c++;
}
for(i=0;i<4;i++)
{
if(i%2==0)
{
for(j=0;j<2;j++)
{
System.out.print (a[i][j]+" ");
}
}
else
{
for(j=0;j<4;j++)
{
System.out.print (a[i][j]+" ");
}
}
System.out.println();
}
}
}
31. An Example of String and String Functions
class TestString
{
public static void main(String arg[])
{
String str1=new String("Java Programming");
String str2[]=new String[20];
System.out.println("String : "+str1);
System.out.println("Upper Case: "+str1.toUpperCase());
System.out.println("Lower Case : "+str1.toLowerCase());
System.out.println("Length : "+str1.length());
System.out.println("Index of r : "+str1.indexOf('r'));
System.out.println("Index of r after 8th character : "+str1.indexOf('r',8));
System.out.println("Substring from 5th character : "+str1.substring(5));
System.out.println("Substring from 5th to 12th character : "+str1.substring(5,12));
}
}
32. An example to compare Strings in Java
class TestStr
{
public static void main(String a[])
{
String s1=new String("Java");
String s2=new String("Programming");
int n=s1.compareTo(s2);
if(n==0)
{
System.out.println("Both strings are equal");
}
else if(n>0)
{
System.out.println("First string is greater");
}
else
{
System.out.println("Second string is greater");
}
}
}
33. Simple Example of vectors in Java
import java.util.*; //importing util package to use vectors
class TestVect
{
public static void main(String[] arg)
{
Vector v = new Vector();
v.add(10);
v.add(20);
v.add("Programming");
v.add("in");
v.add("Java");
v.add(100);
System.out.println("Vector is " + v);
}
}
33. Simple Example of vectors with index number
import java.util.*;
class TestVect {
public static void main(String[] arg)
{
Vector v = new Vector();
v.add(0,10);
v.add(1,20);
v.add(2,"Programming");
v.add(3,"in");
v.add(4,"Java");
v.add(5,100);
System.out.println("Vector is " + v);
}
}
8