You are on page 1of 56

1. Develop a Java package with simple Stack and Queue classes.

Use JavaDoc

comments for documentation


Queue package: package queuepackage; public class queue2 { private int maxsize; private long[] queArray; private int front; private int rear; private int nitems; public queue2(int s) { maxsize=s; queArray=new long[maxsize]; front=0; rear=-1; nitems=0; } public void insert(long j) { if(rear==maxsize-1) rear=-1; queArray[++rear]=j; nitems++; } public long remove() { long temp=queArray[front++]; if(front==maxsize) front=0; nitems--; return temp; } public long peekFront() { return queArray[front]; } public boolean isEmpty() { return(nitems==0); } public boolean isFull() { return(nitems==maxsize);

} public int size() { return nitems; } } Stack package: package stackpackage; public class stack2 { int []a; int top; public stack2(int n) { a=new int[n]; top=-1; } public void push(int val) { if(top==a.length-1) { System.out.println("stack overflow"); } else { top++; a[top]=val; } } public void pop() { if(top==-1) { System.out.println("stack underflow"); } else { System.out.println("element popped"+a[top]); top--; } } public void display() { if(top==-1)

{ System.out.println("stack empty"); } else { for(int i=top;i>=0;i--) { System.out.println("sstack element :"+a[i]); } } } }

Main program: import queuepackage.queue2; import stackpackage.stack2; import java.io.*; public class usestackqueue2 { public static void main(String args[]) { BufferedReader sc=new BufferedReader(new InputStreamReader(System.in)); int c; stack2 s; int n; try { do { System.out.println("1.stack 2.queue"); c=Integer.parseInt(sc.readLine()); switch(c) { case 1: System.out.println("enter the size of stack"); n=Integer.parseInt(sc.readLine()); s=new stack2(n); int choice; do { System.out.println("1.push,2.pop,3.display,0.exit,enter your choice:"); choice=Integer.parseInt(sc.readLine()); switch(choice) {

case 1: int value; System.out.println("enter the element to push:"); value=Integer.parseInt(sc.readLine()); s.push(value); break; case 2: s.pop(); break; case 3: s.display(); break; case 0: break; default:System.out.println("invalid choice"); } }while(choice!=0); break; case 2: queue2 thequeue = new queue2(5); thequeue.insert(10); thequeue.insert(20); thequeue.insert(30); thequeue.insert(40); thequeue.remove(); thequeue.remove(); thequeue.remove(); thequeue.insert(50); thequeue.insert(60); thequeue.insert(70); thequeue.insert(80); while(!thequeue.isEmpty()) { long n1= thequeue.remove(); System.out.print(n1); System.out.print(""); } System.out.println(""); break; } }while(c!=0); } catch(Exception e) {} } }

Output:

C:\j2sdk1.4.0\bin>javac usestackqueue2.java C:\j2sdk1.4.0\bin>java usestackqueue2 1.stack 2.queue 1 enter the size of stack 5 1.push,2.pop,3.display,0.exit,enter your choice: 1 enter the element to push: 1 1.push,2.pop,3.display,0.exit,enter your choice: 1 enter the element to push: 2 1.push,2.pop,3.display,0.exit,enter your choice: 1 enter the element to push: 3 1.push,2.pop,3.display,0.exit,enter your choice: 3 sstack element :3 sstack element :2 sstack element :1 1.push,2.pop,3.display,0.exit,enter your choice: 2 element popped3 1.push,2.pop,3.display,0.exit,enter your choice: 3 sstack element :2 sstack element :1 1.push,2.pop,3.display,0.exit,enter your choice: 0 1.stack 2.queue 2 4050607080 1.stack 2.queue 0

Design a class for Complex numbers in Java. In addition to methods for basic operations on complex numbers, provide a method to return the number of active objects created
complex number program in java,java complex number program. Program: import java.io.*; class complex { int a,b; public static int c; public complex(int x,int y) { a=x;b=y; c++; } public static String add(complex n1,complex n2) { int a1=n1.a+n2.a; int b1=n1.b+n2.b; if(b1<0) return (a1+" "+b1+"i"); else return (a1+" "+b1+"i"); } public static String sub(complex n1,complex n2) { int a1=n1.a-n2.a; int b1=n1.b-n2.b; if(b1<0) return (a1+" "+b1+"i"); else return (a1+" "+b1+"i"); } public static String mul(complex n1,complex n2) { int a1=n1.a*n2.a; int b1=n1.b*n2.b; int v1=n1.a*n2.b; int v2=n2.a*n1.b; int vi=v1+v2; if(vi<0) return(a1-b1+" "+vi+"i"); else

return(a1-b1+"+"+vi+"i"); } } class com { public static void main(String a[])throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int x,y; System.out.println("enter the no for complex1:"); x=Integer.parseInt(in.readLine()); y=Integer.parseInt(in.readLine()); System.out.println("enter the no for complex2:"); int m=Integer.parseInt(in.readLine()); int n=Integer.parseInt(in.readLine()); complex c1=new complex(x,y); complex c2=new complex(m,n); System.out.println("addition:"+complex.add(c1,c2)); System.out.println("subtraction:"+complex.sub(c1,c2)); System.out.println("multiplication:"+complex.mul(c1,c2)); System.out.println("count="+complex.c); } }

