You are on page 1of 30

SL.NO.

01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11.

PROGRAM NAME

PAGE NO.
2 3 4 5 6 7 8 9 13 14

Write a program to check whether two strings are equal or not. Write a program to display reverse string. Write a program to find the sum of digits of a given number. Write a program to display a multiplication table. Write a program to display all prime numbers between 1 to 1000. Write a program to insert element in existing array. Write a program to sort existing array Write a program to create object for TreeSet and Stack and use all methods. Write a program to check all the math class functions. Write a program to execute any Windows 95 application (like notepad calculator etc). Write a program to find the total memory, free memory and free memory after executing garbage collection (gc()). Write a program to copy a file to another file using java.io package classes. Get the file names at run time and if the target file existed then ask confirmation to overwrite and take necessary actions. Write a program to get a file name at run time and display number of lines and words in that file. Write a program to list files in the current working directory depending upon a given pattern. Create a Text field that allows numeric value and in specified length. Create a Frame with two labels at runtime display X and Y co-ordinates of mouse pointer in the labels. Create a Frame and Checkbox group with five Checkbox with Label as green, red, blue, yellow and white. At run time change the background color with appropriate selection of the Checkbox. Create a Frame, Choice and Label. Add five items in the choice. At runtime display selected items of choice in the label. Create a Frame with three Scrolls; change the background Color of the Frame using function with value of Scrolls. Create a Message Dialog Box like Windows 95 Message Box. At runtime pass message for that Message Box. 1

16 17

12.

13. 14.

19

20 21 23 24

15. 16. 17.

18. 19. 20.

26

28 30

