Social Icons

Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Wednesday, 4 December 2013

calculator Structure in java Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


/*<applet code="carry" width="300" height= "300"></applet>*/

public class carry extends Applet
{
static final int n=3;
int i,j,k;
TextField x;


public void init()
{





setLayout(new BorderLayout());
add(new TextField(15),BorderLayout.NORTH);
setLayout(new FlowLayout(FlowLayout.LEFT));
setFont(new Font("SansSerif",Font.BOLD,30));

add(new Button("+"));
add(new Button("-"));
add(new Button("*"));
add(new Button("/"));


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
   {
   k=i*n+j;
   if(k>0)
  add(new Button(""+k));
    }

}


}

}

Thursday, 14 November 2013

Moving fan using java Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Banner5" width=500 height=500>
</applet>
*/
public class Banner5 extends Applet implements Runnable,MouseListener,MouseMotionListener{

Thread t = null;

Thread t1 = null;
int x,y,state;
boolean stopFlag,st1;
// Set colors and initialize thread.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
addMouseListener(this);
addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent me)
{
t1 = new Thread(this);
stopFlag = false;
t1.start();

}
public  void mouseEntered(MouseEvent me)
{
t = new Thread(this);
stopFlag = false;
t.start();



}
public  void mouseExited(MouseEvent me)
{
stopFlag = true;
st1=true;
showStatus("Exited");
}

public  void mousePressed(MouseEvent me)
{}
public  void mouseReleased(MouseEvent me)
{}







public void mouseMoved(MouseEvent me)
{
 //repaint();

showStatus("hello");
}



public void run() {
char ch;

for( ; ; ) {
try {



repaint();
Thread.sleep(300);
x=x+10;
y=y+10;



if(stopFlag)
break;
} catch(InterruptedException e) {}
}
}


public void mouseDragged(MouseEvent me)
{

}















// Start thread
public void start() {
t = new Thread(this);
stopFlag = true;
t.start();
x=0;
}

// Pause the banner.
public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {

g.fillArc(100, 40, 100, 100, 0+x, 45);
g.fillArc(100, 40, 100, 100, 120+x, 45);
g.fillArc(100, 40, 100, 100, 240+x, 45);


g.fillArc(100, 200, 100, 100, 240-y, 45);
g.fillArc(100, 200, 100, 100, 120-y,45);
g.fillArc(100, 200, 100, 100, 0-y, 45);

}
}

Saturday, 26 October 2013

Right Moving String in Java Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*

<applet code=jitender width=400 height=400>
</applet>

*/


public class jitender extends Applet implements Runnable
{
Thread t=null;
boolean stopflag;
String msg,ch1;





  public void init()
  {
  setBackground(Color.cyan);
  setForeground(Color.red);
 }

 public void start()
  {
  msg="A simple jitender moving";
if(msg==null)
 {
  msg="message not found";
 }
msg=" "+msg;
 t=new Thread(this);
 stopflag=false;
  t.start();
}

public void run()
{
char ch;
String ch1;

 char p1[]=msg.toCharArray();
for( ; ;)
{
 try
   {
repaint();
Thread.sleep(250);

ch=p1[p1.length-1];

for(int i=p1.length-1;i>0;i--)
{
   p1[i]=p1[i-1];
 }
 p1[0]=ch;
ch1= String.valueOf(p1);
 msg=ch1;












   if(stopflag)
    {
    break;
     }


   }catch(InterruptedException e){}


}

}

public void stop()
{
   stopflag=true;
    t=null;
}

public void paint(Graphics g)
{
g.drawString(msg,50,30);
}

}

Sunday, 29 September 2013

Exception handling in java

class ex
{

 public static int quotient(int a,int b)
{

   if(b==0)
  {
  throw new ArithmeticException("divide by zero") ;
  }
 return a/b;


}