Output:

C:\j2sdk1.4.0\bin>javac com.java C:\j2sdk1.4.0\bin>java com enter the no for complex1: 21 12 enter the no for complex2: 34 45 addition:55 57i subtraction:-13 -33i multiplication:174+1353i count=2

Design a Date class similar to the one provided in the java.util package in java
user defined date package in java,java date package,user defined date package. Program: package date; public class day { int day,month,year,y,m,tot_day=0; int day_mon[]={31,28,31,30,31,30,31,31,30,31,30,31}; String week[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}; public void days(int a,int b,int c) { valid(a,b,c); day=a; month=b;year=c; for(y=0;y<year;y++)< div=""> if(y%4==0) tot_day+=366; else tot_day+=365; if(month>2 && year%4==0) day_mon[1]=29; for(m=0;m<(month-1);m++) tot_day+=day_mon[m]; tot_day+=day; System.out.println(day+"-"+month+"-"+year+" this is :"+"="+week[(tot_day+4)%7]); } public void valid(int dd,int mm,int yy) { if(yy<1) { System.out.println("invalid...."); System.exit(0); } switch(mm) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(dd>31 || dd<1) {

System.out.println("invalid......"); System.exit(0); } break; case 4: case 6: case 9: case 11: if(dd>30 || dd<1) { System.out.println("invalid......"); System.exit(0); } break; case 2: if(yy%4!=0 && dd>=29 && dd<1) { System.out.println("invalid......"); System.exit(0); } else if(yy%4==0 || dd>28 || dd<1) { System.out.println("invalid......"); System.exit(0); }break; default: System.out.println("invalid......"); System.exit(0); } } } Main program: import date.*; import java.io.*; class chkd { public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream(System.in); day d=new day(); System.out.println("enter the date:(dd mm yyyy):"); int dd=Integer.parseInt(in.readLine()); int mm=Integer.parseInt(in.readLine()); int yy=Integer.parseInt(in.readLine());

d.days(dd,mm,yy); } } Output:

C:\j2sdk1.4.0\bin>javac chkd.java Note: chkd.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\j2sdk1.4.0\bin>java chkd enter the date:(dd mm yyyy): 30 04 1989 30-4-1989 this is :=sunday C:\j2sdk1.4.0\bin>java chkd enter the date:(dd mm yyyy): 30 02 2000 invalid......

Design a Java interface for ADT Stack. Develop two different classes that implement this interface, one using array and the other using linked-list. Provide necessary exception handling in both the implementations.
Program: import java.io.*; interface stackoperation { public void push(int i); public void pop(); } class Astack implements stackoperation {

int stack[]; int top; Astack() { stack=new int[10]; top=0; } public void push(int item) { if(stack[top]==10) System.out.println("overflow"); else { stack[++top]=item; System.out.println("item pushed"); } } public void pop() { if(stack[top]<=0) System.out.println("underflow"); else { stack[top]=top--; System.out.println("item popped");

} } public void display() { for(int i=1;i<=top;i++) System.out.println("element:"+stack[i]); } } class liststack implements stackoperation { node top,q; int count; public void push(int i) { node n=new node(i); n.link=top; top=n; count++; } public void pop() {

if(top==null) System.out.println("under flow"); else

{ int p=top.data; top=top.link; count--; System.out.println("popped element:"+p); } } void display() { for(q=top;q!=null;q=q.link) { System.out.println("the elements are:"+q.data); } } class node { int data; node link; node(int i) { data=i; link=null; } } }

class sample { public static void main(String args[])throws IOException { int ch,x=1,p=0,t=0; DataInputStream in=new DataInputStream(System.in); do { try { System.out.println("----------------------------------"); System.out.println("1.Arraystack 2.liststack 3.exit"); System.out.println("-----------------------------------"); System.out.println("enter ur choice:"); int c=Integer.parseInt(in.readLine()); Astack s=new Astack(); switch(c) { case 1: do { if(p==1) break; System.out.println("ARRAY STACK"); System.out.println("1.push 2.pop 3.display 4.exit");

System.out.println("enter ur choice:"); ch=Integer.parseInt(in.readLine()); switch(ch) { case 1:System.out.println("enter the value to push:"); int i=Integer.parseInt(in.readLine()); s.push(i); break; case 2: s.pop(); break; case 3: System.out.println("the elements are:"); s.display(); break; case 4: p=1; continue; } }while(x!=0); break; case 2: liststack l=new liststack(); do {

if(t==1) break; System.out.println("LIST STACK:"); System.out.println("1.push 2.pop 3.display 4.exit"); System.out.println("enter your choice:"); ch=Integer.parseInt(in.readLine()); switch(ch) { case 1: System.out.println("enter the value for push:"); int a=Integer.parseInt(in.readLine()); l.push(a); break; case 2: l.pop(); break; case 3: l.display(); break;

case 4: t=1; continue; } }

while(x!=0); break; case 3: System.exit(0); } } catch(IOException e) { System.out.println("io error"); } } while(x!=0); } }

