You are on page 1of 29

Draw lines,rect,oval: Save as choice.java import javax.swing.*; import java.awt.Graphics; /* <html> <body> <applet code="choice.

class" width=500 height=500> </applet> </body> </html> */ public class choice extends JApplet { int i,ch; public void init() { String input; input=JOptionPane.showInputDialog ("enter your choice( 1-lines,2-rectangles,3-ovals)"); ch=Integer.parseInt(input); } public void paint(Graphics g) { switch(ch) { case 1:{ for(i=1;i<=10;i++) { g.drawLine(10,10,250,10*i); } break; } case 2:{ for(i=1;i<=10;i++) { g.drawRect(10*i,10*i,50+10*i,50+10*i); } break; } case 3:{ for(i=1;i<=10;i++) { g.drawOval(10*i,10*i,50+10*i,50+10*i); } break; } } }

} -------------------------------------------------------------------------JTable with data enter: Save as JTableDemo.java import java.awt.*; import javax.swing.*; /* <applet code = "JTableDemo" width = 400 height=200> </applet> */ public class JTableDemo extends JApplet { public void init() { //Get Content Pane Container contentPane = getContentPane(); //set layout manger contentPane.setLayout(new BorderLayout()); //Initialize column headings final String[] colHeads = { "Name", "Phone", "Fax" }; //Initialize data final Object[][] data = { { "Gail", "4567", "8675" }, {"Keen", "7566", "5555" }, {"Viviane", "5674", "5887"}, {"Anne", "1237", "3333" }, { "John", "5656", "3144" }, { "Ellen", "1134", "5335"}, { "Gail", "4567", "8675" }, {"Keen", "7566", "5555" }, {"Viviane", "5674", "5887"}, {"Anne", "1237", "3333" }, { "John", "5656", "3144" }, { "Ellen", "1134", "5335"} }; //Create the table JTable table = new JTable(data, colHeads); //Add table to a scroll pane int v = ScrollPaneConstants .VERTICAL_SCROLLBAR_AS_NEEDED ; int h = ScrollPaneConstants. HORIZONTAL_SCROLLBAR_AS_NEEDED ; JScrollPane jsp = new JScrollPane(table, v, h); // Add scroll pane to the content pane contentPane.add(jsp, BorderLayout.CENTER); } } ----------------------------------------------------------------Abstract class named triagle,hexagon: Save as AbstractDemo.java abstract class shape { void numberofsides() {

System.out.println("This is for abstract class"); } } class Trapezoid extends shape { void numberofsides() { System.out.println("It is having 4 sides"); } } class Triangle extends shape { void numberofsides() { System.out.println("it is having 3 sides"); } } class Hexagonal extends shape { void numberofsides() { System.out.println("it is having 6 sides"); } } class AbstractDemo { public static void main(String args[]) { Trapezoid tz=new Trapezoid(); Triangle t=new Triangle(); Hexagonal h=new Hexagonal(); tz.numberofsides(); t.numberofsides(); h.numberofsides(); } } ---------------------------------------------------------------------Fibonacci class Rfib { int fibo(int n) { int a=0,b=1,c=2,i; if(n==1) return(a); else if(n==2) return(b); else { for(i=3;i<=n;i++)

{ c=a+b; a=b; b=c; } return(c); } } } class fib { public static void main(String args[]) { int n,i,a=0,b=1,c; Rfib f=new Rfib(); n=Integer.parseInt(args[0]); for(i=1;i<=n;i++) { System.out.println(f.fibo(i)); } for(i=1;i<n;i++) { if(i==1) System.out.println(a); else if(i==2) System.out.println(b); else { c=a+b; a=b; b=c; System.out.println(c); } } } } ------------------------------------------Infix into postfix: Save as InToPost.java import java.io.*; class stack { char stack1[]=new char[20]; int top; void push(char ch) { top++; stack1[top]=ch; } char pop()

{ char ch; ch=stack1[top]; top--; return ch; } int pre(char ch) { switch(ch) { case '-':return 1; case '+':return 1; case '*':return 2; case '/':return 2; } return 0; } boolean operator(char ch) { if(ch=='/'||ch=='*'||ch=='+'||ch=='-') return true; else return false; } boolean isAlpha(char ch) { if(ch>='a'&&ch<='z'||ch>='0'&&ch=='9') return true; else return false; } void postfix(String str) { char output[]=new char[str.length()]; char ch; int p=0,i; for(i=0;i<str.length();i++) { ch=str.charAt(i); if(ch=='(') { push(ch); } else if(isAlpha(ch)) { output[p++]=ch; } else if(operator(ch)) { if(stack1[top]==0||(pre(ch)>pre

(stack1[top]))||stack1[top]=='(') { push(ch); } } else if(pre(ch)<=pre(stack1[top])) { output[p++]=pop(); push(ch); } else if(ch=='(') { while((ch=pop())!='(') { output[p++]=ch; } } } while(top!=0) { output[p++]=pop(); } for(int j=0;j<str.length();j++) { System.out.print(output[j]); } } } class InToPost { public static void main(String[] args)throws Exception { String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); stack b=new stack(); System.out.println("Enter input string"); s=br.readLine(); System.out.println("Input String:"+s); System.out.println("Output String:"); b.postfix(s); } } -----------------------------------------------------------------------------------------good morning and welcome class A extends Thread { synchronized public void run() { try

{ while(true) { sleep(1000); System.out.println("good morning"); } } catch(Exception e) { } } } class B extends Thread { synchronized public void run() { try { while(true) { sleep(2000); System.out.println("hello"); } } catch(Exception e) { } } } class C extends Thread { synchronized public void run() { try { while(true) { sleep(3000); System.out.println("welcome"); } } catch(Exception e) { } } } class ThreadDemo { public static void main(String args[]) { A t1=new A(); B t2=new B(); C t3=new C();

t1.start(); t2.start(); t3.start(); } } -------------------------------------------------------count words class Count { public static void main(String args[]) { int i,c=0; for(i=0;i<args.length;i++) { System.out.println(args[i]); c++; } System.out.println("number of words="+c); } } ----------------------------------------------------------file exists r not import java.io.*; class FileDemo { public static void main(String args[]) { File f1=new File("/java/copyright","Goutham.java"); System.out.println("file name"+f1.getName()); System.out.println("path"+f1.getPath()); System.out.println("parent"+f1.getParent()); System.out.println(f1.exists()); System.out.println(f1.canRead()); System.out.println(f1.canWrite()); System.out.println(f1.isDirectory()); System.out.println(f1.isFile()); System.out.println(f1.lastModified()); System.out.println(f1.length()); System.out.println(f1.delete()); } } -------------------------------------Calculator: Save as Cal.java import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Cal" width=300 height=300> </applet> */ public class Cal extends Applet