 public static void main(String args[])
 {
 int c;
 int a=5,b=0;
 

       
  try
  {
   c=quotient(a,b);
  System.out.println("the value of a/b is "+c);

  }



 catch(ArithmeticException e)
 {
  System.out.println("the b value can not be negative :");
 System.out.println(" :"+e.getMessage());
  System.out.println(" :"+e.toString());

  e.printStackTrace();
 }


     finally
       {
          System.out.println("hello finally is executed ");
 
              }            


}


}

Saturday, 28 September 2013

String character interchange in java

class str
{
public static void main(String args[])
{
String s="this is lecture class";
char temp;
String a[]=s.split(" ");

System.out.println("..........the String  are  ...... ");

for(int i=0;i<a.length-1;i++)
{
System.out.print("  "+a[i]);

}


System.out.println("  ");

for(int i=0;i<a.length-1;i++)
{
char p[]=a[i].toCharArray();
for(int j=0;j<p.length-1;j=j+2)
 {
  temp=p[j];
  p[j]=p[j+1];
  p[j+1]=temp;

}

 a[i]=new String(p);

}

System.out.println("..........the String after interchange of character ...... ");
for(int i=0;i<a.length-1;i++)
{
System.out.print("  "+a[i]);

}





}
}

Wednesday, 25 September 2013

check palindrome in java

import java.util.Scanner;
class pldm
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String which is palindrome ");
String s=in.nextLine();

if(ispalindrome(s))
{
System.out.println("word is palindrome");
}
else
System.out.println("word is not palindrome");
}

public static boolean ispalindrome(String s1)
{
int low=0;
int high=s1.length()-1;

  while(low<high)
{
if(s1.charAt(low)!=s1.charAt(high))
 {
return false;
 }
low++;
high--;
 }
 return true;

}
}







Monday, 16 September 2013

insertion sort in java


import java.util.Scanner;
class insert
{
  public static void main(String args[])
  {
  Scanner s = new Scanner(System.in);
  int i,b,c,temp;
  int j;
  int a[] = new int[5];
  System.out.println("\n Enter Element into the array: ");
  for(i=0;i<5;i++)
  {
   a[i]=s.nextInt();
  }
  System.out.println("\n The Element into the array are : ");
  for(i=0;i<5;i++)
  {
   System.out.print(a[i]+" ");
  }




   for(i=0;i<5;i++)
   {
    temp=a[i];
j=i-1;
   while(j>=0 && temp<a[j])
    {
      a[j+1]=a[j];

 j=j-1;
}
 a[j+1]=temp;

 
  }

    System.out.println("\n The Element after sorting into the array are : ");
  for(i=0;i<5;i++)
  {
   System.out.print(a[i]+" ");
  }





 }

}

Friday, 6 September 2013

Anonymous array


class Arr
{
static int i=5;

 static void F(int p[])
  {
  for(i=0;i<p.length;i++)
  {
  System.out.print(p[i]+" ");
 
   }
   }



 public static void main(String args[])
 {
  Arr a= new Arr();
   int i,j;

   F(new int[] {1,2,3,4});
 


  }

}

Wednesday, 4 September 2013

Dynamic Dispatch With abstract Function

abstract class A
{
int d1;
int d2;

abstract double area();

 A(int a, int b)
{
d1=a;
d2=b;

}

}


class B extends A
{
  B(int a,int b)
  {
  super(a,b);
  }

  double area()
  {
   return (d1*d2);
  }


}




class C extends A
{
 C(int a, int b)
 {
  super(a,b);
 }

 double area()
{
return d1*d2;
}

}

class over
{
public static void main(String args[])
{
C c=new C(5,3);
B b=new B(4,5);
A r;
r=c;
System.out.println(r.area());
r=b;
System.out.println(r.area());

}
}

Dynamic Method dispatch

class A
{


void show()
{
System.out.println("\n it is of a class");
}

}


class B extends A
{
void show()
{

System.out.println("\n it is of B class");
}


}