Output:

C:\j2sdk1.4.0\bin>javac sample.java Note: sample.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details.

C:\j2sdk1.4.0\bin>java sample ---------------------------------1.Arraystack 2.liststack 3.exit ----------------------------------enter ur choice: 1 ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 1 enter the value to push: 1 item pushed ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 1 enter the value to push: 2

item pushed ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 1 enter the value to push: 3 item pushed ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 3 the elements are: element:1 element:2 element:3 ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 2 item popped ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 3

the elements are: element:1 element:2 ARRAY STACK 1.push 2.pop 3.display 4.exit enter ur choice: 4 ---------------------------------1.Arraystack 2.liststack 3.exit ----------------------------------enter ur choice: 2 LIST STACK: 1.push 2.pop 3.display 4.exit enter your choice: 1 enter the value for push: 1 LIST STACK: 1.push 2.pop 3.display 4.exit enter your choice: 1 enter the value for push: 2 LIST STACK:

1.push 2.pop 3.display 4.exit enter your choice: 1 enter the value for push: 3 LIST STACK: 1.push 2.pop 3.display 4.exit enter your choice: 3 the elements are:3 the elements are:2 the elements are:1 LIST STACK: 1.push 2.pop 3.display 4.exit enter your choice: 2 popped element:3 LIST STACK: 1.push 2.pop 3.display 4.exit enter your choice: 3 the elements are:2 the elements are:1 LIST STACK: 1.push 2.pop 3.display 4.exit

enter your choice: 4 ---------------------------------1.Arraystack 2.liststack 3.exit ----------------------------------enter ur choice: 3

Write a Java program to read a file that contains DNA sequences of arbitrary length one per line (note that each DNA sequence is just a String). Your program should sort the sequences in descending order with respect to the number of 'TATA' subsequences present. Finally write the sequences in sorted order into another file
PROGRAM import java.io.*; class files { String s[]=new String[100]; String str="",m,t; int x,size=0,i,tem,j=0,chg; int a[]=new int[100]; int b[]=new int[100]; int c[]=new int[100]; files(String s1,String s2)throws IOException { File r=new File(s1); File w=new File(s2); FileInputStream fin=new FileInputStream(r); FileOutputStream fout=new FileOutputStream(w); do{ i=fin.read(); if(i!=-1) { str=str+(char)i; if((char)i=='\n') { s[j]=str; j++; size++;

str=""; } } } while(i!=-1); for(int l=0;l<size;l++)< div=""> { if(s[l]==null) break; m=s[l]; t="tata"; b[l]=a[l]=search(m,t); } for(int p=0;p<size;p++)< div=""> { for(int q=0;q<size;q++)< div=""> { if(b[q]<b[q+1])< div=""> { tem=b[q]; b[q]=b[q+1]; b[q+1]=tem; } } } for(int h=0;h<size;h++)< div=""> { x=b[h]; c[h]=linear(x,a,size); } for(int y=0;y<size;y++)< div=""> { chg=c[y]; byte buf[]=s[chg].getBytes(); fout.write(buf); } } static int linear(int x,int a[],int size) { int m,d,item=x,siz=size; for(d=0;d<=siz;d++) { if(item==a[d]) { m=d; break;

} } return d; } static int search(String m,String c) { int ch,ma,inc=0; String pat,main,check; pat=main=m; check=c; do{ pat=main.substring(0,4); if(pat.equalsIgnoreCase(check)) ++inc; main=main.substring(1); ch=check.length(); ma=main.length(); } while(ch<ma);< div=""> return inc; } } class filehand { public static void main(String a[])throws IOException { DataInputStream in=new DataInputStream(System.in); String s1,s2; System.out.println("enter the read file name :"); s1=in.readLine(); System.out.println("enter the write file name:"); s2=in.readLine(); files f=new files(s1,s2); System.out.println("successfully writed"); } } OUTPUT: C:\j2sdk1.4.0\bin>javac filehand.java C:\j2sdk1.4.0\bin>java filehand enter the read file name : input.txt enter the write file name: output.txt

successfully writed

Input.txt jfjksdf tata fji fjksdfi tatt fg tata taata tata jfijsdf tata tjkltj tatta tata tata

Output.txt jfijsdf tata tjkltj tatta tata tata fjksdfi tatt fg tata taata tata jfjksdf tata fji

