Welcome To Uday Satya Blog

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to connect to DataBase using JDBC-ODBC Driver



Coming soon...

Java Index


INDEX

Basic Programs
1 Program to Demonstrate simple JAVA program
2 Program to compute the area of a circle
3 Program to demonstrate type casting in JAVA
4 Program to demonstrate the basic Arithmetic Operations
5 Program to find largest of three numbers
6 Program to print first 10 Fibonacci numbers
7 Program to Calculate the factorial of a number using RECURSION
8 Program to print prime numbers using CLA
9 Program to Demonstrate Command Line Argument
10 Program to convert temperature from Fahrenheit to Celsius using CLA
11 Program to produce conversion table dollar and rupees using CLA
12 Program to print area of right angled triangle using CLA
13 Program to print all the elements in an array using CLA
14 Program to find the sum of all the elements in an array
15 Program to print all the elements in a 2D array
16 Program to find the sum of two matrices
17 Program to find the difference of two matrices using CLA
18 Program to print the UPPER and LOWER triangle of a matrix
19 Program to find the trace of a matrix
20 Program to find the transpose of a matrix
21 Program to find the product of two matrices
22 Program to demonstrate a CLASS
23 Program to demonstrate a Constructor
24 Program to demonstrate a Parameterized Constructor
25 Program to Demonstrate THIS keyword
26 Program to Demonstrate Constructor Overloading
27 Program to pass objects as parameters
28 Program to Demonstrate Queue using Class
29 Program to Demonstrate Stack using Class
30 Program to Demonstrate Access Specifiers
31 Program to Demonstrate Static Variables
32 Program to Demonstrate Static Methods
33 Program to demonstrate an Inner class
34 Program to Demonstrate Type Wrapping
35 Program to Demonstrate Single Inheritance
36 Program to Demonstrate Multilevel Inheritance
37 Program to Demonstrate hierarchical Inheritance
38 Program to show SUPER Class variable to refer a sub class object
39 Program to Demonstrate Method Overriding
40 Program to Demonstrate Runtime Polymorphism
41 Program to Demonstrate Abstract Class
42 Program to Demonstrate FINAL Keyword
43 Program to Demonstrate FINAL Method
44 Program to Demonstrate FINAL Class
45 Program to Demonstrate INTERFACE
46 Program to access implementation through Interface references
47 Program to Demonstrate EXTENDING INTERFACE
48 Program to Demonstrate Stack using interface
49 Program to Demonstrate Queue using Interface
50 Program to Demonstrate a SIMPLE PACKAGE
Exception Handling
51 Program to Demonstrate Exception
52 Program to Demonstrate TRY CATCH
53 Program to Demonstrate CATCH ARITHMETIC Exceptions
54 Program to Demonstrate Multiple CATCH
55 Program to Demonstrate NESTED TRY
56 Program to Demonstrate FINALLY clause
57 Program to Demonstrate User Defined EXCEPTIONS
58 Program to Demonstrate Throws
MultiThreading
59 Program to Demonstrate MULTITHREADING
60 Program to Demonstrate Runnable Interface
61 Program to Demonstrate Thread Priorities
62 Program to Demonstrate Synchronized Block
63 Program to Demonstrate Synchronized Method
64 Program to Demonstrate Deadlock
65 Program to Demonstrate Thread Methods
66 Program to Create Threads using Thread Class
67 Program to Demonstrate Join() & isAlive() methods
68 Program to Show how to Get User Input from Keyboard
Applets
69 Program to Create a Frame Window using Applet
70 Program to Demonstrate Life cycle of methods in Applet
71 Program to display Parameters Passed from HTML
72 Program to Demonstrate all shapes in Graphics class
73 Program to Show a Hut,Mountains & Face
74 Program to Show Status of Applet Window
75 Program to Show Position of the Mouse
76 Program to Demonstrate Colors
77 Program to Pass Parameters perform Mathematical Operations
78 Program To Get Document Code Bases
79 Program to Show an Image
Event Handling
80 Program to demonstrate an Adapter
81 Program to Demonstrate Simple Banner
82 Program to Demonstrate Button
83 Program to Demonstrate Checkbox
84 Program to Demonstrate Radio Buttons (Checkbox Group)
85 Program to Demonstrate Choice
86 Program to Demonstrate File Dialog
87 Program to Demonstrate Labels
88 Program to Demonstrate Lists
89 Program to Demonstrate Menu
90 Program to Demonstrate Text Area
91 Program to Make a Login Window
92 Program to Make a Login Focus
93 Program to Demonstrate Mouse Adapter
94 Program to Demonstrate Mouse Events
95 Program to Demonstrate the key event handlers
96 Program to Demonstrate Border Layout
97 Program to Demonstrate Card Layout
98 Program to Test Fonts Using Mouse Events
99 Program to Demonstrate Grid Layout
100 Program to Demonstrate Scrollbar
JDBC
101 Program to Check Connection to Database
102 Program to retrieve Records from Database
103 Program to demonstrate Insertion into Database
104 Program to demonstrate Update into Database
105 Program to demonstrate Prepared Statement
106 Program to Demonstrate Data Entry using Applet
107 Program to Demonstrate Result Set Meta Data
108 Program to Demonstrate Transaction handling
109 Program to Demonstrate Callable Statement
110 Program to Demonstrate Result Set Meta Data
111 Program to Demonstrate Database Meta Data
RMI
112 Program to add Interface
113 Program to add Server
114 Program to add Client
115 Program for implementation for the interface
116 Create a Policy to Allow Permission for Everyone