implements ActionListener { String msg=" "; int v1,v2,result; TextField t1; Button b[]=new Button[10]; Button add,sub,mul,div,clear,mod,EQ; char OP; public void init() { Color k=new Color(120,89,90); setBackground(k); t1=new TextField(10); GridLayout gl=new GridLayout(4,5); setLayout(gl); for(int i=0;i<10;i++) { b[i]=new Button(""+i); } add=new Button("add"); sub=new Button("sub"); mul=new Button("mul"); div=new Button("div"); mod=new Button("mod"); clear=new Button("clear"); EQ=new Button("EQ"); t1.addActionListener(this); add(t1); for(int i=0;i<10;i++) { add(b[i]); } add(add); add(sub); add(mul); add(div); add(mod); add(clear); add(EQ); for(int i=0;i<10;i++) { b[i].addActionListener(this); } add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); mod.addActionListener(this); clear.addActionListener(this); EQ.addActionListener(this);

} public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); char ch=str.charAt(0); if ( Character.isDigit(ch)) t1.setText(t1.getText()+str); else if(str.equals("add")) { v1=Integer.parseInt(t1.getText()); OP='+'; t1.setText(""); } else if(str.equals("sub")) { v1=Integer.parseInt(t1.getText()); OP='-'; t1.setText(""); } else if(str.equals("mul")) { v1=Integer.parseInt(t1.getText()); OP='*'; t1.setText(""); } else if(str.equals("div")) { v1=Integer.parseInt(t1.getText()); OP='/'; t1.setText(""); } else if(str.equals("mod")) { v1=Integer.parseInt(t1.getText()); OP='%'; t1.setText(""); } if(str.equals("EQ")) { v2=Integer.parseInt(t1.getText()); if(OP=='+') result=v1+v2; else if(OP=='-') result=v1-v2; else if(OP=='*') result=v1*v2; else if(OP=='/') result=v1/v2; else if(OP=='%')

result=v1%v2; t1.setText(""+result); } if(str.equals("clear")) { t1.setText(""); } } } -------------------------------------------------------sum of integer import java.io.*; import java.util.*; class Sum { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a[]=new int[10]; int i,sum=0; System.out.println("enter integer:\n"); for(i=0;i<10;i++) { a[i]=Integer.parseInt(br.readLine()); } for(i=0;i<10;i++) { System.out.println("integers are:"+a[i]); } for(i=0;i<10;i++) { sum+=a[i]; } System.out.println("sum is:\n"+sum); } } import java.io.*; import java.util.*; class Sum { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a[]=new int[10]; int i,sum=0; System.out.println("enter integer:\n"); for(i=0;i<10;i++) {

a[i]=Integer.parseInt(br.readLine()); } for(i=0;i<10;i++) { System.out.println("integers are:"+a[i]); } for(i=0;i<10;i++) { sum+=a[i]; } System.out.println("sum is:\n"+sum); } } ---------------------------------------------------------applet message import java.awt.*; import java.applet.*; /*<applet code="Hellojava" width=300 height=50> </applet>*/ public class Hellojava extends Applet { public void paint(Graphics g) { g.drawString("welcome to Applet",20,20); } } ----------------------------------Divide: Save as Div.java import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="Div"width=230 height=250> </applet>*/ public class Div extends Applet implements ActionListener { String msg; TextField num1,num2,res;Label l1,l2,l3; Button div; public void init() { l1=new Label("Number 1"); l2=new Label("Number 2"); l3=new Label("result"); num1=new TextField(10); num2=new TextField(10); res=new TextField(10); div=new Button("DIV"); div.addActionListener(this); add(l1);

add(num1); add(l2); add(num2); add(l3); add(res); add(div); } public void actionPerformed(ActionEvent ae) { String arg=ae.getActionCommand(); if(arg.equals("DIV")) { String s1=num1.getText(); String s2=num2.getText(); int num1=Integer.parseInt(s1); int num2=Integer.parseInt(s2); if(num2==0) { try { System.out.println(" "); } catch(Exception e) { System.out.println("ArithematicException"+e); } msg="Arithemetic"; repaint(); } else if((num1<0)||(num2<0)) { try { System.out.println(""); } catch(Exception e) { System.out.println("NumberFormat"+e); } msg="NumberFormat"; repaint(); } else { int num3=num1/num2; res.setText(String.valueOf(num3)); } } } public void paint(Graphics g)

{ g.drawString(msg,30,70); } } --------------------------------------------------server.java: import java.net.*; import java.io.*; public class server { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(2000); Socket s=ss.accept(); BufferedReader br=new BufferedReader (new InputStreamReader(s.getInputStream())); double rad,area; String result; rad=Double.parseDouble(br.readLine()); System.out.println("From Client : "+rad); area=Math.PI*rad*rad; result="Area is "+area; PrintStream ps=new PrintStream(s.getOutputStream()); ps.println(result); br.close(); ps.close(); s.close(); ss.close(); } } Client.java import java.net.*; import java.io.*; public class client { public static void main(String args[]) throws Exception { Socket s=new Socket("localhost",2000); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String rad; System.out.println("Enter radius of the circle "); rad=br.readLine(); PrintStream ps=new PrintStream(s.getOutputStream()); ps.println(rad); BufferedReader fs=new BufferedReader(new InputStreamReader (s.getInputStream())); String result=fs.readLine(); System.out.println("From Server : "+result);

br.close(); fs.close(); ps.close(); s.close(); } } ------------------------------------------------------------palindraom class Palindrom { public static void main(String args[]) { String s=args[0]; int len,i=0,n,c=0,p; len=s.length(); n=len/2; p=len-n+1; while(i<len/2) { if(s.charAt [i]==s.charAt(p)) c++; i++; p--; } if(c==len/2) { System.out.println("palindrom"); } else { System.out.println("not palindrom"); } } } --------------------------------------------------------------sorting class Sort { static String s[]={"goutham","ravi","hari"}; public static void main(String args[]) { int i,j; int n=s.length; String t=null; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(s[j].compareTo(s[i])<0) {

t=s[i]; s[i]=s[j]; s[j]=t; } } System.out.println(s[i]); } } } ---------------------------------------------------------------------Signal: Save as Signal.java import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="Signal" width=300 height=100> </applet>*/ public class Signal extends Applet implements ItemListener { Checkbox red,yellow,green; Light light; public void init() { Panel p1=new Panel(); p1.setSize(200,200); p1.setLayout(new FlowLayout()); p1.add(light=new Light()); light.setSize(40,90); Panel p2=new Panel(); p2.setLayout(new FlowLayout()); CheckboxGroup g=new CheckboxGroup(); p2.add(red=new Checkbox("red",g,false)); p2.add(yellow=new Checkbox("yellow",g,false)); p2.add(green=new Checkbox("green",g,false)); add("center",p1); add("south",p2); red.addItemListener(this); yellow.addItemListener(this); green.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { if(red.getState())

{ light.red(); } if(yellow.getState()) { light.yellow(); } if(green.getState()) { light.green(); } } class Light extends Canvas { private boolean red; private boolean yellow; private boolean green; public Light() { red=false; yellow=false; green=false; } public void red() { red=true; yellow=false; green=false; repaint(); } public void yellow() { red=false; yellow=true; green=false; repaint(); } public void green() { red=false; yellow=false; green=true; repaint(); } public void paint(Graphics g) { if(red) { g.setColor(Color.red); g.fillOval(10,10,20,20); g.setColor(Color.black); g.drawOval(10,35,20,20);

g.drawOval(10,60,20,20); g.drawRect(5,5,30,86); } else if(yellow) { g.setColor(Color.yellow); g.fillOval(10,35,20,20); g.setColor(Color.black); g.drawRect(5,5,30,80); g.drawOval(10,10,20,20); g.drawOval(10,60,20,20); } else if(green) { g.setColor(Color.green); g.fillOval(10,60,20,20); g.setColor(Color.black); g.drawRect(5,5,30,80); g.drawOval(10,10,20,20); g.drawOval(10,35,20,20); } else{ g.setColor(Color.black); g.drawRect(5,5,30,80); g.drawOval(10,10,20,20); g.drawOval(10,35,20,20); g.drawOval(10,60,20,20); } } } } -------------------------------------------------fact using applet /* <applet code="Fact.java" width=300 height=100> </applet> */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Fact extends Applet implements ActionListener { int n,f=1; Button compute; TextField t1; String s1,s2; public void init() { t1=new TextField(); compute = new Button("compute"); add(t1); add(compute);

t1.setText("0"); compute.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s=ae.getActionCommand(); if(s.equals("compute")) { s1=t1.getText(); n=Integer.parseInt(s1); while(n!=0) { f*=n; n--; } s2=String.valueOf(f); } repaint(); } public void paint(Graphics g) { g.drawString("factorial:"+s2,6,50); } } ----------------------------------------------matrix mul import java.io.*; class Mmul { public static void main (String args[])throws Exception { BufferedReader Br=new BufferedReader(new InputStreamReader(System.in)); int i=0,j=0,k=0; int a[][]=new int[10][10]; int b[][]=new int[10][10]; int c[][]=new int[10][10]; System.out.println("enter A matrix"); for(i=0;i<2;i++) for(j=0;j<2;j++) a[i][j]=Integer.parseInt(Br.readLine()); System.out.println("enter B matrix"); for(i=0;i<2;i++) for(j=0;j<2;j++) b[i][j]=Integer.parseInt(Br.readLine()); System.out.println("c matrix"); for(i=0;i<2;i++) for(j=0;j<2;j++) { c[i][j]=0;

for(k=0;k<2;k++) c[i][j]+=a[i][k]*b[k][j]; System.out.println(" "+c[i][j]); } System.out.println(" "); } } ---------------------------------Evaluate postfix: Save as EvalPostFix import java.io.*; import java.util.*; class StackDemo { static int index,pos; int T; float stk[]; StackDemo() { stk=new float[10]; T=-1; index=0; pos=0; } void push(float s) { if(T>=19) { System.out.println("Stack overflow"); System.exit(0); }else{ T=T+1; stk[T]=s; } } float pop() { float num; if(T==-1) { System.out.println("Stack is empty"); return(0); } else { num=stk[T]; T--; } return(num); }

float ExpEval(char sfix[],float data[]) { int j=0; float op1,op2,fs; char ch; while(sfix[j]!='\0') { ch=sfix[j]; if(sfix[j]>='a'||sfix[j]>= 'A'&&sfix[j]<='z'||sfix[j]<='Z') { push(data[j]); } else { op2=pop(); op1=pop(); switch(ch) { case '+':push(op1+op2); break; case '-':push(op1-op2); break; case '*':push(op1*op2); break; case '/':push(op1/op2); break; case '%':push(op1%op2); break; } } j++; } fs=pop(); return(fs); } } class EvalPostFix { public static void main(String args[]) { String str; char postfix[]=new char[25]; float number[]=new float[25]; int j=0; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a postfix expression:");

str=br.readLine(); str.getChars(0,str.length(),postfix,0); while(postfix[j]!='\0') { if(postfix[j]>='a'||postfix[j] >='A'&&postfix[j]<='z'||postfix[j]<='Z') { System.out.println("enter a number for%c:"+postfix[j]); number[j]=Integer.parseInt(br.readLine()); } j++; } } catch(Exception e) { System.out.println("Exception \n Read:"+e); } StackDemo s1=new StackDemo(); System.out.println("The result is "+s1.ExpEval(postfix,number)); } } -------------------------------------------------------------------------------quadratic class Quad { public static void main(String args[]) { int a,b,c,d; double x1,x2,re,im; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); c=Integer.parseInt(args[2]); if(a==0) { System.out.println("linear equation"); x1=-c/b; System.out.println("x1="+x1); } else { d=b*b-(4*a*c); if(d==0) { System.out.println("real and equal"); x1=-(b/(2*a)); System.out.println("x1=x2="+x1); } else

if(d>0) { System.out.println("real and unequal"); x1=(-b+Math.sqrt(d))/(2*a); x2=(-b-Math.sqrt(d))/(2*a); } else { System.out.println("root are not real solutions"); re=(-b)/(2*a); im=Math.sqrt(-d)/(2*a); System.out.println("real="+re); System.out.println("imaginary="+im); } } } } --------------------------------------------------prime range class Primeran { public static void main(String args[]) { int num,i=1,j=1,c=0; num=Integer.parseInt(args[0]); while(i<=num) { j=1; c=0; while(j<=i) { if(i%j==0) c++; j++; } if(c==2) System.out.println(i+"prime"); i++; } } } ---------------------------------------------------------mouse event import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="MouseEventDemo " width=300 height=100> </applet> */