Develop a simple paint-like program that can draw basic graphical primitives in different dimensions and colors. Use appropriate menu and buttons
import java.awt.*; import java.applet.*; import java.awt.event.*; public class paint extends Frame implements ActionListener { int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,n=5,ch=1; int xpoints[]=new int[n]; int ypoints[]=new int[n]; MenuBar mb; Menu op,col; MenuItem l,c,r,a,po,x,br,bb,bg,fr,fb,fg; Panel p; TextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10; Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10; Button b; paint() { p=new Panel(); t1=new TextField(4); t2=new TextField(4); t3=new TextField(4); t4=new TextField(4); l1=new Label("x1"); l2=new Label("y1");

l3=new Label("x2"); l4=new Label("y2"); t5=new TextField(4); t6=new TextField(4); l5=new Label("height"); l6=new Label("width"); t7=new TextField(4); t8=new TextField(4); t9=new TextField(4); t10=new TextField(4); l7=new Label("yaxis"); l8=new Label("yaxis"); l9=new Label("yaxis"); l10=new Label("yaxis"); b=new Button("ok"); p.add(l1); p.add(t1); p.add(l2); p.add(t2); p.add(l3); p.add(t3); p.add(l4); p.add(t4); p.add(l5); p.add(t5); p.add(l6); p.add(t6); p.add(l7); p.add(t7); p.add(l8); p.add(t8); p.add(l9); p.add(t9); p.add(l10); p.add(t10); p.add(b); b.addActionListener(this); setLayout(new BorderLayout()); add("South",p); mb=new MenuBar(); setMenuBar(mb); op=new Menu("option"); op.add(l=new MenuItem("Line")); op.add(c=new MenuItem("Circle")); op.add(r=new MenuItem("Rectangle")); op.add(a=new MenuItem("Arc"));

op.add(po=new MenuItem("Polygon")); op.add(x=new MenuItem("exit")); col=new Menu("Color"); Menu back=new Menu("Background"); back.add(br=new MenuItem("Red")); back.add(bg=new MenuItem("Green")); back.add(bb=new MenuItem("Blue")); col.add(back); Menu fore=new Menu("Foreground"); fore.add(fr=new MenuItem("Red")); fore.add(fg=new MenuItem("Green")); fore.add(fb=new MenuItem("Blue")); col.add(fore); mb.add(op); mb.add(col); br.addActionListener(this); bg.addActionListener(this); bb.addActionListener(this); fr.addActionListener(this); fg.addActionListener(this); fb.addActionListener(this); l.addActionListener(this); c.addActionListener(this); r.addActionListener(this); a.addActionListener(this); po.addActionListener(this); x.addActionListener(this); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void paint(Graphics g) { switch(ch) { case 1: g.drawLine(x1,y1,x2,y2); break; case 2: g.drawOval(x1,y1,x2,y2); break; case 3: g.drawRect(x1,y1,x2,y2); break; case 4: g.drawArc(x1,y1,x2,y2,x3,y3);

break; case 5: xpoints[0]=x1; xpoints[1]=x2; xpoints[2]=x3; xpoints[3]=x4; xpoints[4]=x5; ypoints[0]=y1; ypoints[1]=y2; ypoints[2]=y3; ypoints[3]=y4; ypoints[4]=y5; g.drawPolygon(xpoints,ypoints,n); break; } } public void actionPerformed(ActionEvent e) { String s=e.getActionCommand(); Object s1=e.getSource(); if(s1==br) setBackground(Color.red); else if(s1==bg) setBackground(Color.green); else if(s1==bb) setBackground(Color.blue); else if(s1==fr) setForeground(Color.red); else if(s1==fg) setForeground(Color.green); else if(s1==fb) setForeground(Color.blue); if(s.equals("exit")) System.exit(0); else if(s.equals("Line")) { ch=1; } else if(s.equals("Circle")) ch=2; else if(s.equals("Rectangle")) ch=3; else if(s.equals("Arc")) { ch=4;

} else if(s.equals("Polygon")) { ch=5; } else if(s.equals("ok")) { x1=Integer.parseInt(t1.getText()); x2=Integer.parseInt(t2.getText()); y1=Integer.parseInt(t3.getText()); y2=Integer.parseInt(t4.getText()); x3=Integer.parseInt(t5.getText()+0); y3=Integer.parseInt(t6.getText()+0); x4=Integer.parseInt(t7.getText()+0); y4=Integer.parseInt(t8.getText()+0); x5=Integer.parseInt(t9.getText()+0); y5=Integer.parseInt(t10.getText()+0); repaint(); } } public static void main(String args[]) { paint f=new paint(); f.setSize(800,400); f.setVisible(true); } }

Output: C:\j2sdk1.4.0\bin>javac paint.java C:\j2sdk1.4.0\bin>java paint

Develop a scientific calculator using even-driven programming paradigm of Java


scientific calculator program in java,java scientific calculator program,java calculator program. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;

public class scientific extends JFrame implements ActionListener { JTextField jtx; double temp,temp1,result,a; static double m1,m2; int k=1,x=0,y=0,z=0; char ch; JButton one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3; JButton plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan; Container cont; JPanel textPanel,buttonpanel; scientific() { cont=getContentPane(); cont.setLayout(new BorderLayout()); JPanel textpanel=new JPanel(); Font font=new Font("Arial",Font.PLAIN,18); jtx=new JTextField(25); jtx.setFont(font); jtx.setHorizontalAlignment(SwingConstants.RIGHT); jtx.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent keyevent) { char c=keyevent.getKeyChar(); if(c>='0' && c<='9') { } else { keyevent.consume(); } } }); textpanel.add(jtx); buttonpanel=new JPanel(); buttonpanel.setLayout(new GridLayout(5,6)); boolean t=true; sin=new JButton("SIN"); buttonpanel.add(sin); sin.addActionListener(this); mr=new JButton("MR");