1. WRITE A PROGRAM TO CHECK WHETHER TWO STRINGS ARE EQUAL OR NOT. import java.io.*; class pro1 { public static void main(String args[]) throws IOException { String s1,s2; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter First string : "); s1=b.readLine(); System.out.print("Enter Second string : "); s2=b.readLine(); if(s1.equals(s2)) System.out.print("Equal"); else System.out.print("Not Equal"); } }

OUTPUT :Enter First string : welcome Enter Second string : user Not Equal Enter First string : welcome Enter Second string : welcome Equal

2. WRITE A PROGRAM TO DISPLAY REVERSE STRING. import java.io.*; class pro2 { public static void main(String args[]) throws IOException { String s1; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter any string : "); s1=b.readLine(); int cnt=s1.length(); System.out.println("Actual String = "+s1); System.out.print("Reverse String= "); while(cnt>0) { System.out.print(s1.charAt(cnt-1)); cnt--; } System.out.println(); } }

OUTPUT :Enter any string : PROGRAM Actual String = PROGRAM Reverse String = MARGORP

3. WRITE A PROGRAM TO FIND THE SUM OF DIGITS OF A GIVEN NUMBER.

import java.io.*; class pro3 { public static void main(String args[]) throws IOException { int num,rem,sum=0; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter any number : "); num=Integer.parseInt(b.readLine()); while(num!=0) { rem=num%10; sum=sum+rem; num=num/10; } System.out.print("Sum Of the Digit= "+ sum); } }

OUTPUT :Enter any number : 4652 Sum Of the Digit = 17

4. WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE.

import java.io.*; class pro4 { public static void main(String args[]) throws IOException { int num,cnt,sum; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter any number : "); num=Integer.parseInt(b.readLine()); for(cnt=1;cnt<=10;cnt++) { sum=num*cnt; System.out.println(num+" * "+cnt+" = "+ sum); } } }

OUTPUT :Enter any number : 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50

5. WRITE A PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN 1 TO 1000. import java.io.*; class pro5 { public static void main(String args[]) { int i,j; for(i=1; i<1000; i++) { int flag=0; for(j=2; j<(i/2); j++) { if(i % j==0) { flag=1; break; } } if(flag==0) System.out.print(i+" \t "); } } }

OUTPUT :1 2 5 7 11 13 17 19 23

6. WRITE A PROGRAM TO INSERT ELEMENT IN EXISTING ARRAY. import java.io.*; class pro6 { public static void main(String args[]) throws IOException { int cnt,last,pos,num; int arr[]=new int[10]; int newarr[]=new int[10]; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter the total number of array : "); last=Integer.parseInt(b.readLine()); for(cnt=0;cnt<last;cnt++) { System.out.print("Enter element"+(cnt+1)+" : "); arr[cnt]=Integer.parseInt(b.readLine()); } System.out.print("Enter the position to insert new element in an existing array :"); pos=Integer.parseInt(b.readLine()); System.out.print("Enter the element : "); num=Integer.parseInt(b.readLine()); for(cnt=0;cnt<last;cnt++) { if(cnt+1==pos) newarr[cnt]=num; else newarr[cnt]=arr[cnt]; } System.out.println("Array after inserting new element is :"); for(cnt=0;cnt<last;cnt++) { System.out.print(newarr[cnt]+" "); } } } OUTPUT :Enter the total number of array : 5 Enter element1 :23 Enter element2 :12 Enter element3 :45 Enter element4 :33 Enter element5 :21 Enter the position to insert new element in an existing array :2 Enter the element : 88 Array after inserting new element is : 23 88 45 33 21 7

7. WRITE A PROGRAM TO SORT EXISTING ARRAY. import java.io.*; class pro7 { public static void main(String args[]) throws IOException { int cnt,last,i,j,temp; int arr[]=new int[10]; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("Enter the total number of array : "); last=Integer.parseInt(b.readLine()); for(cnt=0;cnt<last;cnt++) { System.out.print("Enter element"+(cnt+1)+" : "); arr[cnt]=Integer.parseInt(b.readLine()); } for(i=1;i<=last;i++) { for(j=1;j<=last;j++) { if(arr[j-1]<arr[j]) { temp=arr[j-1]; arr[j-1]=arr[j]; arr[j]=temp; } } } System.out.println("Array after sorting is :"); for(cnt=0;cnt<last;cnt++) { System.out.print(arr[cnt]+" "); } } } OUTPUT :Enter the total number of array : 5 Enter Element1 :23 Enter Element2 :12 Enter Element3 :45 Enter Element4 :33 Enter Element5 :21 Array after sorting is : 45 33 23 21 12 8

8. WRITE A PROGRAM TO CREATE OBJECT FOR TREESET AND STACK AND USE ALL METHODS. import java.io.*; import java.util.*; class pro8a { public static void main(String args[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b =new BufferedReader(a); TreeSet ts=new TreeSet(); System.out.print("\nEnter the total no. of Items : "); int n=Integer.parseInt(b.readLine()); String str=null; for(int cnt=1;cnt<=n;cnt++) { System.out.print("\nEnter the "+cnt+" character : "); str=b.readLine(); ts.add(str); } System.out.println("\n\tThe Item on the tree are : \t"+ts); System.out.println("\n\tThe first item on the tree is : \t"+ts.first()); System.out.println("\n\tThe last item on the tree is : \t"+ts.last()); System.out.println("\n\tTotal no. of item on the tree are : \t"+ts.size()); System.out.print("\nEnter the element you want to delete : "); String del=b.readLine(); ts.remove(del); System.out.println("\n\tThe Item on the tree are : \t"+ts); } } OUTPUT :Enter the total no. of Items :4 Enter the 1 character : we Enter the 2 character : can Enter the 3 character : change Enter the 4 character : live The Item on the tree are : [can, change, live, we] The first item on the tree is :can The last item on the tree is : we Total no. of item on the tree are : 4 Enter the element you want to delete : we The Item on the tree are : [can, change, live]

import java.io.*; import java.util.*; import java.lang.*; class pro8b { public static void main(String args[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b =new BufferedReader(a); Stack st=new Stack(); char ans='y'; while(ans=='y' || ans=='Y') { System.out.println("\n\tMENU FOR STACK OPERATION"); System.out.println("\n\t1.\t PUSH"); System.out.println("\n\t2.\t POP"); System.out.println("\n\t3.\t PEEK"); System.out.println("\n\t4.\t SEARCH"); System.out.println("\n\t5.\t DISPLAY"); System.out.println("\n\t6.\t EXIT"); System.out.print("\n\nEnter your choice : "); int ch=Integer.parseInt(b.readLine()); switch(ch) { case 1: System.out.print("Enter the total number of element to be push : "); int n=Integer.parseInt(b.readLine()); for(int i=1;i<=n;i++) { System.out.print("\n\tEnter the "+i+" Element : "); int val=Integer.parseInt(b.readLine()); st.push(new Integer(val)); } break; case 2: try { System.out.print("\n\tElement poped is "+st.pop()); } catch(Exception e) { System.out.println("\n\tStack is underflow........"); } break; case 3: try { System.out.println("\n\tElement on the top of the Stack is "+(Integer)st.peek()); } 10

catch(Exception e) { System.out.print("\n\tStack is Empty"); } break; case 4: System.out.print("\n\tEnter the element to be searched : "); int num=Integer.parseInt(b.readLine()); int sr=st.search(new Integer(num)); if(sr==-1) System.out.println("\nElemet does not found in the stack"); else System.out.println("\nElement found in the Stack at "+sr+"position"); break; case 5: System.out.println("Elemets of the stack are "); System.out.println(st); break; case 6: System.exit(0); default: System.out.print("Invalid Choice.......Please enter the correct choice"); } System.out.print("Do you want to continue (Y/N) : "); String s=b.readLine(); ans=s.charAt(0); } } } OUTPUT :MENU FOR STACK OPERATION 1. PUSH 2. POP 3. PEEK 4. SEARCH 5. DISPLAY 6. EXIT Enter your choice : 1 Enter the total number of element to be push : 2 Enter the 1 Element : 23 Enter the 2 Element : 45 Do you want to continue(Y/N) : y

11

MENU FOR STACK OPERATION 1. PUSH 2. POP 3. PEEK 4. SEARCH 5. DISPLAY 6. EXIT Enter your choice : 4 Enter the total element to be searched : 55 Element does not found in the stack Do you want to continue(Y/N) : y

MENU FOR STACK OPERATION 1. PUSH 2. POP 3. PEEK 4. SEARCH 5. DISPLAY 6. EXIT Enter your choice :5 Elements of the stack are [23, 45] Do you want to continue(Y/N) : n

12

9. WRITE A PROGRAM TO CHECK ALL THE MATH CLASS FUNCTIONS. import java.io.*; class pro9 { public static void main(String ar[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.println("\n\n\t\t ****** Mathematical Functions ***** \n"); System.out.println("\n\t\t-----------------------------------------\n"); System.out.println("\n\n\t\t\t<== Exponential Functions ==>\n"); System.out.println("\n\t\t\t-----------------------------\n"); System.out.println("\n\tExponential Value = "+Math.exp(3)); System.out.println("\n\tpower Value = "+Math.pow(2,2)); System.out.println("\n\n\t\t\t<== Trignometrical Functions ==>\n"); System.out.println("\n\t\t\t--------------------------------\n"); System.out.println("\n\t sin Value = "+Math.sin(30)); System.out.println("\n\t cos Value = "+Math.cos(30)); System.out.println("\n\t tan Value = "+Math.tan(30)); System.out.println("\n\t cot Value = "+Math.atan(30)); System.out.println("\n\t <== Ordinary Mathematical Functions ==>\n"); System.out.println("\n\t\t\t---------------------------------------\n"); System.out.println("\n\tAbsolute Value = "+Math.abs(-30)); System.out.println("\n\tRound Value = "+Math.abs(10.456)); System.out.println("\n\tCeil Value = "+Math.ceil(10.4)); System.out.println("\n\tFloor Value = "+Math.floor(10.456)); System.out.println("\n\tSquare Root Value = "+Math.sqrt(9)); } } OUTPUT :****** Mathematical Functions ***** ------------------------------------------------------<== Exponential Functions ==> --------------------------------------Exponential Value = 20.085536923187668 Power Value = 4.0 <== Trignometrical Functions ==> -----------------------------------------Sin Value = -0.9880316240928618 Cos Value = 0.15425144988758405 Tan Value = -6.405331196646276 Cot Value = 1.5374753309166493 <== Ordinary Mathematical Functions ==> ---------------------------------------------------Absolute Value = 30 Round Value = 10.456 Floor Value = 10.0 Square Root Value = 3.0 13

10. WRITE A PROGRAM TO EXECUTE ANY WINDOWS 95 APPLICATION (LIKE NOTEPAD CALCULATOR ETC). import java.io.*; class pro10 { public static void main(String ar[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); char ans='y'; while(ans=='y'||ans=='Y') { System.out.println("\n\n\t\t\t ***** Windows95 Application Menu *****\n"); System.out.println("\n\t\t---------------------------------------------------------\n"); System.out.println("\n\n\t\t1. Notepad"); System.out.println("\n\t\t2. Calculator"); System.out.println("\n\t\t3. Exit"); System.out.print("\n\t\tEnter your choice : "); int ch=Integer.parseInt(b.readLine()); switch(ch) { case 1: Runtime.getRuntime().exec("Notepad"); break; case 2: Runtime.getRuntime().exec("calc"); break; case 3: System.exit(0); break; default: System.out.println("\nInvalid Choice "); } System.out.print("\n\tDo you want to continue (Y/N) : "); String s=b.readLine(); ans=s.charAt(0); } } } OUTPUT :***** Windows95 Application Menu *****\ -------------------------------------------------------------------------1. Notepad 2. Calculator 3. Exit Enter your choice :1 14

Do you want to continue (Y/N) : y ***** Windows95 Application Menu *****\ -------------------------------------------------------------------------1. Notepad 2. Calculator 3. Exit Enter your choice :2

Do you want to continue (Y/N) : y ***** Windows95 Application Menu *****\ -------------------------------------------------------------------------1. Notepad 2. Calculator 3. Exit Enter your choice :3

15

11. WRITE A PROGRAM TO FIND THE TOTAL MEMORY, FREE MEMORY AND FREE MEMORY AFTER EXECUTING GARBAGE COLLECTION (gc()). import java.io.*; class pro11 { public static void main(String ar[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); Runtime rn=Runtime.getRuntime(); long mem1=0,mem2=0; Integer arr[]=new Integer[1000]; System.out.println("\n\t\tTotal Memory is "+rn.totalMemory()); mem1=rn.freeMemory(); System.out.println("\n\tInital Free Memory is "+mem1); rn.gc(); mem1=rn.freeMemory(); System.out.println("\n\tFree Memory after Garbage Collector "+mem1); for(int i=0;i<1000;i++) arr[i]=new Integer(i); mem2=rn.freeMemory(); System.out.println("\n\tFree Memory after allocation "+mem2); System.out.println("\n\tMemory used by alloction "+ (mem1-mem2)); for(int i=0;i<1000;i++) arr[i]=null; rn.gc(); mem2=rn.freeMemory(); System.out.println("\n\tFree Memory after collecting discarded integers "+mem2); } } OUTPUT :Total Memory is 5177344 Initial Free Memory is 4949960 Free Memory after Garbage Collector 5039128 Free Memory after allocation 5020712 Memory used by allocation 18416 Free Memory after collecting discarded integer 5039128

16

12. WRITE A PROGRAM TO COPY A FILE TO ANOTHER FILE USING JAVA.IO PACKAGE CLASSES. GET THE FILE NAMES AT RUN TIME AND IF THE TARGET FILE EXISTED THEN ASK CONFIRMATION TO OVERWRITE AND TAKE NECESSARY ACTIONS. import java.lang.*; import java.io.*; class pro12 { public static void main(String ar[]) throws IOException { InputStreamReader a =new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); int i; FileInputStream fin=null; FileOutputStream fout=null; String filename=null; try { System.out.print("\n\tEnter the file name to copy to another file : "); filename=b.readLine(); try { fin=new FileInputStream(filename); } catch(Exception e) { System.out.println("\n\tFile Not Found"); return; } try { System.out.print("\n\tEnter the file name to copy : "); filename=b.readLine(); fout=new FileOutputStream(filename); } catch(Exception e) {} } catch(Exception e) {} try { do { i=fin.read(); if(i!=-1) fout.write(i); 17

}while(i!=-1); System.out.println("\n\tFile copied to "+filename+" Sucessfully"); } catch(IOException e) { System.out.println("\n\tFile Error"); } fin.close(); fout.close(); } }

OUTPUT :Enter the file name to copy to another file : c:\test.txt Enter the file name to copy : c:\hello.txt File copied to c:\hello.txt successfully Enter the file name to copy to another file : c:\tests.txt File Not Found

18

13. WRITE A PROGRAM TO GET A FILE NAME AT RUN TIME AND DISPLAY NUMBER OF LINES AND WORDS IN THAT FILE. import java.lang.*; import java.io.*; class pro13 { public static void main(String ar[]) throws IOException { InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); int lines=0,words=0; System.out.print("\n\tEnter The File Name : "); String s=b.readLine(); File f=new File(s); InputStreamReader in=new InputStreamReader(new FileInputStream(f)); StreamTokenizer st=new StreamTokenizer(in); st.wordChars(0,255); st.whitespaceChars(0,' '); st.eolIsSignificant(true); while(st.nextToken()!= st.TT_EOF) { if(st.ttype==st.TT_EOL) lines++; if(st.ttype==st.TT_WORD) words++; } System.out.println("\n\tNumber of Lines : "+lines+"\n\t Number of words : "+words); } } OUTPUT :Enter The File Name : c:\test.txt Number of Lines : 0 Number of Words : 2

Enter The File Name : c:\hook.log Number of Lines : 238 Number of Words : 357 19

14. WRITE A PROGRAM TO LIST FILES IN THE CURRENT WORKING DIRECTORY DEPENDING UPON A GIVEN PATTERN. import java.lang.*; import java.io.*; class pro14 { public static void main(String ar[]) throws IOException { InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.print("\n\tEnter the Directory Name : "); String s=b.readLine(); File f=new File(s); if(f.exists()) { if(f.isDirectory()) { String []f1=f.list(); int i; for(i=0;i<f1.length;i++) { String tp=f1[i]; if(tp.endsWith("java")) System.out.println("\n\t\t"+tp); if(tp.endsWith("class")) System.out.println("\n\t\t"+tp); } } } } } OUTPUT :Enter The Directory Name : d:\ Test.java Test.class Abc.java Abc.class Applet2.java

20

15. CREATE A TEXT FIELD THAT ALLOWS NUMERIC VALUE AND IN SPECIFIED LENGTH. //<applet code=pro15 width=400 height=400></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class pro15 extends Applet implements ActionListener { Frame f=new Frame("Testing Numeric Value"); Label l1=new Label("Enter Number "); Label res=new Label(); TextField t1=new TextField(15); Button b1=new Button("TEST"); Panel p=new Panel(); public void init() { add(l1); add(t1); add(b1); add(res); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { int i,f=0; StringBuffer sb=new StringBuffer(t1.getText()); for(i=0;i<t1.getText().length();i++) { if(sb.charAt(i)<'0' || sb.charAt(i)>'9') f=1; } if(i<4 || i>6) res.setText("Number should be of length four(4) or lessthan six(6) "); else if(f==1) res.setText("Enter only Numeric values "); else res.setText("Number = "+t1.getText()); } } }

21

OUTPUT :-

22

16. CREATE A FRAME WITH TWO LABELS AT RUNTIME DISPLAY X AND Y COORDINATES OF MOUSE POINTER IN THE LABELS. //<applet code=pro16 width=400 height=400></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class pro16 extends Applet implements MouseMotionListener { public int mousex=0,mousey=0; Label x=new Label(); Label y=new Label(); public void init() { add(x); add(y); addMouseMotionListener(this); } public void start() { } public void mouseDragged(MouseEvent me) { } public void mouseMoved(MouseEvent me) { mousex=me.getX(); mousey=me.getY(); x.setText("X = "+me.getX()); y.setText("Y = "+me.getY()); showStatus("Moving mouse at x= "+me.getX()+" Y= "+me.getY()); } } OUTPUT :-

23

17. CREATE A FRAME AND CHECKBOX GROUP WITH FIVE CHECKBOX WITH LABEL AS GREEN, RED, BLUE, YELLOW AND WHITE. AT RUN TIME CHANGE THE BACKGROUND COLOR WITH APPROPRIATE SELECTION OF THE CHECKBOX. //<applet code=pro17 width=400 height=400></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class pro17 implements ItemListener { Checkbox R,G,B,Y,W; CheckboxGroup cbg; Frame f =new Frame("Color Testing "); Panel p=new Panel(); public pro17() { cbg=new CheckboxGroup(); R=new Checkbox("RED",cbg,false); G=new Checkbox("GREEN",cbg,false); B=new Checkbox("BLUE",cbg,false); Y=new Checkbox("YELLOW",cbg,false); W=new Checkbox("WHITE",cbg,false); f.add(p); p.add(R); p.add(G); p.add(B); p.add(Y); p.add(W); f.setSize(400,400); f.show(); R.addItemListener(this); G.addItemListener(this); B.addItemListener(this); Y.addItemListener(this); W.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { if(cbg.getSelectedCheckbox().getLabel()=="RED") p.setBackground(Color.red); else if(cbg.getSelectedCheckbox().getLabel()=="GREEN") p.setBackground(Color.green); else if(cbg.getSelectedCheckbox().getLabel()=="BLUE") p.setBackground(Color.blue); else if(cbg.getSelectedCheckbox().getLabel()=="YELLOW") p.setBackground(Color.yellow); else if(cbg.getSelectedCheckbox().getLabel()=="WHITE") p.setBackground(Color.white); else 24

{} } public static void main(String args[]) { Pro17 obj=new pro17(); } }

OUTPUT :-

25

18. CREATE A FRAME, CHOICE AND LABEL. ADD FIVE ITEMS IN THE CHOICE. AT RUNTIME DISPLAY SELECTED ITEMS OF CHOICE IN THE LABEL. //<applet code=pro18 width=400 height=400></applet> import java.awt.*; import java.awt.event.*; import java.applet.*; public class pro18 extends Applet implements ItemListener { Choice ch; Label L; String msg=" "; Frame f; public void init() { f=new Frame(); ch=new Choice(); ch.add("INDIA"); ch.add("PAKISTAN"); ch.add("SRILANKA"); ch.add("AUSTRALIA"); ch.add("ENGLAND"); add(ch); ch.addItemListener(this); L=new Label("Choice Selected "); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Choice Selected : "; msg+=ch.getSelectedItem(); g.drawString(msg,60,100); } }

26

OUTPUT :-

27

19. CREATE A FRAME WITH THREE SCROLLS; CHANGE THE BACKGROUND COLOR OF THE FRAME USING FUNCTION WITH VALUE OF SCROLLS. //<applet code=pro19 width=400 height=400></applet> import java.awt.*; import java.awt.event.*; import java.applet.*; public class pro19 extends Applet implements AdjustmentListener { Frame f; Scrollbar red,blue,green; int i=0,r=0,b=0,gr=0; public void init() { f=new Frame("Scroll and changing frame background color"); f.setVisible(true); f.setSize(200,200); red=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256); blue=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256); green=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256); add(red); add(blue); add(green); red.addAdjustmentListener(this); blue.addAdjustmentListener(this); green.addAdjustmentListener(this); } public void adjustmentValueChanged(AdjustmentEvent ae) { repaint(); } public void paint(Graphics g) { r=red.getValue(); b=blue.getValue(); gr=green.getValue(); Color c=new Color(r,b,gr); g.setColor(c); f.setBackground(c); } }

28

OUTPUT :-

29

20. CREATE A MESSAGE DIALOG BOX LIKE WINDOWS 95 MESSAGE BOX. AT RUNTIME PASS MESSAGE FOR THAT MESSAGE BOX. import javax.swing.*; import java.awt.*; import java.awt.event.*; class messagedialog extends Frame implements ActionListener { TextField t1=new TextField(20); Button b1=new Button("Click Here"); Panel p=new Panel(); messagedialog() { add(p); p.add(t1); p.add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str = t1.getText(); JOptionPane.showMessageDialog(this,str); } public static void main(String ar[]) { messagedialog obj = new messagedialog(); obj.setSize(400,400); obj.setVisible(true); } } OUTPUT :-

30

You might also like