Saturday 19 May 2012

Program to Sort an Array

class sort
{
public static void main(String args[])
{
int a[ ] ={3,5,4,1,2};
int temp, i , j ;
System.out.println("The Array before Sorting :");
for(i=0;i<a.length;i++)
{
System.out.println(" "+a[i]);
}
for(i=0 ; i < a.length ; i++)
{
for(j=i+1;j< a.length ; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
System.out.println("The Array After Sorting :");
for(i = 0;i<a.length ;i++)
{
System.out.println("  "+a[i]);
}
}
}
}

Prime Factor Program in Java

class PrimeFactors
{
public static void main(String args[])
{
int num =9;
int i , j , flag =0;
System.out.println("The Prime Factors for number "+num+"  are :");
for(i=0;i<n;i++)
{
if(n % i == 0)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag =1;
break;
}
else
{
flag=0;
}
}
if(flag == 0|| i == 2)
{
System.out.println(" "+i);
}
}
}
}
}

Monday 11 July 2011

Traffic Lights program in java




// This is an creative program created by me to give you the creative example of the
// applets in java
// hope you will find this program interesting

import java.awt.*;
import java.applet.*;
import java.awt.Color.*;
/*
<applet code="TrafficLights" width=1000 height=675>
</applet>
*/
public class TrafficLights extends Applet
{
public int y=800;
public void paint(Graphics g)
{
try
{
g.drawRect(50,50,150,400);
g.setColor(Color.black);
g.fillRect(50,50,150,400);
g.drawRect(80,400,85,520);
g.setColor(Color.black);
g.fillRect(80,400,85,520);
g.drawRect(40,520,950,525);
g.setColor(Color.gray);
g.fillRect(40,520,950,525);
g.setColor(Color.red);

g.fillOval(75,75,100,100);
g.setColor(Color.red);
g.fillRect(y+5,530,60,60);
g.setColor(Color.black);
g.fillOval(y,590,30,30);
g.fillOval(y+40,590,30,30);
Thread.sleep(10000);
}
catch(InterruptedException ie)
{
}


try
{
g.drawRect(50,50,150,400);
g.setColor(Color.black);
g.fillRect(50,50,150,400);
g.drawRect(80,400,85,520);
g.setColor(Color.black);
g.fillRect(80,400,85,520);
g.drawRect(40,520,950,525);
g.setColor(Color.gray);
g.fillRect(40,520,950,525);
g.setColor(Color.yellow);
g.fillOval(75,225,100,100);
g.setColor(Color.red);
g.fillRect(y+5,530,60,60);
g.setColor(Color.black);
g.fillOval(y,590,30,30);
g.fillOval(y+40,590,30,30);
Thread.sleep(5000);
}
catch(InterruptedException ie)
{
}



try
{
g.drawRect(50,50,150,400);
g.setColor(Color.black);
g.fillRect(50,50,150,400);
g.drawRect(80,400,85,520);
g.setColor(Color.black);
g.fillRect(80,400,85,520);
g.drawRect(40,520,950,525);
g.setColor(Color.gray);
g.fillRect(40,520,950,525);
g.setColor(Color.green);
g.fillOval(75,300,100,100);
for(int i=0;i<80;i++)
{
g.setColor(Color.gray);
g.fillRect(y+5,530,60,60);
g.fillOval(y,590,30,30);
g.fillOval(y+40,590,30,30);
y=y-10;

g.setColor(Color.red);
g.fillRect(y+5,530,60,60);
g.setColor(Color.black);
g.fillOval(y,590,30,30);
g.fillOval(y+40,590,30,30);
Thread.sleep(200);
}
Thread.sleep(10000);
}
catch(InterruptedException ie)
{
}


}
}



OUTPUT :
//In output you will see wheel car moving at the green light.


Monday 4 July 2011

Concept of isAlive and join in java

isAlive : This method is used to check whether a thread is in the running state or not .
Join : This method is use to specify the time you want to wait for the specified thread to terminate .

Example :

class Demo
{
String arr[]={"Here comes the I'st child ",
                    "Here comes the II'nd child",
                    "Here comes the III'rd child"};
public void run()
{
try
{
for(int i=0;i<3;i++)
{
Thread.sleep(3000);
System.out.println(arr[i]);
}
}
catch(InterruptedException e)
{
}
}
}
class Demojoin
{
public static void main(String args[])
{
System.out.println("The School Bus is waiting for the three children at the Bus Stop");
Demo ob1 = new Demo();
Thread t = new Thread(ob1);
t.start();
while(t.isAlive()) //Here you can make out the use of isAlive concept
{
try
{
System.out.println("Still waiting ........ ");
join(1000);
}
catch(InterrutptedException e)
{
}
}
System.out.println("Finally!!");
}
}

OUTPUT :

AbeBooks Logo Canada

Thursday 16 June 2011

Program to Print the sum of Rows of 2-D Array

class Sum
{
public static void main ( String args[ ])
{
int s[ ] = new int[3];
int a[ ][ ] = {{1,2,3},{3,4,5},{6,7,8}};
int  r, c;

for(r=0 ; r<a.length ; r++)
{
System.out.print( "|" );
for(c=0 ; c<a.length ; c++)
{
System.out.print( a[r][c]+" "); 
}
System.out.println( "|" );
}

for(r=0 ; r<a.length ; r++)
{
s[r] = 0;
}
for(r=0 ; r<a.length ; r++)
{
for(c=0 ; c<a.length ; c++)
{
s[r] = s[r] + a[r][c] ;
}
}
for(r=0 ; r<3 ; r++)
{
System.out.printlln("The sum of "+(r+1)+" row of an array is :" +s[r]);
}
}
}

OUTPUT 


















Program to Print Triangle Pattern in java

Q.
           *
      *    *
   *    *    *
*    *    *    * 


Solution :

class triangle
{
public static void main ( String args [ ] )
{
int  i , j , k;

for( i =1 ; i < 5 ; i ++ )
{
for( j =1 ; j <5-i ; j++)
{
System.out.print(" ");
}
for( k =i ; k >0 ; k--)
{
System.out.print("* ");
}
System.out.println(" ");
}
}
}

Program to Reverse a number in java

class Reverse
{
public static void main(String args[ ])
{
int rev=0; // Initializing a variable
int temp;
int n = 432;
while(n!=0)
{
temp = n%10;
rev = rev * 10 + temp ;
n = n/10;
}
System.out.println("The Reverse of the number "+n+" is : "+ rev);
}
}

OUTPUT :

The Reverse of the number 432 is : 234