buttonpanel.add(mr); mr.addActionListener(this); seven=new JButton("7"); buttonpanel.add(seven); seven.addActionListener(this); eight=new JButton("8"); buttonpanel.add(eight); eight.addActionListener(this); nine=new JButton("9"); buttonpanel.add(nine); nine.addActionListener(this); clr=new JButton("AC"); buttonpanel.add(clr); clr.addActionListener(this); cos=new JButton("COS"); buttonpanel.add(cos); cos.addActionListener(this); mc=new JButton("MC"); buttonpanel.add(mc); mc.addActionListener(this); four=new JButton("4"); buttonpanel.add(four); four.addActionListener(this); five=new JButton("5"); buttonpanel.add(five); five.addActionListener(this); six=new JButton("6"); buttonpanel.add(six); six.addActionListener(this); mul=new JButton("*"); buttonpanel.add(mul); mul.addActionListener(this); tan=new JButton("TAN"); buttonpanel.add(tan); tan.addActionListener(this); mp=new JButton("M+"); buttonpanel.add(mp); mp.addActionListener(this); one=new JButton("1"); buttonpanel.add(one); one.addActionListener(this); two=new JButton("2"); buttonpanel.add(two); two.addActionListener(this);

three=new JButton("3"); buttonpanel.add(three); three.addActionListener(this); min=new JButton("-"); buttonpanel.add(min); min.addActionListener(this); pow2=new JButton("x^2"); buttonpanel.add(pow2); pow2.addActionListener(this); mm=new JButton("M-"); buttonpanel.add(mm); mm.addActionListener(this); zero=new JButton("0"); buttonpanel.add(zero); zero.addActionListener(this); plmi=new JButton("+/-"); buttonpanel.add(plmi); plmi.addActionListener(this); poin=new JButton("."); buttonpanel.add(poin); poin.addActionListener(this); plus=new JButton("+"); buttonpanel.add(plus); plus.addActionListener(this); pow3=new JButton("x^3"); buttonpanel.add(pow3); pow3.addActionListener(this); rec=new JButton("1/x"); buttonpanel.add(rec); rec.addActionListener(this); sqrt=new JButton("Sqrt"); buttonpanel.add(sqrt); sqrt.addActionListener(this); lg=new JButton("log"); buttonpanel.add(lg); lg.addActionListener(this); div=new JButton("/"); div.addActionListener(this); buttonpanel.add(div); eq=new JButton("="); buttonpanel.add(eq); eq.addActionListener(this);

