Date: 16-12-2008 Week-1a) Aim: Write a java program that print all roots of equation of quadratic a,b,c will be supplied

through keyboard Code: import java.lang.*; import java.math.*; class Quad { public static void main(String arg[]) { int a,b,c; a=Integer.parseInt(arg[0]); b=Integer.parseInt(arg[1]); c=Integer.parseInt(arg[2]); int delta=(b*b)-(4*a*c); if(delta<0) { System.out.println("roots are imaginary,no real values"); } else { double x1=(-b+Math.sqrt(delta))/(2*a); double x2=(-b-Math.sqrt(delta))/(2*a); System.out.println(x1+" "+x2); } } } Output:

WWW.ABHIROX.CO.CC, WWW.ABHIROX.CO.NR

Week-1b) Aim: Write a java program that uses both recursive and non recursive functions to print the nth value in Fibonacci series. Code: import java.lang.*; class Fibo { public static void main(String arg[ ]) { int f,f1=0,f2=1,n; n=Integer.parseInt(arg [0]); System.out.println(f1); System.out.println(f2); for(int i=0;i<n-1;i++) { f=f1+f2; f1=f2; f2=f; System.out.println(f); } System.out.println("Fibonacci series using recursion"); for(int i=0;i<n;i++) System.out.println(fib(i)); } static int fib(int n) { if(n==0||n==1) return 1; else return fib(n-1)+fib(n-2); } }

WWW.ABHIROX.CO.CC, WWW.ABHIROX.CO.NR

Output:

WWW.ABHIROX.CO.CC, WWW.ABHIROX.CO.NR

Date: 23-12-2008 Week-2a) Aim: Write a java program that prompts the user for an integer and then prints out all prime numbers upto the integer. Code: import java.lang.*; class Prime { public static void main(String arg[]) { int n,c,i,j; n=Integer.parseInt(arg[0]); System.out.println("prime numbers are"); for(i=1;i<=n;i++) { c=0; for(j=1;j<=i;j++) { if(i%j==0) c++; } if(c==2) System.out.println(" "+i); } } } Output:

WWW.ABHIROX.CO.CC, WWW.ABHIROX.CO.NR