1-10 PROGRAMS



01  Program to Demonstrate simple JAVA program

class A
{
public static void main(String args[])
{
System.out.println("Welcome to my World");
}
}

-----------------------------------------------------------------------------------------

Save  the  above  Program  as  A.java

Compile Program  in Command by using javac
C:\>javac A.java

Run the Program in Command by using java
C:\>java A

---------------------------------------------------------------------------------------------

OUTPUT :

Welcome to my World



02  Program to compute the area of a circle

class A2
{
public static void main(String args[])
{
int r=10;
System.out.println("Radius of Circle :" + r);
System.out.println("Area of Circle :" + r*r);
}
}

OUTPUT :

Radius of Circle : 10
Area of Circle : 314


03  Program  to demonstrate type casting in JAVA

class conversion
{
public static void main(String args[])
{
byte b;
int i=257;
double d=323.142;
System.out.println("\nConversion of int to byte.");
b=(byte) i;
System.out.println("i and b " +i+" "+b);
System.out.println("\nConversion of double to int.");
i=(int)d;
System.out.println("d and i " +d +" "+i);
System.out.println("\nConversion of double to byte.");
b=(byte)d;
System.out.println("d and b " +d + " " +b);
}
}

OUTPUT :

Conversion of int to byte.
i and b 257 1

Conversion of double to int.
d and i 323.142 323

Conversion of double to byte.
d and b 323.142 67


04  Program to demonstrate the basic Arithmetic Operations

class arith
{
public static void main(String args[])
{
int a=10,b=3;
System.out.println("sum is "+(a+b));
System.out.println("difference is "+(a-b));
System.out.println("product is "+(a*b));
System.out.println("quotient is "+(a/b));
System.out.println("remainder is "+(a%b));
}
}

OUTPUT :

sum is 13
difference is 7
product is 30
quotient is 3
remainder is 1


05  Program to find largest of three numbers

class large
{
public static void main(String args[])
{
int a=10,b=30,c=20;
System.out.println("Greatest of given 3 numbers");
if(a>b&&a>c)
System.out.println(+a);
else if(b>c)
System.out.println(+b);
else
System.out.println(+c);
}
}

OUTPUT :

Greatest of given 3 numbers
30


06  Program to print first 10 Fibonacci numbers

class fib
{
public static void main(String args[])
{
int i=9,f=0,s=1,temp;
System.out.println(+f);
while(i>0)
{
System.out.println(+s);
temp=f+s;
f=s;
s=temp;
i--;
}
}
}

OUTPUT :

0
1
1
2
3
5
8
13
21
34


07  Program to Calculate the factorial of a number using RECURSION

class Factorial
{
int fact(int n)
{
int result;
if(n==1)
return 1;
result=fact(n-1)*n;
return result;
}
}
class Recursion
{
public static void main(String args[]){
Factorial f=new Factorial();
System.out.println("Factorial of 3 is " +f.fact(3));
System.out.println("Factorial of 4 is " +f.fact(4));
System.out.println("Factorial of 5 is " +f.fact(5));
}
}

OUTPUT :

Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120


08  Program to print prime numbers using CLA

class primecla
{
public static void main(String s[])
{
int n,r;
n=Integer.parseInt(s[0]);
for(int i=2;i<=n;i++)
{
int c=0;
for(int j=1;j<=i;j++)
{
r=i%j;
if (r==0)
c+=1;
}
if(c==2)
System.out.println(i);
}}}

OUTPUT :

java primecla 10
2
3
5
7



09  Program to Demonstrate Command Line Argument

