Friday, July 23, 2010

Menu

Buzz It
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="men1" width=200 height=200>
</applet>
*/

class MenuFrame extends Frame
{
String msg="";
CheckboxMenuItem debug,test;
MenuFrame(String title)
{
super(title);
MenuBar mbar = new MenuBar();
setMenuBar(mbar);

Menu file =new Menu("file");
MenuItem item1,item2,item3,item4,item5;
file.add(item1 = new MenuItem("New..."));
file.add(item2 = new MenuItem("Open..."));
file.add(item3 = new MenuItem("Close..."));
file.add(item4 = new MenuItem("-"));
file.add(item5 = new MenuItem("quit..."));
mbar.add(file);

Menu edit =new Menu("edit");
MenuItem item6,item7,item8,item9;
edit.add(item6 = new MenuItem("cut"));
edit.add(item7 = new MenuItem("copy"));
edit.add(item8 = new MenuItem("paste"));
edit.add(item9 = new MenuItem("-"));

Menu sub = new Menu("special");
MenuItem item10,item11,item12;
sub.add(item10 = new MenuItem("First"));
sub.add(item11 = new MenuItem("Second"));
sub.add(item12 = new MenuItem("Third"));
edit.add(sub);

debug = new CheckboxMenuItem("debug");
edit.add(debug);
test = new CheckboxMenuItem("testing");
edit.add(test);

mbar.add(edit);

MyMenuHandler handler = new MyMenuHandler(this);
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addItemListener(handler); ;
test.addItemListener(handler)
MyWindowAdapteradapter=new MyWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on.",10,220);
else
g.drawString("Debug is off.",10,220);

g.drawString(msg,10,200);
if(test.getState())
g.drawString("Testing is on.",10,240);
else
g.drawString("Testing is off.",10,240);
}
}
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuframe)
{
this.menuFrame =menuFrame;
}
public void windowClosing(WindowEvent we)
{
menuFrame.setVisible(false);
}
}
class MyMenuHandler implements ActionListener,ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame = menuFrame;
}
public void actionPerformed(ActionEvent ae)
{
String msg="You Selected : ";
String arg =(String)ae.getActionCommand();
if(arg.equals("New..."))
msg += "New.";
else if(arg.equals("Open..."))
msg += "Open.";
else if(arg.equals("Close..."))
msg += "Close.";
else if(arg.equals("Quit..."))
msg += "Quit.";
else if(arg.equals("Edit"))
msg += "Edit.";
else if(arg.equals("copy"))
msg += "copy.";
else if(arg.equals("paste"))
msg += "paste.";
else if(arg.equals("First"))
msg += "First.";
else if(arg.equals("Second"))
msg += "Second.";
else if(arg.equals("Third"))
msg += "Third.";
else if(arg.equals("Debug"))
msg += "Debug.";
else if(arg.equals("Testing"))
msg += "Testing.";

menuFrame.msg =msg;
menuFrame.repaint();
}

public void itemStateChanged(ItemEvent ie)
{

menuFrame.repaint();
}
}

public class men1 extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("menu");
int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height"));
setSize(new Dimension (width,height));
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
}


OUTPUT:



Tabbed pane

Buzz It
import javax.swing.*;
import java.awt.*;
import java.applet.*;
/*
<applet code="tpane" width=200 height=200>
</applet>
*/
public class tpane extends JApplet
{
public void init()
{
JTabbedPane p1 = new JTabbedPane();
p1.addTab("cities",new city());
p1.addTab("colors",new color());
getContentPane().add(p1);
}
}
class city extends JPanel
{
public city()
{
JButton b1 = new JButton("new york");
add(b1);
JButton b2 = new JButton("las vegas");
add(b2);
JButton b3 = new JButton("chicago");
add(b3);
}
}
class color extends JPanel
{
public color()
{
JCheckBox c1 = new JCheckBox("red");
add(c1);
JCheckBox c2 = new JCheckBox("green");
add(c2);
JCheckBox c3 = new JCheckBox("blue");
add(c3);
}
}

OUTPUT

MultiLevel Inheritace