cont.add("Center",buttonpanel); cont.add("North",textpanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String s=e.getActionCommand(); if(s.equals("1")) { if(z==0) { jtx.setText(jtx.getText()+"1"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"1"); z=0; } } if(s.equals("2")) { if(z==0) { jtx.setText(jtx.getText()+"2"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"2"); z=0; } } if(s.equals("3")) { if(z==0) { jtx.setText(jtx.getText()+"3"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"3"); z=0; }

} if(s.equals("4")) { if(z==0) { jtx.setText(jtx.getText()+"4"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"4"); z=0; } } if(s.equals("5")) { if(z==0) { jtx.setText(jtx.getText()+"5"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"5"); z=0; } } if(s.equals("6")) { if(z==0) { jtx.setText(jtx.getText()+"6"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"6"); z=0; } } if(s.equals("7")) { if(z==0) { jtx.setText(jtx.getText()+"7"); }

else { jtx.setText(""); jtx.setText(jtx.getText()+"7"); z=0; } } if(s.equals("8")) { if(z==0) { jtx.setText(jtx.getText()+"8"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"8"); z=0; } } if(s.equals("9")) { if(z==0) { jtx.setText(jtx.getText()+"9"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"9"); z=0; } } if(s.equals("0")) { if(z==0) { jtx.setText(jtx.getText()+"0"); } else { jtx.setText(""); jtx.setText(jtx.getText()+"0"); z=0; } }

if(s.equals("AC")) { jtx.setText(""); x=0; y=0; z=0; } if(s.equals("log")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.log(Double.parseDouble(jtx.getText())); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("1/x")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=1/Double.parseDouble(jtx.getText()); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("x^2")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.pow(Double.parseDouble(jtx.getText()),2); jtx.setText(""); jtx.setText(jtx.getText() + a); }

} if(s.equals("x^3")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.pow(Double.parseDouble(jtx.getText()),3); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("+/-")) { if(x==0) { jtx.setText("-"+jtx.getText()); x=1; } else { jtx.setText(jtx.getText()); } } if(s.equals(".")) { if(y==0) { jtx.setText(jtx.getText()+"."); y=1; } else { jtx.setText(jtx.getText()); } } if(s.equals("+")) { if(jtx.getText().equals("")) { jtx.setText(""); temp=0; ch='+'; }

else { temp=Double.parseDouble(jtx.getText()); jtx.setText(""); ch='+'; y=0; x=0; } jtx.requestFocus(); } if(s.equals("-")) { if(jtx.getText().equals("")) { jtx.setText(""); temp=0; ch='-'; } else { x=0; y=0; temp=Double.parseDouble(jtx.getText()); jtx.setText(""); ch='-'; } jtx.requestFocus(); } if(s.equals("/")) { if(jtx.getText().equals("")) { jtx.setText(""); temp=1; ch='/'; } else { x=0; y=0; temp=Double.parseDouble(jtx.getText()); ch='/'; jtx.setText(""); } jtx.requestFocus(); }

if(s.equals("*")) { if(jtx.getText().equals("")) { jtx.setText(""); temp=1; ch='*'; } else { x=0; y=0; temp=Double.parseDouble(jtx.getText()); ch='*'; jtx.setText(""); } jtx.requestFocus(); } if(s.equals("MC")) { m1=0; jtx.setText(""); } if(s.equals("MR")) { jtx.setText(""); jtx.setText(jtx.getText() + m1); } if(s.equals("M+")) { if(k==1) { m1=Double.parseDouble(jtx.getText()); k++; } else { m1+=Double.parseDouble(jtx.getText()); jtx.setText(""+m1); } } if(s.equals("M-")) { if(k==1) { m1=Double.parseDouble(jtx.getText());

k++; } else { m1-=Double.parseDouble(jtx.getText()); jtx.setText(""+m1); } } if(s.equals("Sqrt")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.sqrt(Double.parseDouble(jtx.getText())); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("SIN")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.sin(Double.parseDouble(jtx.getText())); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("COS")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.cos(Double.parseDouble(jtx.getText())); jtx.setText(""); jtx.setText(jtx.getText() + a); }

} if(s.equals("TAN")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { a=Math.tan(Double.parseDouble(jtx.getText())); jtx.setText(""); jtx.setText(jtx.getText() + a); } } if(s.equals("=")) { if(jtx.getText().equals("")) { jtx.setText(""); } else { temp1 = Double.parseDouble(jtx.getText()); switch(ch) { case '+': result=temp+temp1; break; case '-': result=temp-temp1; break; case '/': result=temp/temp1; break; case '*': result=temp*temp1; break; } jtx.setText(""); jtx.setText(jtx.getText() + result); z=1; } } jtx.requestFocus(); } public static void main(String args[])

{ scientific n=new scientific(); n.setTitle("CALCULATOR"); n.setSize(370,250); n.setResizable(false); n.setVisible(true); } }

Output: C:\j2sdk1.4.0\bin>javac scientific.java C:\j2sdk1.4.0\bin>java scientific