class CommandLine
{
public static void main(String args[])
{
for(int i=0; i
System.out.println("args[" +i + "]: "+args[i]);
}
}

OUTPUT :

java CommandLine 1 3 4 2 5
args[0]: 1
args[1]: 3
args[2]: 4
args[3]: 2
args[4]: 5


10  Program to convert temperature from Fahrenheit to Celsius scale using CLA

class ftoccla
{
public static void main(String s[])
{
int f;
double c;
f=Integer.parseInt(s[0]);
c=5*(f-32)/9;
System.out.println(f+" fahrenheit = "+c+" celsius");
}
}

OUTPUT :

java ftoccla 34

11-20 PROGRAMS



11   Program to produce the conversion table for dollar and rupees using CLA

class dollarcla
{
public static void main(String s[])
{
int n,d;
n=Integer.parseInt(s[0]);
System.out.println("Dollars\t\tRupees" );
for(int i=1;i<=n;i++)
{
d=Integer.parseInt(s[i]);
System.out.println(d+"\t=\t"+(d*45) );
}
}
}

OUTPUT :

java dollarcla 4 1 34 36 37
Dollars         Rupees
1       =       45
34      =       1530
36      =       1620
37      =       1665


12  Program to print area  of right angled triangle using CLA

class AreaTri
{
public static void main(String s[])
{
int a,b,area;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
area=a*b/2;
System.out.println("Area of Right angled Triangle = "+area);
}
}

OUTPUT :

java AreaTri 5 4

Area of Right angled Triangle = 10


13  Program to print all the elements in an array using CLA

class matrixcla
{
public static void main(String s[])
{
int n,m;
n=Integer.parseInt(s[0]);
System.out.println("The length of the matrix is: " +n);
System.out.println("The elements of the matrix are:");
for(int i=1;i<=n;i++)
{
m=Integer.parseInt(s[i]);
System.out.println(m);
}
}
}

OUTPUT :

java matrixcla 5 10 34 36 37 99
The length of the matrix is: 5
The elements of the matrix are:
10
34
36
37
99


14  Program to find the sum of all the elements in an array

class Sum
{
public static void main(String args[])
{
double nums[]={1.1,1.2,1.3,1.4,1.5};
double result=0;
System.out.println("The Elements of Array are :");
for(int i=0;i<5;i++)
            {
            System.out.println(nums[i]);
            result=result+nums[i];
            }
System.out.println("Sum is " + result);
}
}

OUTPUT :

The Elements of Array are :
1.1
1.2
1.3
1.4
1.5
Sum is 6.5


15  Program to print all the elements in a 2D array

class Matrix
{
public static void main(String s[]) 
{
int a[][]= {{1,2,3},{4,5,6}};
System.out.println("Number of Row= " + a.length);
System.out.println("Number of Column= " + a[1].length);
System.out.println("The elements of the matrix are : ");
for(int i = 0; i
{
for(int j = 0; j < a[1].length; j++)
{
System.out.print(" "+ a[i][j]);
} 
System.out.println();
}
}}

OUTPUT :

Number of Row= 2
Number of Column= 3
The elements of the matrix are :
 1 2 3
 4 5 6
16  Program to find the sum of two matrices

class addm
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,1,1},{0,1,0},{0,0,1}};
int c[][]= new int[3][3];
int i,j;
for(i=0;i<3;i++)
            for( j=0;j<3;j++)
                        c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(+c[i][j]+ "  ");
            System.out.println();
            }
}
}
                                   
OUTPUT :

2  3  4
4  6  6
7  8  10         


17  Program to find the difference of two matrices using CLA

class Matrixcla {
public static void main(String s[])  {
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int x,y,k=0;
System.out.println("First Matrix : ");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
x=Integer.parseInt(s[k]);
a[i][j]=x;
k++;
System.out.print(" "+ a[i][j]); } 
System.out.println(); } 
System.out.println("Second Matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++) {
y=Integer.parseInt(s[k]);
b[i][j]=y;
k++;
System.out.print(" "+b[i][j]); } 
System.out.println(); }
System.out.println("Difference of two matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++)
System.out.print(" "+(a[i][j]-b[i][j]));
System.out.println();
}}}

OUTPUT :
java Matrixcla 6 7 8 9 5 5 5 5
First Matrix :
 6 7
 8 9
Second Matrix :
 5 5
 5 5
Difference of two matrix :
 1 2
 3 4
18  Program to print the UPPER and LOWER triangle of a matrix
class UppLow{
public static void main(String arsg[]) {
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
System.out.println("the given matrix:");
for(int i=0;i<3;i++)  {
for(int j=0;j<3;j++) 
System.out.print(" "+a[i][j]);
System.out.println(); }
System.out.println("upper triangle of matrix:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i<=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println(); }
System.out.println("lower triangle of matrix:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i>=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println();
}}}

OUTPUT :
The given matrix:
1 2 3
4 5 6
7 8 9
Upper triangle of matrix:
1 2 3
  5 6
    9
Lower triangle of matrix:
1
4 5
7 8 9
19  Program to find the trace of a matrix

class Trace
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int i,j,sum=0;
System.out.println("The given matrix : ");
for(int i = 0; i <3; i++)
            {
            for(int j = 0; j <3; j++)
            System.out.print(a[i][j] + " "); 
            System.out.println();
            }
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        if(i==j)
                           sum=sum+a[i][j];
            }
System.out.println("Trace is : " +sum);
}
}
                                   
OUTPUT :

The given matrix :
1 2 3
4 5 6
7 8 9
Trace is : 15            


20  Program to find the transpose of a matrix

class Transpose
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
System.out.println("The given matrix : ");
for(int i = 0; i <3; i++)
            {
            for(int j = 0; j <3; j++)
            System.out.print(a[i][j] + " "); 
            System.out.println();
            }
System.out.println("Transpose of Matrix : ");
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(a[j][i]+ "  ");
            System.out.println();
            }
}
}
           
OUTPUT :

The given matrix :
1  2  3
4  5  6
7  8  9
Transpose of Matrix :
1  4  7
2  5  8
3  6  9