class C extends B
{
void show()
{

System.out.println("\n it is of C class");
}

}

class over
{
public static void main(String args[])
{
C c=new C();
A a =new A();
B b=new B();
A r;
r=a;
a.show();
r=b;
b.show();
r=c;
r.show();
}
}

Function overriding In java

class A
{


void show()
{
System.out.println("\n it is of a class");
}

}


class B extends A
{
void show()
{
super.show();
System.out.println("\n it is of B class");
}


}




class C extends B
{
void show()
{
super.show();
System.out.println("\n it is of C class");
}

}

class over
{
public static void main(String args[])
{
C c=new C();
c.show();
}
}

use of Super Keyword

class Box
{
double width;
double height;
double depth;

 Box(Box ob)
 {
  width=ob.width;
  height=ob.width;
  depth=ob.depth;
 }

 Box(double w, double h,double d)
 {
  width=w;
  height=h;
  depth=d;
 }
  Box(double d)
  {
  width=d;
  height=d;
  depth=d;

  }

 void volume()
 {

 System.out.println("\n the volume is " +width*height*depth);
 }
}

class B extends Box
{
double weight;
 B(double a,double b,double c, double d)
 {
 super(a,b,c);
 weight=d;
 }
  void show()
  {
 System.out.println("\n Weight is "+weight);
  }


}

class C
{
public static void main(String args[])
{
B ob=new B(5,6,10,4);
 ob.volume();
 ob.show();
}

}

Friday, 23 August 2013

Generating Random No. In java

import java.util.Random;
class Run
{

public static void main(String args[])
{
 int i;
 Random d = new Random(3);
 Random d1 = new Random(0);
 for(i=0;i<10;i++)
 {
 System.out.println("Print Random value having seed 3  " + d.nextInt(1000));
System.out.println("Print Random value having seed 0 : "+d1.nextInt(1000));
 }
 }

}

output:

Print Random value having seed 3  734
Print Random value having seed 0 : 360
Print Random value having seed 3  660
Print Random value having seed 0 : 948
Print Random value having seed 3  210
Print Random value having seed 0 : 29
Print Random value having seed 3  581
Print Random value having seed 0 : 447
Print Random value having seed 3  128
Print Random value having seed 0 : 515
Print Random value having seed 3  202
Print Random value having seed 0 : 53
Print Random value having seed 3  549
Print Random value having seed 0 : 491
Print Random value having seed 3  564
Print Random value having seed 0 : 761
Print Random value having seed 3  459
Print Random value having seed 0 : 719
Print Random value having seed 3  961
Print Random value having seed 0 : 854

Printing Date in java Program

import java.util.Date;
class New
{

public static void main(String args[])
{

 Date d = new Date();

 System.out.println("Time in millisecond is from 1 jun 1970 " + d.getTime());
System.out.println("Current Date and Time is : "+d.toString());
 }


}

output:

   e:\java_programs>java New
Time in millisecond is  1377282070857
Current Date and Time is : Fri Aug 23 23:51:10 IST 2013

Command line Input Java Program


class New
{
public static void main(String args[])
{

 int a = Integer.parseInt(args[0]);
 int b= Integer.parseInt(args[1]);

 int c=a+b;
 System.out.println("Addition of two no . is  "+c);
}


}


output:

               

e:\java_programs>java New 5 6
Addition of two no . is  11


Java Program Using Scanner class to take Input from user

import java.util.Scanner;
class New
{
public static void main(String args[])
{
 Scanner s= new Scanner(System.in);
 System.out.println("Enter an integer value ");

 int a = s.nextInt();
 System.out.println("Enter 2nd integer");
 int b= s.nextInt();
 int c=a+b;
 System.out.println("Addition of two no. is "+c);
}


}


output:


      e:\java_programs>java New
Enter an integer value
25
Enter 2nd integer
36
Addition of two no. is 61
 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management