public class MouseEventDemo extends Applet implements MouseListener, MouseMotionListener { String msg=" "; int mouseX=0,mouseY=0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { mouseX=0; mouseY=10; msg="mouse clicked by me"; repaint(); } public void mouseEntered(MouseEvent me) { mouseX=0; mouseY=10; msg="mouse entered by me"; repaint(); } public void mouseExited(MouseEvent me) { mouseX=0; mouseY=10; msg="mouse exited by me"; repaint(); } public void mousePressed(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Down"; showStatus(msg+"at"+mouseX+","+mouseY); repaint(); } public void mouseReleased(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Up"; showStatus(msg+"at"+mouseX+" "+mouseY); repaint(); } public void mouseDragged(MouseEvent me)

{ mouseX=me.getX(); mouseY=me.getY(); msg="*"; showStatus("Dragging the mouse at"+mouseX+","+mouseY); repaint(); } public void mouseMoved(MouseEvent me) { msg="mouse moved"; showStatus("mouse moved at"+me.getX()+","+me.getY()); repaint(); } public void paint(Graphics g) { g.drawString(msg,mouseX,mouseY); } } -------------------------------------------------------------------------------consumer class Q { boolean valueSet=false; int n; synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("Exception is:"+e); } System.out.println("got:"+n); notify(); return n; } synchronized void put(int n) { if(valueSet) try { wait(); } catch(InterruptedException e)

{ System.out.println ("\n Exception in put:"+e); } this.n=n; valueSet=true; System.out.println("\nput:"+n); notify(); } } class Producer implements Runnable { Q q; Producer(Q q) { this.q=q; new Thread(this,"Producer").start(); } public void run() { int i=0; while(true) q.put(i++); } } class Consumer implements Runnable { Q q; Consumer(Q q) { this.q=q; new Thread(this,"Consumer").start(); } public void run() { while(true) q.get(); } } class ProdConsDemo { public static void main(String args[]) { Q q=new Q(); new Producer(q); new Consumer(q); System.out.println("\n press ctrl+c to stop"); } } ------------------------------------------------------------