Buzz It
import java.io.*;
class box
{
private double width;
private double height;
private double depth;
box(box ob)
{
width=ob.width;
height=ob.height;
depth=ob.depth;
}
box(double w,double h,double d)
{
width =w;
height=h;
depth=d;
}
box()
{
width=-1;
height=-1;
depth=-1;
}
double volume()
{
return(height*width*depth);
}
}
class boxweight extends box
{
double weight;
boxweight(boxweight ob)
{
super(ob);MultiLevel Inheritace*/


import java.io.*;

class box

{

private double width;

private double height;

private double depth;

box(box ob)

{

width=ob.width;

height=ob.height;

depth=ob.depth;

}

box(double w,double h,double d)

{

width =w;

height=h;

depth=d;

}

box()

{

width=-1;

height=-1;

depth=-1;

}

double volume()

{

return(height*width*depth);

}

}

class boxweight extends box

{

double weight;

boxweight(boxweight ob)

{

super(ob);

weight=ob.weight;

}

boxweight(double w,double h,double d,double m)

{

super(w,h,d);

weight=m;

}

boxweight()

{

super();

weight=-1;

}

boxweight(double len,double m)

{

super();

weight=m;

}

}

class shipment extends boxweight

{

double cost;

shipment(shipment ob)

{

super(ob);

cost=ob.cost;

}

shipment(double w,double h,double d,double m,double c)

{

super(w,h,d,m);

cost=c;

}

shipment()

{

super();

cost=-1;

}

shipment(double len,double m,double c)

{

super(len,m);

cost=c;

}

}

class level

{

public static void main(String args[])

{

shipment s1=new shipment(10,20,15,10,3.41);

shipment s2=new shipment(2,3,4,0.76,1.28);

double vol;

vol=s1.volume();

System.out.println("volume of shipment is"+vol);

System.out.println("weight of shipment is"+s1.weight);

System.out.println("shipping cist is"+s1.cost);

System.out.println();

vol=s2.volume();

System.out.println("volume of shipment is"+vol);

System.out.println("weight of shipment is"+s2.weight);

System.out.println("shipping cist is"+s2.cost);

}

}




weight=ob.weight;
}
boxweight(double w,double h,double d,double m)
{
super(w,h,d);
weight=m;
}
boxweight()
{
super();
weight=-1;
}
boxweight(double len,double m)
{
super();
weight=m;
}
}
class shipment extends boxweight
{
double cost;
shipment(shipment ob)
{
super(ob);
cost=ob.cost;
}
shipment(double w,double h,double d,double m,double c)
{
super(w,h,d,m);
cost=c;
}
shipment()
{
super();
cost=-1;
}
shipment(double len,double m,double c)
{
super(len,m);
cost=c;
}
}
class level
{
public static void main(String args[])
{
shipment s1=new shipment(10,20,15,10,3.41);
shipment s2=new shipment(2,3,4,0.76,1.28);
double vol;
vol=s1.volume();
System.out.println("volume of shipment is"+vol);
System.out.println("weight of shipment is"+s1.weight);
System.out.println("shipping cist is"+s1.cost);
System.out.println();
vol=s2.volume();
System.out.println("volume of shipment is"+vol);
System.out.println("weight of shipment is"+s2.weight);
System.out.println("shipping cist is"+s2.cost);
}
}

OUTPUT


String at center

Buzz It
import java.applet.*;
import java.awt.*;
/*
<applet code="cstr" width=200 height=200>
</applet>
*/
public class cstr extends Applet
{
Font f=new Font("courier",Font.BOLD|Font.ITALIC,36);
String msg="WELCOME TO JAVA";
public void paint(Graphics g)
{
Dimension d=this.getSize();
g.setFont(f);
drawCenteredString(msg,d.width,d.height,g);
}
public void drawCenteredString(String s,int w,int h,Graphics g)
{
FontMetrics fm=g.getFontMetrics();
int x=(w-fm.stringWidth(s))/2;
int y=(fm.getAscent()+(h-(fm.getAscent()+fm.getDescent()))/2);
g.drawString(s,x,y);

}
}

OUTPUT



Moving face

Buzz It
import java.io.*;
import java.awt.*;
import java.applet.*;
/*<applet code="movingface" width=500 height=500>
</applet>
*/
public class movingface extends Applet implements Runnable
{
int x=5,y=5;
Thread t=null;
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
for(int i=0;i<100;i++)
{
try
{
repaint();
Thread.sleep(200);
x=x+10;
y=y+10;
}
catch(InterruptedException e)
{
System.out.println(e);
}}
}
public void paint(Graphics g)
{
g.drawOval(40+x,40+y,120,150);
g.drawOval(57+x,75+y,36,20);
g.drawOval(110+x,75+y,30,20);
g.fillOval(68+x,81+y,10,10);
g.fillOval(121+x,81+y,10,10);
g.drawOval(85+x,100+x,30,30);
g.fillArc(60+x,125+x,80,40,180,180);
g.drawOval(25+x,92+x,15,30);
g.drawOval(160+x,92+x,15,30);
}
}
OUTPUT