println("Enter The Row Size Of The Second Matrix").out. System. int c[][]=new int[r1][c2].in).j<c2.i<r1. Code: import java.out.k<r2.j++) { a[i][j]=Integer.parseInt(dis.readLine()).ABHIROX.j<c1.Week-2b) Aim: Write a java program to multiply two given matrices. for(k=0.println("Enter The Row Size Of The First Matrix").i<r1.println("Enter The Column Size Of The First Matrix"). System. for(i=0.readLine()).*.parseInt(dis.out. int b[][]=new int[r2][c2].out. System.i++) for(j=0.j++) { c[i][j]=0.j<c1.readLine()).i<r2. import java. int r2=Integer.i++) { for(j=0.io.j++) { b[i][j]=Integer. } System.parseInt(dis. int a[][]=new int[r1][c1].out.lang. int c2=Integer.parseInt(dis. WWW. System.parseInt(dis. System.*.println("Enter The Elements Into The Second Matrix").CO.println("Enter The Column Size Of The Second Matrix").i++) for(j=0. } if(c1!=r2) System.println("Multiplication Is Not Possible"). else { for(i=0. int i.NR .readLine()). int r1=Integer.CC.readLine()).out.readLine()).ABHIROX.j.CO.k. for(i=0.parseInt(dis.out.println("Enter The Elements Into The First Matrix"). int c1=Integer. class Matrix { public static void main(String arg[])throws IOException { DataInputStream dis=new DataInputStream(System.k++) { WWW.

CO. WWW.i++) { for(j=0.out.j<c2.j++) { System.ABHIROX.CC.} } c[i][j]=c[i][j]+a[i][k]*b[k][j]. } for(i=0.ABHIROX.out.NR .println(" ").CO.println(c[i][j]+" } System. WWW.i<r1. } } } Output: } ").

CO.CC.out.println("sum= "+sum).*.countTokens()).parseInt(sto.CO.ABHIROX. System.out.Week-2c) Aim: Write a program that display each integer and sum of all integers using stringtokenizer. WWW. StringTokenizer sto=new StringTokenizer(arg[0]. sum=sum+a. class Stringtoken { public static void main(String arg[]) { int sum=0.util.out. Code: import java.println(sto. while(sto.nextToken()). } } Output: WWW. } System.ABHIROX.hasMoreTokens()) { int a=Integer. System.println(a).NR .":").

println("not palindrome").NR . int right=word. class Palindrome { public static void main(String arg[ ]) throws IOException { DataInputStream dis=new DataInputStream(System.length( )-1.println("palindrome"). else System.Ex:MADAM Code: import java.*.CO.io.*. import java.} if(flag==1) System.CC. } } Output: WWW. right--. String word=dis.util. import java.CO.out. int flag=1.readLine( ).in).ABHIROX. WWW.charAt(left)!=word.charAt(right)) { flag=0.lang.out.*.Date: 30-12-2008 Week-3a) Aim: Write a java program that checks whether a given string is a palindrome or not.ABHIROX. } left++. while(left<right) { if(word. //break. int left=0.

i<n.ABHIROX. import java. String array[ ]=new String[n].println("enter the size").out.i++) { for(int j=0.readLine().*.lang.compareTo(array[j+i])>0) { String temp=array[j].out. int n=Integer. System.j++) { if(array[j].i<n.NR .util.*. } for(int i=0.in).CC. array [j]=array[j+1].*. import java.println("enter names").println("the sorted strings are"). } } } System.i++) { array[i]=dis.io.j<n-1.parseInt(dis.out. array[j+1]=temp. class Sort { public static void main(String arg[ ]) throws IOException { DataInputStream dis=new DataInputStream(System.Week-3b) Aim:Write a java program for sorting a given list of name in ascending order.CO.i++) WWW. Code: import java.i<n. for(int i=0. for(int i=0. WWW.CO. System.readLine()).ABHIROX.

out.} } System.CO.ABHIROX. Output: WWW.println(array[i]).NR .ABHIROX.CC.CO. WWW.

ABHIROX.x<n. Code: import java. } } WWW. int i=0.*. int n=st. import java. String str=dis.compareTo(a[y+1])>0) { temp=a[y].x++) { for(int y=0. while(st.count=0.*. a[y]=a[y+1].countTokens( ).*.Week-3c) Aim: Write a java program to make frequency count of words in a given text.CO. String a[ ]=new String[n].hasMoreTokens()) { a[i]=st. i++.readLine().CC.y++) { if(a[y]. class Fre { public static void main(String[ ] args) throws IOException { DataInputStream dis=new DataInputStream(System.ABHIROX. StringTokenizer st=new StringTokenizer(str).in).io.lang. } for(int x=1.y<n-x. WWW. String temp.nextToken(). import java.NR .CO. a[y+1]=temp.util.

compareTo(a[i+1])==0) { count++. for(i=0.} System.println("the sorted string are:"). WWW.i++) { System.out. if(i<n-1) { while(a[i].out.out.i<n.ABHIROX.println().i++) {count=1. System.println(a[i]+" "). i++. } System.ABHIROX.i<n.println(a[i]+" "+count).out.CO.exit(0).NR .CC.out. } } } System.CO. } } } Output: WWW. for(i=0. if(i>=n-1) { System.println(a[i]+" "+count).

WWW. WWW.*.getPath()). System. System.ABHIROX. Code: import java.util.println(f.java"). System. System.out.CO.println(f.Date: 06-01-2009 Week-4a) Aim: Write a java program that reads a filename from user then displays information from user then displays information about whether the file exists.canRead()?"Readable":"Not Readable").out.ABHIROX. System.CC.println(f.whether file is readable.NR . whether file is writable the type of file and length of file in bytes.out.lang.println(f.CO.exists()?"Exist":"Non Exist").*.out.out.getName()). import java. import java.println(f.io.*. class FileOp { public static void main(String arg[ ])throws IOException { File f=new File("Z:/week 3/Palindrome.getName()).

*.ABHIROX.lang.length()+"bytes").*.canWrite()?" " :" "). } } Output: Week-4b) Aim: Write a java program that reads a file and displays the file on the screen with a line number before each line.exists()) { Scanner infile=new Scanner(f). import java. System.io.out.System. class FileNo { public static void main(String arg[ ])throws IOException { int linenum=0.txt "). if(f. try{ File f=new File("Z:/f. import java.out. Code: import java.CO.ABHIROX. String line.println(f. WWW. WWW.CC.println(f.CO.NR .util.*.

int i. class FileCount { public static void main(String arg[]) { int ccount=0.CO.ABHIROX.out. if(i!=-1) { WWW.nextLine(). WWW.*.hasNext()) { line=infile.println(e). try { FileInputStream f=new FileInputStream("Z:/week 3/Palindrome. do{ i=f.*. import java.lines and words in atext file import java. } } Output: } } "+line).NR . System.CC.CO.ABHIROX.java").io.out.read().lang.println(++linenum+" } infile.wcount=0.close(). } catch(Exception e) { System.while(infile.lcount=0. Week-4c) Aim: Write a java program that displays the number of characters.

util.io.ABHIROX.println("Character count is "+ccount).*.out.lang.out. System. } } Output: } ccount++.println(e). System.println("word count is "+wcount).println("line count is "+lcount). import java.*. Date:03-02-2009 Week-5a)i) Aim: Write a java program that Implements stack ADT. import java. System.out. public void pop(). public void push().close(). WWW. Code: import java.CO.*. } catch(Exception e) { System.ABHIROX. public void display().} }while(i!=-1). public void peek().CC. f. interface Mystack { int n=10. } class Stackimp implements Mystack { WWW. if(i=='\n') lcount++.NR . if((char)i==' '||(char)i=='\n') wcount++.out.CO.

} } public void peek() { if(top<0) { System.println("popped element" +popper).ABHIROX.println("e"). top--. stack[++top]=ele. } } public void pop() { if(top<0) { System.in).println("enter element"). return: } else { WWW. int ele=Integer.parseInt(dis.NR .readLine()).CO.CO.out.println("underflow").println("overflow"). int top=-1.ABHIROX.out.out.out. } else { System.out.CC. WWW. return. if(top==(n-1)) { System. System.int stack[]=new int[n].println("underflow"). return.out. } } catch(Exception e) { System. } else { int popper=stack[top]. public void push() { try{ DataInputStream dis=new DataInputStream(System.

break. case 3:stk.ABHIROX. WWW. break.CO.readLine()).parseInt(dis. } public void display() { if(top<0) { System.pop(). int ch=0. switch(ch){ case 1:stk.i<=top. } }while(ch<=5).println("popped element" +popper).NR .push(). Stackimp stk=new Stackimp().peek(). } } Output: WWW. case 5:System.println("empty").display().println("elements are"+str). for(int i=0.out.pop 3. System.CO.peek 5.exit(0).out. break. } else { String str=" ". ch=Integer. case 2:stk.display”).out.i++) str=str+" "+stack[i]. break.int popper=stack[top].ABHIROX. case 4:stk. } } } } class Stackadt { public static void main(String arg[])throws IOException { DataInputStream dis=new DataInputStream(System.peek 4.println("enter ur choice for 1.out. do{ System. return. System.in).push 2.CC.

ABHIROX.println("enter expresion"). DataInputStream dis=new DataInputStream(System.Week-5a)ii) Aim: Write a java program that evaluates the postfix expression Code: import java.*. class IntoPo { public static void main(String arg[])throws IOException { Stack stk=new Stack(). String exp=dis. WWW.lang. import java.CO.readLine().ABHIROX. WWW.*.NR .*. char input. System.CO.CC.in).out.io.util. import java. String output=" ".

CO.println("result is"+output). } } Output: WWW. break.charAt(pos).CO. default : output=output+input. break. } } System.out.push(input).for(int pos=0.pos++) { input=exp. break. switch(input) { case '+' : case '-' : case '*' : case '/' : stk. case '(' : break.ABHIROX.pos<exp. case ')' : char ch=((Character)stk.NR . WWW.length().CC.pop()). output=output+ch.ABHIROX.

CO.NR . Week-5a)iii) Aim:Write a java program that evaluates the postfix expression WWW.CO.CC.ABHIROX. WWW.ABHIROX..