Develop a template for linked-list class along with its methods in Java
link list program in java,java link list program. import java.io.*; import java.util.*; class Link { public T data; public Link nextLink; public Link(T d) { data = d; } public void printLink() { System.out.println("item:"+data); } } class LinkList { private Link first; private Link last; public LinkList() { first = null; } public boolean isEmpty() { return first == null; } public void insert(T d){ Link link = new Link(d); if(first==null){

link.nextLink = null; first = link; last=link; } else{ last.nextLink=link; link.nextLink=null; last=link; } } public Link delete() { Link temp = first; first = first.nextLink; return temp; } public void printList() { Link currentLink = first; while(currentLink != null) { currentLink.printLink(); currentLink = currentLink.nextLink; } System.out.println(""); } } class template { public static void main(String[] args) { int i,c=1,ch,p1=0,p2=0,p3=0; Scanner in=new Scanner(System.in); LinkList l = new LinkList(); LinkList s=new LinkList(); LinkList d=new LinkList(); do { System.out.println("1.INTEGER 2.STRING 3.DOUBLE 4.exit"); System.out.println("enter ur choice:"); c=in.nextInt(); switch(c) { case 1: do { if(p1==1)break; System.out.println("1.insert 2.delete 3.display 4.exit"); System.out.println("enter ur choice:"); ch=in.nextInt(); switch(ch) {

case 1: System.out.println("Integer list"); System.out.println("enter the insert value:"); i=in.nextInt(); l.insert(i); break; case 2: l.delete(); System.out.println("data deleted:"); break; case 3: System.out.println("elements are :"); l.printList(); break; case 4: p1=1; continue; } }while(c!=0); break; case 2: do { if(p2==1)break; System.out.println("1.insert 2.delete 3.display 4.exit"); System.out.println("enter ur choice:"); ch=in.nextInt(); switch(ch) { case 1: System.out.println("STRING list"); System.out.println("enter the insert value:"); String a=in.next(); s.insert(a); break; case 2: s.delete(); System.out.println("data deleted:"); break; case 3: System.out.println("elements are :"); s.printList(); break; case 4: p2=1; continue; }

}while(c!=0); break; case 3: do{ if(p3==1)break; System.out.println("1.insert 2.delete 3.display 4.exit"); System.out.println("enter ur choice:"); ch=in.nextInt(); switch(ch) { case 1: System.out.println("DOUBLE list"); System.out.println("enter the insert value:"); double x=in.nextDouble(); d.insert(x); break; case 2: d.delete(); System.out.println("data deleted:"); break; case 3: System.out.println("elements are :"); d.printList(); break; case 4: p3=1; continue; } }while(c!=0); break; case 4: System.exit(0); } }while(c!=0); } }

Output: C:\jdk1.5.0\bin>java template 1.INTEGER 2.STRING 3.DOUBLE 4.exit enter ur choice: 1 1.insert 2.delete 3.display 4.exit enter ur choice: 1 Integer list enter the insert value: 1 1.insert 2.delete 3.display 4.exit enter ur choice: 1 Integer list enter the insert value: 2 1.insert 2.delete 3.display 4.exit enter ur choice: 1 Integer list enter the insert value: 3 1.insert 2.delete 3.display 4.exit

enter ur choice: 3 elements are : item:1 item:2 item:3 1.insert 2.delete 3.display 4.exit enter ur choice: 2 data deleted: 1.insert 2.delete 3.display 4.exit enter ur choice: 3 elements are : item:2 item:3 1.insert 2.delete 3.display 4.exit enter ur choice: 4 1.INTEGER 2.STRING 3.DOUBLE 4.exit enter ur choice: 2 1.insert 2.delete 3.display 4.exit enter ur choice: 1 STRING list enter the insert value: niren 1.insert 2.delete 3.display 4.exit enter ur choice: 1 STRING list enter the insert value: kumar 1.insert 2.delete 3.display 4.exit enter ur choice: 1 STRING list enter the insert value: raj 1.insert 2.delete 3.display 4.exit

enter ur choice: 3 elements are : item:niren item:kumar item:raj 1.insert 2.delete 3.display 4.exit enter ur choice: 2 data deleted: 1.insert 2.delete 3.display 4.exit enter ur choice: 3 elements are : item:kumar item:raj 1.insert 2.delete 3.display 4.exit enter ur choice: 4 1.INTEGER 2.STRING 3.DOUBLE 4.exit enter ur choice: 4

Design a thread-safe implementation of Queue class. Write a multi-threaded producer-consumer application that uses this Queue class.
import java.io.*; class queue { int n; boolean v=false; synchronized int get() { if(!v) try { wait(); } catch(InterruptedException e){} System.out.println("GOT:"+n); v=false;

notify(); return n; } synchronized void put(int n) { if(v) try { wait(); } catch(InterruptedException e){} this.n=n; v=true; System.out.println("put:"+n); notify(); } } class prod implements Runnable { queue q; int n; prod(queue q,int n) { this.q=q; this.n=n; new Thread(this,"producer").start(); } public void run() { int i=1; while(i<=n) { try { Thread.sleep(1000); q.put(i++); } catch(InterruptedException e) {} } System.exit(0); } } class cons implements Runnable { queue q;

cons(queue q) { this.q=q; new Thread(this,"consumer").start(); } public void run() { while(true) { try { Thread.sleep(1000); q.get(); } catch(InterruptedException e){} }}} class nprod { public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream(System.in); System.out.println("enter the buffer size"); int a=Integer.parseInt(in.readLine()); queue q=new queue(); new prod(q,a); new cons(q); } }

Output:
C:\j2sdk1.4.0\bin>javac nprod.java Note: nprod.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\j2sdk1.4.0\bin>java nprod Enter the Buffer size 5 PUT:1 GOT:1 PUT:2 GOT:2 PUT:3 GOT:3 PUT:4 GOT:4