Back Color using scroll bar

Buzz It
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code=”scroll” width=500 height=500>
</applet>
*/
public class scroll extends Applet implements AdjustmentListener
{
Scrollbar s1;
Scrollbar s2;
Scrollbar s3;
int x,y,z;
Color c;
public void init()
{
s1=new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
s2=new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
s3=new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
add(s1);
add(s2);
add(s3);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
s3.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
x=s1.getValue();
y=s2.getValue();
z=s3.getValue();
c=new Color(x,y,z);
g.setColor(c);
setBackground(c);
}
}

OUTPUT

Sunday, July 04, 2010

Program to implement package

Buzz It
package code
pkj of-circle
package p;
public class circle
{
double a;
double pi=3.14;
public double circlearea(int h)
{
a=pi*h*h;
return a;
}
}

pjk of-rect
package p;
public class rect
{
double a;
public double rectarea(double l,double b)
{
a=l*b;
return a;
}
}

pkj of –triangle
package p;
public class triangle
{
double a;
double t=0.5;
public double triarea(double b,double h)
{a=t*b*h;
return a;
}
}

import p.*;
import java.io.*;
class areademo
{
public static void main(String args[])
{int d,h,b,a,s;
String choice;
int ch;
circle c=new circle();
rect r=new rect();
triangle t=new triangle();
try
{
DataInputStream in=new DataInputStream(System.in);
do
{ System.out.println("Enter your choice");
System.out.println("1.Area of Circle.");
System.out.println("2.Area of Triangle.");
System.out.println("3.Area of Rectangle.");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("Enter radius of circle");
d=Integer.parseInt(in.readLine());
System.out.println("Area of circle="+c.circlearea(d));
break;
case 2:
System.out.println("Enter base & height of triangle");
b=Integer.parseInt(in.readLine());
h=Integer.parseInt(in.readLine());
System.out.println("Area of Triangle="+t.triarea(b,h));
break;
case 3:
System.out.println("enter the length & breadth");
a=Integer.parseInt(in.readLine());
s=Integer.parseInt(in.readLine());
System.out.println("area="+r.rectarea(a,s));
break;
default:
System.out.println("invalid entry");
}
System.out.println("do you want to continue enter y or n");
choice=in.readLine();
}
while(choice.equals("y"));
}
catch(IOException ex)
{
System.out.println("Error");}}}

OUTPUT:

Different shapes

Buzz It
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="shaps" width=300 height=400>
</applet>
*/
public class shaps extends Applet implements ActionListener
{
static int p=0;
Button cir,rec,ply,lin;
String msg;
public void init()
{
cir =new Button("Circle");
rec=new Button("Rectangle");
ply=new Button("Polygon");
lin=new Button("Line");
add(cir);
add(rec);
add(ply);
add(lin);
cir.addActionListener(this);
rec.addActionListener(this);
ply.addActionListener(this);
lin.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Circle")
p=1;
if(ae.getActionCommand()=="Rectangle")
p=2;
if(ae.getActionCommand()=="Polygon")
p=3;
if(ae.getActionCommand()=="Line")
p=4;
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
switch(p)
{
case 1:
g.drawOval(100,100,200,200);
break;
case 2:
g.drawRect(100,100,200,200);
break;
case 3:
int a[]={400,200,950,190,45,234};
int b[]={500,300,250,190,50,234};
g.drawPolygon(a,b,6);
break;
case 4:
g.drawLine(0,0,100,100);
break;
}
}
}



OUTPUT:

Program to implement interface

Buzz It
interface area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class rect implements area
{
public float compute(float x,float y)
{return (x*y);
}
}
class circle implements area
{
public float compute(float x,float y)
{
return (pi*x*x);
}
}
class intrface
{public static void main(String args[])
{
rect re=new rect();
circle ci=new circle();
area ar;
ar=re;
System.out.println("Area of rectangle"+ar.compute(10,20));
ar=ci;
System.out.println("Area of circle="+ar.compute(10,0));
}
}


OUTPUT:

Complex number generation