case '-' : val=fo-so. } public static void main(String arg[])throws IOException { Stack stk=new Stack().char op) { int val=0.*. break.CC. break. DataInputStream dis=new DataInputStream(System. String str=dis. break.io.CO. import java.util.*.CO.charAt(pos).NR . for(int pos=0.push(new Integer((Character. switch(ch){ case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : stk.10)))). } return val. break. import java. case '+': case '-': case '/': case '*': WWW. case '/' : val=fo/so. char ch. class Evaluation { static int dooperation(int fo.pos++) { ch=str.lang. WWW.length().pos<str.*.ABHIROX. case '*' : val=fo*so.in).digit(ch.ABHIROX.readLine(). break.import java. switch(op){ case '+' : val=fo+so.int so.

ch). break.intValue().intValue().println("result is" +stk.NR . WWW.} } System.pop()). } } int fo=((Integer)stk.push(new Integer(result)).ABHIROX.pop()).CO. int so=((Integer)stk. int result=dooperation(fo. and computes as factorial value and returns it in another text field.so.out.ABHIROX.pop()).when the button named “Compute” is clicked WWW. Date: 10-02-2009 Week-6a) Aim:Develop an applet that receives an integer in one text field.CC. stk.CO.

import java.blue).drawString("This is IT IInd Year IInd sem".CO.class" height=100 width=500></applet>*/ public class appletTest extends Applet { public void paint(Graphics g) { g. setForeground(Color.ABHIROX. import java.*.io.Applet. /*<applet code="appletTest.*.60).applet.ABHIROX.CC.NR .CO. } } Output: Week-6b) Aim:Develop an applet that displays a simple message Code: WWW.50. WWW.import java.awt.