PUT:5 GOT:5

Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers below 100,000 and writes them into a pipe. Design another thread that generates fibonacci numbers and writes them to another pipe. The main thread should read both the pipes to identify numbers common to both
import java.io.*; import java.io.PipedWriter; import java.io.PipedReader; class fibonacci extends Thread { PipedWriter fw=new PipedWriter(); public PipedWriter getwrite() { return fw; } public void run() { super.run(); fibo(); } int f(int n) { if(n<2) return n; else return f(n-1)+f(n-2); } void fibo() { for(int i=2,fibv=0;(fibv=f(i))<100000;i++) { try{ fw.write(fibv); } catch(IOException e){ } } } }

class receiver extends Thread { PipedReader fibr,primer; public receiver(fibonacci fib,prime pr)throws IOException { fibr=new PipedReader(fib.getwrite()); primer=new PipedReader(pr.getwrite()); } public void run() { int p=0,f=0; try{ p=primer.read(); f=fibr.read(); } catch(IOException e) { } while(true) { try { if(p==f){ System.out.println ("Match:"+p); p=primer.read(); f=fibr.read(); } else if(f<p)< div=""> f=fibr.read(); else p=primer.read(); }catch(IOException e) {System.exit(-1); } } } } class prime extends Thread { PipedWriter pw=new PipedWriter(); public PipedWriter getwrite() {

return pw; } public void run() { super.run(); prim(); } public void prim() { for(int i=2;i<100000;i++) { if(isprime(i)) { try{ pw.write(i); } catch(IOException e){ } } } } boolean isprime(int n) { boolean p=true; int s=(int)Math.sqrt(n); for(int i=2;i<=s;i++) { if(n%i==0) p=false; } return p; } } class fibprime { public static void main (String[] args)throws IOException { fibonacci fi=new fibonacci(); prime pri=new prime(); receiver r=new receiver(fi,pri); fi.start(); pri.start(); r.start(); } }

Output:
C:\j2sdk1.4.0\bin>javac fibprime.java C:\j2sdk1.4.0\bin>java fibprime Match:2 Match:3 Match:5 Match:13 Match:89 Match:233 Match:1597 Match:28657

IT2301 - Java Programs - Develop a application using GUI and multithreads in java
import java.awt.*; import java.awt.Graphics2D; import javax.swing.*; import java.util.*; import java.awt.event.*; class AnalogClock extends JPanel { ImageIcon img; private GregorianCalendar m_calendar; private int[] x=new int[2]; private int[] y=new int[2]; private java.util.Timer clocktimer=new java.util.Timer(); private TimeZone clockTimeZone=TimeZone.getDefault(); public AnalogClock() { this.setPreferredSize(new Dimension(210,210)); this.setMinimumSize(new Dimension(210,210));

clocktimer.schedule(new TickTimerTask(),0,1000); } public void paint(Graphics g) { g.setColor(Color.orange); g.fillRect(0,0,this.getWidth(),this.getHeight()); drawCardinals((Graphics2D)g); drawHands((Graphics2D)g); } void clockMinutes(int startRadius,int endRadius,double theta) { theta-=Math.PI/2; x[0]=(int)(getWidth()/2+startRadius*Math.cos(theta)); y[0]=(int)(getHeight()/2+startRadius*Math.sin(theta)); x[1]=(int)(getWidth()/2+endRadius*Math.cos(theta)); y[1]=(int)(getHeight()/2+endRadius*Math.sin(theta)); } void drawCardinals(Graphics2D g) { g.setStroke(new BasicStroke(9)); g.setColor(Color.black); for(double theta=0;theta<="" span=""> { clockMinutes(100,100,theta); g.drawPolyline(x,y,2); } } public void drawHands(Graphics2D g) { double h=2*Math.PI*(m_calendar.get(Calendar.HOUR)); double m=2*Math.PI*(m_calendar.get(Calendar.MINUTE)); double s=2*Math.PI*(m_calendar.get(Calendar.SECOND)); g.setStroke(new BasicStroke(9)); clockMinutes(0,55,h/12+m/(60*12)); g.setColor(Color.red); g.drawPolyline(x,y,2); clockMinutes(0,70,m/60+s/(60*60)); g.setColor(Color.blue); g.drawPolyline(x,y,2); clockMinutes(0,70,s/60); g.setColor(Color.black); g.drawPolyline(x,y,2); g.fillOval(getWidth()/2-8,getHeight()/2-8,16,16); } class TickTimerTask extends TimerTask {

public void run() { m_calendar=(GregorianCalendar)GregorianCalendar.getInstance(clockTimeZone); repaint(); } } } class Analog { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("My Java Project Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Clock.gif")); AnalogClock m_AnalogClock=new AnalogClock(); frame.add(m_AnalogClock); frame.setVisible(true); } }

Output:
C:\jdk1.5.0\bin>javac Analog.java C:\jdk1.5.0\bin>java Analog

You might also like