Buzz It
import java.io.*;
import java.io.DataInputStream;
class input
{
int real,img;
void read()
{
try
{
System.out.println("enter the real part");
DataInputStream in=new DataInputStream(System.in);
real=Integer.parseInt(in.readLine());
System.out.println("enter the imaginery part");
img=Integer.parseInt(in.readLine());
System.out.print("u entered:"+real);
System.out.println("+i"+img);
}
catch(Exception e)
{

}
}
void write()
{
System.out.print("the result is="+real);
System.out.println("+i"+img);
}
}
class comp
{
public static void main(String args[])
{
input ob1=new input();
input ob2=new input();
input ob3=new input();
ob1.read();
ob2.read();
ob3.real=ob1.real+ob2.real;
ob3.img=ob1.img+ob2.img;
ob3.write();
}
}



OUTPUT:

Add Image

Sort String

Buzz It
import java.io.DataInputStream;
class sort
{ String s[]=new String[20];
int n;
void read()
{
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the limit");
n=Integer.parseInt(in.readLine());
System.out.println("Enter string to sort");

for(int i=0;i<n;i++)
{
s[i]=(in.readLine());
}
}
catch(Exception e)
{
System.out.println("Exception caught"+e);
}
}
void calc()
{

for(int i=0;i<n;i++)
{
for(int j=i+1;j<n ;j++)
{
if((s[i].compareTo(s[j]))>0)
{
String t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
}
void result()
{System.out.println("the sorted result is");
for(int i=0;i<n;i++)
System.out.println(s[i]);
}
}
class sortstrings
{
public static void main(String args[])
{
sort obj=new sort();
obj.read();
obj.calc();
obj.result();
}
}



OUTPUT:

Prime number generation

Buzz It
import java.io.*;
import java.io.DataInputStream;
class primeno
{
int n,i=0,j=2,f;
void read()
{
try
{
System.out.println("enter the limit");
DataInputStream in=new DataInputStream(System.in);
n= Integer.parseInt(in.readLine());
}
catch(Exception e)
{
System.out.println("caught exception"+e);
}
}
void gen()
{
while(i<n)
{
f=1;
for(int k=2;k<=(j/2);k++)
{
if(j%k==0)
{
f=0;
break;
}
}
if(f==1)
{
i++;
System.out.println(+j);
}
j++;
}
}
}
class prime
{
public static void main(String args[])
{
primeno p=new primeno();
p.read();
p.gen();}}

OUTPUT:

Generate Fibonacci series

Buzz It
import java.io.*;
import java.lang.*;
class fibonacci
{
int n,a=-1,b=1,c=0;
void read()
{
try
{
System.out.println("enter the limit");
DataInputStream in=new DataInputStream(System.in);
n= Integer.parseInt(in.readLine());
}
catch(Exception e)
{
System.out.println("caught exception"+e);
}
}
void gen()
{
int a=-1,b=1,c=0;
for(int i=0;i<=n;i++)
{
c=a+b;
a=b;
b=c;
System.out.println(+c);
}
}
}
class fibo
{
public static void main(String args[])
{
fibonacci f=new fibonacci();
f.read();
f.gen();
}
}


OUTPUT:

Display current date & time

Buzz It
import java.util.*;
import java.io.*;
class datetime
{
public static void main(String args[])
{
Date d=new Date();
System.out.println(d);
System.out.println();
long msec=d.getTime();
System.out.println("Milliseconds"+msec);
}
}

OUTPUT

Strange number generation

Buzz It
import java.io.DataInputStream;
class strange
{
public static void main(String args[])
{ int limit=0;
DataInputStream in=new DataInputStream(System.in);
try
{ System.out.println("limit");
limit=Integer.parseInt(in.readLine());
}
catch(Exception e)
{
}
for(int i=1;i<limit;i++)
{ int temp=i; int sum=0;
while(temp>0)
{
int c=temp%10;
sum=sum+c*c*c;
temp=temp/10;
}
if(i==sum)
{
System.out.println(i);
}

}
}
}
OUTPUT

Sorting n number

Buzz It
import java.io.*;
import java.lang.*;
class sorting{int lim;
int[] n=new int[20];
void read()
{ try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the limit");
lim=Integer.parseInt(in.readLine());
System.out.println("Enter the elements");
for(int i=0;i<lim;i++)
{
n[i]=Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{ System.out.println("Caught exception"+e);
}
}
void print()
{ for(int i=0;i<lim;i++)
{ for(int j=i+1;j<lim;j++)
{ if(n[i]>n[j])
{ int temp;
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
System.out.println("Sorted list is");
for(int i=0;i<lim;i++)
{
System.out.println(n[i]);
}
}
}
class sort{ public static void main(String args[])
{
sorting s=new sorting();
s.read();
s.print();
}
}
OUTPUT