add(l). b.CC. Button b. TextField tf2.setText(str1).*. public void init() { l=new Label("Enter the number & press the button").awt.import java. Label l. import java. add(tf1).i<=n.Applet.class" height=100 width=500></applet>*/ public class AddEvent extends Applet implements ActionListener { TextField tf1.event. add(tf2). if(str=="press me") { int n=Integer. for(int i=1. tf2. b=new Button("press me"). /*<applet code="AddEvent.CO.NR . tf1=new TextField("".awt. String str1.5).parseInt(tf1. add(b). str1=""+fact.ABHIROX.addActionListener(this).i++) fact=fact*i. int fact=1. } public void actionPerformed(ActionEvent ae) { String str=ae.applet.getActionCommand(). tf2=new TextField("". WWW.getText()).CO.10).*. } } } Output: WWW.ABHIROX. import java.

ABHIROX.CO.Date: 24-02-2009 Week-7) WWW.CO.NR . WWW.CC.ABHIROX.

b6=new Button("6"). b4. sub=new Button("-"). b8=new Button("8"). WWW. b5=new Button("5").*.*.addActionListener(this).*.% operations. b3. Button add. b1=new Button("1"). Button b0.setLayout(new BorderLayout()).event.addActionListener(this). space=new Button("c").b. p3=new Panel(). b2=new Button("2").setLayout(new GridLayout(4. p2. WWW./. b4=new Button("4"). add=new Button("+").awt.b2. int first=0.result=0.mod.CO.space.4)). import java.CC.20).s="". import java. p3. String a="". p1.b1.Use a grid layout to arrange buttons for the digits +.res. p1=new Panel(). mod=new Button("%"). mul=new Button("*"). p2=new Panel().addActionListener(this).b6. Panel p1. b1. import java. b2. div=new Button("/"). b0. res=new Button("=").b4. String oper="".sub. b7=new Button("7").b8.b7.p="". b0=new Button("0").ABHIROX. public class Calculator extends Applet implements ActionListener { TextField t1.-.*.b5.div.awt.p3.second=0. b9=new Button("9").b3. public void init() { Panel p2.mul. t1=new TextField(a.add(t1).Adda text field to display the result.CO.applet. b3=new Button("3").Aim: Write a java program that works as a simple calculator.NR .ABHIROX.b9.addActionListener(this).addActionListener(this).

add(b1).addActionListener(this).CENTER).add(sub).add(b4). b7.add(b6). p3.setText(t1. p3. space.parseInt(t1. p3. if(a=="0"||a=="1"||a=="2"||a=="3"||a=="4"||a=="5"||a=="6"||a=="7"||a=="8"|| a=="9") { t1. b8. p3. p3.add(add). p3.add(mod).add(p2.add(mul). p1.getText()). p1.getActionCommand(). b9. } if(a=="+"||a=="-"||a=="*"||a=="/"||a=="%") { first=Integer. p3.CC. sub. div.NORTH). t1.add(b0).add(space).ABHIROX.add(b7). p3.addActionListener(this). p3.CO. mod. } public void actionPerformed(ActionEvent ae) { a=ae.addActionListener(this).addActionListener(this). res.b5.add(b9).addActionListener(this).add(res).addActionListener(this).addActionListener(this). add. p3.ABHIROX.CO. p3.BorderLayout.BorderLayout.addActionListener(this). add(p1). p3. WWW.add(b2).addActionListener(this). p3. b6.getText()+a).addActionListener(this). mul.add(b8). oper=a. p3. p3. p3.NR .addActionListener(this).add(p3.addActionListener(this).setText(""). WWW.add(b3). p3.add(div).add(b5).

ABHIROX. if(oper=="/") result=first/Integer.getText()).parseInt(t1.parseInt(t1.setText(result+"").ABHIROX. Output: Date: 10-02-2009 WWW.getText()).parseInt(t1.CO. WWW.parseInt(t1.getText()). if(oper=="-") result=first-Integer.CO. if(oper=="%") result=first%Integer.NR . } if(a=="c") t1.getText()).parseInt(t1.CC.} } /*<applet code=Calculator width=200 height=200></applet>*/ } if(a=="=") { if(oper=="+") result=first+Integer.getText()). t1.setText(""). if(oper=="*") result=first*Integer.

blue). public void init() { addMouseListener(this).io. } public void mouseMoved(MouseEvent me) { txt="Mouse Moved". addMouseMotionListener(this). import java. /*<applet code=Mouse height=400 width=400></applet>*/ public class Mouse extends Applet implements MouseListener.*. } public void mouseEntered(MouseEvent me) { txt="Mouse Entered". import java.applet.MouseMotionListener { String txt="".red).Week-8 Aim: Write a java program for handling mouse events. int x=10.NR .blue). } public void mousePressed(MouseEvent me) { txt="Mouse Pressed". setForeground(Color. setForeground(Color. repaint().awt.CC.y=30.*. } public void mouseExited(MouseEvent me) { txt="Mouse Exited".ABHIROX. WWW.Applet.event. setForeground(Color. import java. Code: import java.CO. } public void mouseClicked(MouseEvent me) { txt="Mouse Clicked".pink).ABHIROX. repaint().awt.CO.*. repaint(). } WWW. repaint(). setForeground(Color. repaint().

} public void paint(Graphics g) { g.30.40). WWW.ABHIROX.CO.CO.green).ABHIROX. setForeground(Color. } public void mouseReleased(MouseEvent me) { txt="Mouse Released".CC.} public void mouseDragged(MouseEvent me) { txt="Mouse Dragged". showStatus("Mouse events Handling"). } Output: Date: 24-02-2009 WWW. setForeground(Color.yellow).drawString(txt. repaint(). repaint().NR .

CC.i<10. } public void run() { for(int i=0.i<10.i++) { System.CO. } WWW.println("hello").sleep(1000). System.out. Frst() { t=new Thread(this).start().i++) { System.out. } public void run() { for(int i=0.println(e).sleep(2000).println("hello"+i).Week-9a) Aim: Write a java program that creates three threads.First thread displays”Good Morning” every one second.the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds. sec() { t=new Thread(this).out. try{ t.NR .start(). try{ t.ABHIROX. t. t. } } } } class sec implements Runnable { Thread t.out. System. Code: class Frst implements Runnable { Thread t. } catch(Exception e) { System.CO.out.println("Good Morning").ABHIROX.println("Good Morning"+i). WWW.

} catch(Exception e) { System. new sec().CO. } public void run() { for(int i=0.sleep(3000). t.ABHIROX.catch(Exception e) { System. } } } class third implements Runnable { Thread t.println("welcome"+i). } } } WWW. System.out.CC.start().println(e).out. new third(). } } } } public class Multithread { public static void main(String arg[]) { new Frst().i<10. WWW.out.println("welcome").CO. try{ t.println(e).ABHIROX. third() { t=new Thread(this).out.NR .i++) { System.

CO.NR .CO. WWW.ABHIROX.Output: Week-9b) WWW.ABHIROX.CC.

CO. WWW.lang. Producer(Q q) { WWW. System.println("InterruptedException catch").ABHIROX. return n. notify(). } catch(InterruptedException e) { System.*.out.out.ABHIROX. } System.out. } catch(InterruptedException e) { System.NR .println("put:"+n). class Q { int n. } } class Producer implements Runnable { Q q. valueSet=true. valueSet=false.CC.println("InterruptedException catch"). import java.CO.lang. } synchronized void put(int n) { if(valueSet) try{ wait().Aim: Write a java program that correctly implements producer consumer problem using the concept of inter thread communication.out.println("got:"+n).n=n.Thread. Code: import java. boolean valueSet=false. notify(). synchronized int get() { if(!valueSet) try{ wait(). } this.

"consumer"). } public void run() { while(true) { q."producer").CO.q=q. } } } class PCFixed { public static void main(String args[]) { Q q=new Q().start(). new Consumer(q).CC. } } } class Consumer implements Runnable { Q q. WWW. } } Output: WWW.this.q=q. } public void run() { int i=0. new Producer(q).ABHIROX.put(i++).start(). while(true) { q.CO. Consumer(Q q) { this. new Thread(this.NR .ABHIROX.get(). new Thread(this.

ABHIROX.Date: 03-03-2009 WWW.CO. WWW.CC.CO.ABHIROX.NR .

If Num1 or Num2 were not an integer.CO.getActionCommand()=="Divide") { try{ int n1=Integer. add(tf2).class" height=100 width=500></applet>*/ public class AddEvent extends Applet implements ActionListener { TextField tf1.awt.ABHIROX.parseInt(tf1.Num1 and Num2. add(tf1).5).Week-10 ) Aim: Write a program that creates a user interface to perform integer divisions. int n=n1/n2. add(b). Button b.showMessageDialog(null.Applet. } public void actionPerformed(ActionEvent ae) { if(ae. public void init() { l=new Label("enter the numbers and press divide button").swing.NR . b. WWW.ABHIROX.5).CC. tf3.parseInt(tf2. b=new Button("Divide").the program would throw a NumberFormatException.CO.setText(""+n).*. add(tf3). } catch(ArithmeticException e1) { JOptionPane.The divison of Num1 and Num2 is displayed in the Result field when the Divide button is clicked.If Num2 were Zero.*.getText()). TextField tf2. add(l)."Arthimetic Exception"). /*<applet code="AddEvent. import javax.5).awt. int n2=Integer.The user enters two numbers int the textfields. tf2=new TextField("". tf1=new TextField("".addActionListener(this). WWW.getText()).*.the program would throw an ArithmeticException Display the exception in a message dialog box. Code: import java. Label l. TextField tf3.event. import java. tf3=new TextField("".applet. import java.

showMessageDialog(null.} } } } catch(NumberFormatException e2) { JOptionPane.CO.ABHIROX.CO.NR .CC. WWW.ABHIROX. } Output: Date: 03-03-2009 Week-11) WWW."NumberFormatException").

io. and then sends the result back to the client.out. import java. DataInputStream dis=new DataInputStream(s.(Use java. The client sends a data to a server.Aim: Write a java program that implements a simple client/server application.io. import java. WWW.and the result produced by the server is the area of the circle. DataInputStream dis=new DataInputStream(System. System.println(3.accept().ABHIROX. ps.CO.println("area of the circle from server:"+dis1. PrintStream ps=new PrintStream(s.14*n*n).parseInt(dis.10000).*.net) Code: import java.NR . DataInputStream dis1=new DataInputStream(s.CC. PrintStream ps=new PrintStream(s. int n=Integer.println("enter the radius of the circle "). The client displays the result on the console .getLocalHost().net.getOutputStream()). class Client { public static void main(String ar[])throws Exception { Socket s=new Socket(InetAddress.readLine()). class Server { public static void main(String ar[])throws Exception { ServerSocket ss=new ServerSocket(10000).*.getInputStream()).net. ps.readLine()).in).getOutputStream()).parseInt(dis.out.*.For Ex: The data sent from the client is the radius of a circle . } } Output: WWW.ABHIROX.getInputStream()).CO.readLine()). The server receives the data uses it to produce a result. int n=Integer. System. Socket s=ss.println(n). } } import java.*.

WWW.CC.Date: 10-03-2009 Week-12a) WWW.ABHIROX.NR .ABHIROX.CO.CO.

50. add(Red).addItemListener(this). When a radio button is selected. WWW. /*<applet code="CBGroup" width=250 height=200></applet>*/ public class CBGroup extends Applet implements ItemListener { String msg="".red).cbg.awt. Green=new Checkbox("GREEN".CC.getLabel()=="YELLOW") { g.*.50).Yellow. add(Yellow). the light is turned on.50. if(cbg.applet.drawOval(10.NR . yellow.awt. Green. import java.setColor(Color. CheckboxGroup cbg.50.CO.false). import java.10. public void init() { cbg=new CheckboxGroup().ABHIROX.cbg. add(Green). Code: import java.getSelectedCheckbox(). and only one light can be on at a time No light is on when the program starts. Red=new Checkbox("RED".CO. } public void paint(Graphics g) { //g. Yellow.addItemListener(this).getSelectedCheckbox(). } if(cbg.false). Yellow=new Checkbox("YELLOW". g.addItemListener(this).setColor(Color.50).event.10. or green.10.Green. g.*. } public void itemStateChanged(ItemEvent ie) { repaint(). Red.getSelectedCheckbox(). } if(cbg.50).getLabel()=="RED") { g.cbg.false). The program lets the user select one of three lights: red.fillOval(10.yellow).fillOval(10.getLabel()=="GREEN") { WWW. Checkbox Red.*.ABHIROX.Aim: Write a java program that stimulates a traffic light.

g.10.ABHIROX.NR .ABHIROX.CO.fillOval(10. } } } Output: Week-12b) WWW. WWW.g.CO.50).50.CC.setColor(Color.green).

*. WWW.23).CO.10.class" width=300 height=300></applet>*/ Output: Date: 17-03-2009 WWW.drawRoundRect(140.60. g.CO.applet. g.90. g.40. Code: import java.CC.30.drawLine(3.90.*.ABHIROX.Aim: Write a java program that allows the user to draw lines.rectangles and ovals.55).BLUE).90.90. } } /*<applet code="Draw.30).setColor(Color.ABHIROX.NR .awt. import java. g.drawRect(100.100.drawOval(195.4.5). public class Draw extends Applet { public void paint(Graphics g) { g.

println("three").Week-13a) Aim: Write a java program to create an abstract class named Shape that contains an empty method name numberOfSides(). } } class Hexagon extends Shape { public void numberOfSides() { System. } class Traingle extends Shape { public void numberOfSides() { System. } } public class Sides { public static void main(String arg[]) { Traingle T=new Traingle(). Each one of the classes contains only the method numberOfSides() that shows the number of sides in the given geometrical figures. WWW.numberOfSides().ABHIROX.out.println("four"). Hexagon H=new Hexagon(). T.ABHIROX.out.*. } } class Trapezoid extends Shape { public void numberOfSides() { System. H. abstract class Shape { abstract void numberOfSides().numberOfSides(). Trapezoid Ta=new Trapezoid().out.lang.Provide three classes named Trapezoid. Code: import java.CO.numberOfSides().NR .println("six"). Ta. WWW.Traingle and Hexagon such that each of the classes extends the class Shape.CO.CC.

ABHIROX. WWW.NR .CO.CC.CO.ABHIROX.Output: WWW.

import java. WWW.available() != 0) { initData[rows++] = dis.util. data = new Object[rows . Code: import javax.*. and the remaining lines corresponding to rows in the table.swing.ABHIROX.hasMoreTokens()) { data[i .". i++) { if (i != 0) { int k = 0.NR .nextToken().awt.Week-13b) Aim: Suppose that a table named Table. File file = new File("I:/Table.txt").io. while (dis. public class JTableEx extends JPanel { public JTableEx() { super(new GridLayout(1.*.txt is stored in a text file. i < rows. bis = new BufferedInputStream(fis). } } else WWW.CO. DataInputStream dis = null. try { fis = new FileInputStream(file).". Object[][] data = null. FileInputStream fis = null. Write a java program to display the tabe using JTable Component. StringTokenizer st1 = new StringTokenizer(initData[i]. The first line in the file is the header.countTokens()].readLine()."). for (int i = 0.CO.*. String initData[] = new String[100]. import java. } StringTokenizer st = new StringTokenizer(initData[0].ABHIROX. The elements are separated by commas. import java. String[] columnNames = null.1][k++] = st1. 0)).1][st."). while (st1. BufferedInputStream bis = null. dis = new DataInputStream(bis).countTokens()]. int rows = 0.*.CC. columnNames = new String[st.

//table. JTableEx newContentPane = new JTableEx().hasMoreTokens()) { columnNames[j++] = st.ABHIROX. JScrollPane scrollPane = new JScrollPane(table).EXIT_ON_CLOSE).printStackTrace(). dis. } fis.NR . } private static void createAndShowGUI() { JFrame frame = new JFrame("SimpleTableDemo").printStackTrace().CO.invokeLater(new Runnable() { public void run() { createAndShowGUI(). add(scrollPane).setContentPane(newContentPane).CC. frame.setFillsViewportHeight(true).setOpaque(true). } public static void main(String[] args) { javax. frame.swing.close().setDefaultCloseOperation(JFrame.ABHIROX. newContentPane. } } } catch (FileNotFoundException e) { e. while (st. } catch (IOException e) { e.nextToken(). WWW. } final JTable table = new JTable(data. 100)).CO. frame.{ int j = 0.close(). WWW. } }).SwingUtilities. frame.setVisible(true).pack(). table.setPreferredScrollableViewportSize(new Dimension(500. bis. columnNames).close().

ABHIROX.} Output: } WWW. WWW.CO.CC.ABHIROX.NR .CO.

Sign up to vote on this title
UsefulNot useful