num of char and lines import java.io.*; class FileDemo2 { public static void main(String args[])throws Exception { int i,l=0,w=0; String s="it is java program"; char buffer[]=new char[s.length()]; s.getChars(0,s.length(),buffer,0); System.out.println("no of characters="+s.length()); for(i=0;i<s.length();i++) { char c=s.charAt(i); if(c=='\t'||c=='\b'||c==' '||c=='\n') { w++; } } System.out.println("no of words:"+w); FileWriter f1=new FileWriter("c:/goutham.txt"); f1.write(buffer); f1.close(); FileReader fr=new FileReader("c:/goutham.txt"); BufferedReader br=new BufferedReader(fr); String t; while((t=br.readLine())!=null) { l++; } System.out.println("no of lines"+l); fr.close(); } } -------------------------------------------------------------------------line num b4re import java.io.*; class FileDemo1 { public static void main(String args[])throws Exception { int c=0; String s="i+ \n is \n java \n program \n"; char buffer[]=new char[s.length()]; s.getChars(0,s.length(),buffer,0); FileWriter f1=new FileWriter("c:/index.txt"); f1.write(buffer); f1.close(); FileReader fr=new FileReader("c:/index.txt"); BufferedReader br=new BufferedReader(fr); String t; while((t=br.readLine())!=null)

{ c++; System.out.println(c+t); } fr.close(); } } -----------------------------------------------------------------------stack adt interface IntStack { void push(int item); int pop(); } class FixedStack implements IntStack { private int st[ ]; private int top; FixedStack(int s) { st=new int[s]; top=-1; } public void push(int item) { if(top==st.length-1) System.out.println("stack is full"); else st[++top]=item; } public int pop() { if(st.length<0) { System.out.println("stack is empty"); return 0; } else return st[top--]; } } class IFTest { public static void main(String args[ ]) { FixedStack s1=new FixedStack(5); FixedStack s2=new FixedStack(8); int i; for(i=0;i<5;i++) s1.push(i); for(i=0;i<8;i++) s2.push(i); System.out.println("stack in s1"); for(i=0;i<5;i++) System.out.println(s1.pop()); System.out.println("stack in s2");

for(i=0;i<8;i++) System.out.println(s2.pop()); } }

You might also like