Java Full Programs
Java Full Programs
package pack1;
public class emp
{
public int bp,eno,a;
public String name,des;
public emp()
{
name="DSD";
des="Programmer";
eno=70245;
}
public emp(int a)
{
this.a=a;
}
public void print()
{
System.out.println("Emp Name:"+name);
System.out.println("Emp No:"+eno);
System.out.println("Emp Desc:"+des);
}
public void print1()
{
System.out.println("Emp Salary:"+a);
}
}
import pack1.*;
class imp
{
public static void main(String args[])
{
emp ob=new emp();
emp ob1=new emp(2345);
ob.print();
ob1.print1();
}}
OUTPUT:
E:\jdk1.3\bin>javac imp.java
E:\jdk1.3\bin>java imp
Emp Name:DSD
Emp No:70245
Emp Desc:Programmer
Emp Salary:2345
INHERITANCE, POLYMORPHISM AND INNER CLASS
import java.io.*;
class number
{
public int a;
void getint()
{
try
{
System.out.println("Enter an integer value :");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
a=Integer.parseInt(s);
}
catch(IOException e)
{
}
}
}
class squ extends number
{
int rect()
{
int y=(a*a);
System.out.println("The square of given value:" +y);
return y;
}
int rect(int a,int b)
{
int z=(a+b);
System.out.println("The square of given value:" +z);
return z;
}
}
class parsel extends squ
{
class contents
{
private int i=424;
public int value()
{
return i;
}
}
public void ship()
{
contents c=new contents();
System.out.println("Value :" +c.value());
}
public static void main(String args[])
{
parsel p=new parsel();
p.ship();
squ s=new squ();
s.getint();
s.rect();
s.rect(3,5);
}
}
OUTPUT:
E:\jdk1.3\bin>javac parsel.java
E:\jdk1.3\bin>java parsel
Value :424
Enter an integer value :
5
The square of given value:25
The square of given value:8
E:\jdk1.3\bin>
SHAPES
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class f extends JFrame
{
boolean rect=false,circle=false,sq=false,ova=false;
public f()
{
JMenu smenu=new JMenu("shapes");
JMenuItem rec=new JMenuItem("Rectangle(red)");
smenu.add(rec);
rec.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rect=true;
circle=false;
sq=false;
ova=false;
Paint();
}
}
);
sqr.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rect=false;
circle=false;
sq=true;
ova=false;
Paint();
}
}
);
oval.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rect=false;
circle=false;
sq=false;
ova=true;
Paint();
}
}
);
circ.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rect=false;
circle=true;
sq=false;
ova=false;
Paint();
}
}
);
}
public void Paint()
{
Graphics g=getGraphics();
if(rect)
{
g.clearRect(100,100,500,500);
Color c=new Color(250,0,0);
g.setColor(c);
g.fillRect(150,150,450,250);
}
if(sq)
{
g.clearRect(100,100,500,500);
Color c=new Color(0,0,250);
g.setColor(c);
g.fillRect(150,150,250,250);
}
if(circle)
{
g.clearRect(100,100,500,500);
Color c=new Color(0,250,0);
g.setColor(c);
g.fillOval(150,150,250,250);
}
if(ova)
{
g.clearRect(100,100,500,500);
Color c=new Color(0,0,0);
g.setColor(c);
g.fillOval(150,150,450,250);
}
}
public static void main(String args[])
{
int a,b,c,d;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
d=Integer.parseInt(args[3]);
f f1=new f();
f1.setBounds(a,b,c,d);
f1.show();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT :
E:\jdk1.3\bin>java f 150 150 600 600
MOUSE EVENTS
import java.awt.*;
import java.awt.event.*;
class third extends Frame implements MouseListener
{
int m;
third()
{
setLayout(null);
setTitle("MOUSE APPLICATION");
addWindowListener(new win());
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
m=1;
repaint();
}
public void mousePressed(MouseEvent e)
{
m=2;
repaint();
}
public void mouseReleased(MouseEvent e)
{
m=3;
repaint();
}
public void mouseEntered(MouseEvent e)
{
m=4;
repaint();
}
public void mouseExited(MouseEvent e)
{
m=5;
repaint();
}
public void paint(Graphics g)
{
Font f=new Font("Helvetica",Font.BOLD+Font.ITALIC,30);
g.setFont(f);
g.setColor(Color.blue);
switch(m)
{
case 1:
g.drawString("MOUSE CLICKED",120,250);
break;
case 2:
g.drawString("MOUSE PRESSED",110,300);
break;
case 3:
g.drawString("MOUSE RELEASED",150,350);
break;
case 4:
g.drawString("MOUSE ENTERED",100,400);
break;
case 5:
g.drawString("MOUSE EXITED",150,300);
break;
}
}
class win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
public static void main(String args[])
{
third t=new third();
t.setSize(500,500);
t.setVisible(true);
}
}
OUTPUT:
CALCULATOR
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="cal.java" width=150 height=200>
</applet>*/
public class cal extends Applet implements ActionListener
{
String ans,x3;
int x;
double x1,x2,a,b,c,d,z;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,add,sub,mul,div,equal,clr,deci;
TextField box;
String msg="";
public void init()
{
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
equal=new Button("=");
clr=new Button("clr");
deci=new Button(".");
box=new TextField(15);
add(box);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(deci);
add(add);
add(sub);
add(mul);
add(div);
add(equal);
add(clr);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
equal.addActionListener(this);
deci.addActionListener(this);
clr.addActionListener(this);
box.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("clr"))
{
msg="";
box.setText("");
}
else if(s.equals("+"))
{
ans=box.getText();
z=Float.valueOf(ans).floatValue();
msg="";
box.setText(msg);
x=1;
}
else if(s.equals("-"))
{
ans=box.getText();
z=Float.valueOf(ans).floatValue();
msg="";
box.setText(msg);
x=2;
}
else if(s.equals("*"))
{
ans=box.getText();
z=Float.valueOf(ans).floatValue();
msg="";
box.setText(msg);
x=3;
}
else if(s.equals("/"))
{
ans=box.getText();
z=Float.valueOf(ans).floatValue();
msg="";
box.setText(msg);
x=4;
}
else if(s.equals(".'"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("1"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("2"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("3"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("4"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("5"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("6"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("7"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("8"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("9"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("0"))
{
msg+=s;
box.setText(msg);
}
else if(s.equals("="))
{
ans=box.getText();
msg="";
b=Float.valueOf(ans).floatValue();
if(x==1)
{
c=z+b;
}
if(x==2)
{
c=z-b;
}
if(x==3)
{
c=z*b;
}
if(x==4)
{
c=z/b;
}
else
{
msg="type in";
}
box.setText(String.valueOf(c));
}
}
}
OUTPUT:
E:\jdk1.3\bin>javac cal.java
E:\jdk1.3\bin>appletviewer cal.java
STUDENT INFORMATION IN TEXT FILE
import java.io.*;
class stud
{
public static char get()[]throws Exception
{
char inp[]=new char[50];
System.out.println("Enter the student information");
System.out.println("Enter the rollno,name,marks");
for(int i=0;i<30;i++)
{
inp[i]=(char)System.in.read();
}
return inp;
}
public static void main(String args[])throws Exception
{
char input[]=get();
OutputStream f=new FileOutputStream("e:\\stud2.txt");
for(int i=0;i<30;i++)
{
f.write(input[i]);
}
f.close();
System.out.println("The student information on the text file");
}
}
OUTPUT:
E:\jdk1.3\bin>java stud
Enter the student information
Enter the rollno,name,marks
Jenita
19mca05
87
90
88
92
The student information on the text file
ANIMATION
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.image.*;
public class ani extends Applet implements Runnable
{
Image ce[];
final int m=1000;
int sq[];
int nsq,idx,fr;
boolean sf;
private int def(String s,int de)
{
int n=de;
if(s!=null)
try
{
n=Integer.parseInt(s);
}
catch(NumberFormatException e)
{
}
return n;
}
public void init()
{
fr=def(getParameter("framerate"),5);
int tx=def(getParameter("cols"),1);
int ty=def(getParameter("rows"),1);
ce=new Image[tx*ty];
StringTokenizer st=new StringTokenizer(getParameter("Sequence"),",");
sq=new int[m];
nsq=0;
while(st.hasMoreTokens()&&nsq<m)
{
sq[nsq]=def(st.nextToken(),0);
nsq++;
}
try
{
Image img=getImage(getDocumentBase(),getParameter("img"));
MediaTracker t=new MediaTracker(this);
t.addImage(img,0);
t.waitForID(0);
int iw=img.getWidth(null);
int ih=img.getHeight(null);
int tw=iw/tx;
int th=ih/ty;
CropImageFilter f;
FilteredImageSource fis;
for(int y=0;y<ty;y++)
{
for(int x=0;x<tx;x++)
{
f=new CropImageFilter(tw*x,th*y,tw,th);
fis=new FilteredImageSource(img.getSource(),f);
int i=y*tx+x;
ce[i]=createImage(fis);
t.addImage(ce[i],i);
}
}
t.waitForAll();
}
catch(Exception e)
{
}
}
public void update(Graphics g)
{
}
public void paint(Graphics g)
{
g.drawImage(ce[sq[idx]],100,100,null);
}
Thread t;
public void start()
{
t=new Thread (this);
sf=false;
t.start();
}
public void stop()
{
sf=true;
}
public void run()
{
idx=0;
while(true)
{
paint(getGraphics());
idx=(idx+1)%nsq;
try
{
Thread.sleep(6000/fr);
}
catch(Exception e)
{
}
if(sf)
return;
}
}
}
/*<applet code="ani.java" width=500 height=500>
<Param name=framerate value=25>
<Param name=img value=bacas.jpg>
<Param name=rows value=1>
<Param name=cols value=2>
<Param name=sequence value=0,1,2,3,4>
</applet>*/
OUTPUT:
CHATTING WITH CLIENT AND SERVER – SERVER MODULE
import java.io.*;
import java.net.*;
public class chatser
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
ServerSocket ss=new ServerSocket(4000);
Socket s =ss.accept();
InputStream i=s.getInputStream();
DataInputStream dis=new DataInputStream(i);
OutputStream o=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(o);
while(true)
{
String s1=b.readLine();
dos.writeUTF(s1);
if(s1.equals("BYE"))break;
String st1=dis.readUTF().toString();
System.out.println(st1);
if(st1.equals("bye"))break;
}
dis.close();
i.close();
dos.close();
o.close();
}
}
OUTPUT:
E:\jdk1.3\bin>javac chatclie.java
E:\jdk1.3\bin>javac chatser.java
JDBC DABASE ACCESS
import java.sql.*;
class sample
{
static Connection con;
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
open();
select();
insert();
update(true);
close();
}
static void open()throws SQLException
{
String dsn="jdbc:odbc:student";
String user="sudhakar";
String password="imca01";
con=DriverManager.getConnection(dsn,user,password);
con.setAutoCommit(false);
}
static void close()throws SQLException
{
con.commit();
con.close();
}
static void select()throws SQLException
{
Statement stmt;
String query;
ResultSet rs;
boolean more;
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT studid,studname,mark FROM studtable");
more=rs.next();
if(!more)
{
System.out.println("NO Rows Found");
return;
}
while(more)
{
System.out.println("ID :"+rs.getString("studid"));
System.out.println("Name :"+rs.getString("studname"));
System.out.println("Mark :"+rs.getInt("mark"));
System.out.println(" ");
more=rs.next();
}
rs.close();
stmt.close();
}
static void insert()
{
try
{
Statement stmt;
String sql;
int rows;
stmt=con.createStatement();
rows=stmt.executeUpdate("INSERT INTO studtable VALUES('20mc10','RAMESH',89)");
con.commit();
stmt.close();
System.out.println(rows+"row added");
}
catch(SQLException s)
{
System.out.println("Error while inserting");
}
}
static void update(boolean commit)
{
try
{
Statement stmt;
String name;
int rows;
if(commit)
name="Sudhakar";
else
name="Captain";
stmt=con.createStatement();
rows=stmt.executeUpdate("UPDATE studtable SET studname='"+name+"' WHERE
studid='20mc101'");
if(commit)
con.commit();
else
con.rollback();
stmt.close();
System.out.println(rows+"row updateded");
}
catch(SQLException s)
{
System.out.println("Error while updating");
}
}
}
OUTPUT:
E:\jdk1.3\bin>javac sample.java
E:\jdk1.3\bin>java sample
ID :20mc101
Name :Sudha
Mark :90
ID :20mc103
Name :Jenita
Mark :92
ID :20mc102
Name :Arputharaj
Mark :78
ID :20mc107
Name :Joben
Mark :79
1row added
1row updateded
E:\jdk1.3\bin>
REMOTE METHOD INVOCATION
import java.rmi.*;
public interface MethodImpl extends Remote
{
double getSqrt(double db1)throws RemoteException;
}
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class RMIClient1
{
public static void main(String args[])
{
try
{
System.out.println(" Enter the Number to find Sq.root : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int x=Integer.parseInt(br.readLine());
MethodImpl m=(MethodImpl)Naming.lookup("rmi://localhost/sqrt");
double db1=m.getSqrt(x);
System.out.println("sqrt : "+db1);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class RMIServer1 extends UnicastRemoteObject
implements MethodImpl
{
public RMIServer1() throws RemoteException
{
System.out.println("server instantiated");
}
public double getSqrt(double db1)
{
return Math.sqrt( db1);
}
public static void main(String args[])
{
try
{
RMIServer1 server=new RMIServer1();
Naming.rebind("sqrt",server);
}
catch(Exception e)
{
System.out.println("Exception"+e.toString());
}
}
}
OUTPUT:
E:\jdk1.3\bin>javac RMIServer1.java
E:\jdk1.3\bin>javac RMIClient1.java
E:\jdk1.3\bin>javac MethodImpl.java
E:\jdk1.3\bin>rmic RMIServer1
E:\jdk1.3\bin>rmiregistry
E:\jdk1.3\bin>java RMIServer1
server instantiated
E:\jdk1.3\bin>java RMIClient1
Enter the Number to find Sq.root :
81
sqrt : 9.0
E:\jdk1.3\bin>
IMPLEMENTING TREE VIEWER
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/*<applet code=tr.java width=500 height=500>
</applet>*/
public class tr extends JApplet
{
JTree tree;
JTextField jtf;
Container contentpane;
public void init()
{
contentpane=this.getContentPane();
DefaultMutableTreeNode top=new DefaultMutableTreeNode("E://IIMCA");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("IIMCA01");
top.add(a);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("LAB");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("JAVA");
a1.add(a2);
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("VISUAL
PROGRAMMING");
a1.add(a3);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
contentpane.add(jsp,BorderLayout.CENTER);
jtf=new JTextField("",20);
contentpane.add(jtf,BorderLayout.NORTH);
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
jtf.setText("");
doMouseClicked(e);
}
}
);
}
void doMouseClicked(MouseEvent e)
{
TreePath tp=tree.getPathForLocation(e.getX(),e.getY());
if(tp!=null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
}
OUTPUT:
E:\jdk1.3\bin>javac tr.java
E:\jdk1.3\bin>appletviewer tr.java
PROHIBITING BADWORDS
import java.io.*;
import java.util.*;
import java.lang.String.*;
class badword
{
public static void main(String args[])throws IOException
{
Properties ht=new Properties();
ht.put("hate","ha");
ht.put("stupid","st");
ht.put("mad","m");
E:\jdk1.3\bin>