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

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

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

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

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

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

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

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

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

System.ABHIROX.CO.whether file is readable.println(f. System. class FileOp { public static void main(String arg[ ])throws IOException { File f=new File("Z:/week 3/Palindrome.io.getName()).out.CC. import java.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.CO.out.getName()).*.lang.NR .ABHIROX.java").out.println(f.println(f.*. WWW. Code: import java.getPath()).util.out.canRead()?"Readable":"Not Readable"). WWW. System.println(f. whether file is writable the type of file and length of file in bytes.println(f. System.*. System.exists()?"Exist":"Non Exist").out. import java.

ABHIROX.*. WWW.CC. } } 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. try{ File f=new File("Z:/f. WWW.io.txt ").length()+"bytes"). class FileNo { public static void main(String arg[ ])throws IOException { int linenum=0. System. import java.out.util.CO.out. if(f. import java.CO.println(f. String line. Code: import java.canWrite()?" " :" ").*.System.NR .ABHIROX.exists()) { Scanner infile=new Scanner(f).println(f.*.lang.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

drawRect(100. import java. g.90.100. g.BLUE).60.awt.4.rectangles and ovals.drawOval(195. WWW.40.55).setColor(Color. } } /*<applet code="Draw.90.drawRoundRect(140.*.drawLine(3.*.ABHIROX.NR .ABHIROX.10.class" width=300 height=300></applet>*/ Output: Date: 17-03-2009 WWW.90.CO.30).Aim: Write a java program that allows the user to draw lines. g.applet. Code: import java.5).CO.23). g.30.90.CC. public class Draw extends Applet { public void paint(Graphics g) { g.

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

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

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

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

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

Sign up to vote on this title
UsefulNot useful