Você está na página 1de 61

Java particle record

Q1. Write a program that implements the concept of Encapsulation?

2011

/****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\encapsulation.java System No : L1S1 Date : 18/08/09 ******************************************************************/

Coding:
class encapsulation //Class Declaration { public void print() //Print Method Defined { System.out.println("JAVA PROGRAM FOR ENCAPSULATION"); } public static void main(String[] args) //Main Method { int i=5, j=6; //Variable Declaration System.out.println("When i = 5 & j = 6 then i + j = " + i+j + "."); encapsulation gul =new encapsulation(); gul.print(); } } Output :

Nitesh Bhura

Java particle record

2011

Q2. Write a program to demonstrate concept of polymorphism (Overloading and Overridden)? /****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\polymorphism.java System No : L1S7 Date : 19-08-2010 ******************************************************************/

Coding:
import java.io.DataInputStream; //Package Declaration class polymorphism //Class Declaration { static DataInputStream x = new DataInputStream(System.in); public static void main(String[] args) //Main Method { System.out.print("Enter 1st no. = "); //To Enter a no. try //Try Exception Starts { int i = Integer.parseInt(x.readLine()); System.out.print("\nEnter 2nd no. = "); int j = Integer.parseInt(x.readLine()); sub s = new sub(); //creating object of class sub String s1 = new String(); String s2 = new String(); //1st string declaration //2nd string declaration

System.out.print("\nEnter 1st String = "); s1 = x.readLine(); System.out.print("\nEnter 2nd String = "); s2 = x.readLine(); //overloading s.add(i,j); //passing int val s.add(s1,s2); //passing string val
Nitesh Bhura 2

Java particle record


} catch(Exception a) {} } } class sub //Sub Class Declared { public void add (int a,int b) //Method Declaration { System.out.print("\nAddition of entered number's = " + (a+b)); } public static void add (String a, String b) { System.out.print("\n\nAddition of entered string's = " + (a+b)); } }; Output :

2011

Nitesh Bhura

Java particle record

2011

Q3. Write a program the use Boolean data type and print the prime number series up to 50? /****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\prime.java System No : L1S7 Date : 19-08-2010 ******************************************************************/

Coding:
class prime //Class Declaration { public static void main(String[] args) //Main Method { boolean b; //Variable Declaration System.out.println("Prime Number's in between 1 to 30 :"); for (int i=1;i<=30 ;i++ ) //For Loop Starts { b = false; for (int j=2;j<i ;j++ ) //Inner For Loop { if (i % j == 0) //If Statement { b = true; break; } } if (b == false) { System.out.println(+ i); //Print Statement } } } }
Nitesh Bhura 4

Java particle record


Output :

2011

Nitesh Bhura

Java particle record

2011

Q4. Write a program for matrix multiplication using I/O stream?


/****************************************************************** Unit No. :4 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ multiplication_of_matrix.java System No : L1S7 Date : 19-08-2010 ******************************************************************/

Coding:
import java.io.DataInputStream; //Package Declaration class multiplication_of_matrix //class Decalaration { static DataInputStream x= new DataInputStream(System.in); public static void main(String[] args) //Main method { int n1[][]=new int [3][3]; //1st array declaration int n2[][]=new int [3][3]; //2nd array declaration try { System.out.println("enter value of 1st matrix : "); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { n1[i][j]=Integer.parseInt(x.readLine()); //Entering value of 1st matrix n1 } } System.out.println("enter value of 2nd matrix : "); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { n2[i][j]=Integer.parseInt(x.readLine()); //Entering value of 2nd matrix n2 } } }
Nitesh Bhura 6

Java particle record


catch(Exception e) //Catch Exception Starts {} System.out.println("1st matrix is = \n"); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { System.out.print(n1[i][j] + "\t"); //showing value of 1st matrix n1 } System.out.println(); } System.out.println("2nd matrix is = \n"); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { System.out.print(n2[i][j] + "\t"); //showing value of 2nd matrix n2 } System.out.println(); } System.out.println("multiplication of both matrix = \n"); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { int total = 0; for (int mul=0;mul<=2 ;mul++ ) { total= total + n1[i][mul] * n2[mul][j] ; //multilication of both matrix } System.out.print(total + "\t"); } System.out.println();
Nitesh Bhura

2011

Java particle record


} } }

2011

Output :

Nitesh Bhura

Java particle record

2011

Q5. WAP to add the element of Vector as arguments of main method (Run time) and rearrange them, and copy it into an Array? /****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ System No : L1S7 Date : 25-08-2010 ******************************************************************/

Coding: import java.util.*; import java.io.*; import java.lang.*; class vector { public static void main(String args[]) throws IOException { Vector v = new Vector(); DataInputStream in = new DataInputStream(System.in); int i,n,s; String g; System.out.println("Enter 5 element in vector : "); for(i=0;i<5;i++) { g=in.readLine(); v.addElement(g); } s=v.size(); String a[]=new String[s]; System.out.println("size is="+s); v.copyInto(a);
Nitesh Bhura 9

Java particle record


System.out.println("print element"); for(i=0;i<5;i++) { System.out.println(a[i]); } } } Output:-

2011

Nitesh Bhura

10

Java particle record

2011

Q6. Write a program to check that the given string is palindrome or not?
/****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\palindrome.java System No : L1S7 Date : 25-08-2010 ******************************************************************/

Coding:
import java.io.*; import java.lang.*; //Package Declaration class palindrome //Class Daclaration { static DataInputStream x = new DataInputStream(System.in); public static void main(String[] args) { System.out.print("Enter a string to Check weather it is palindrome or not : "); String str = new String(); String str1 = new String(); try //Try Exception Starts { str = x.readLine().trim(); //To Enter a string int len = str.length(); int a=0,total = 0; for (int i=len -1;i>=0 ;i-- ) //For loop { if (str.charAt(i) == str.charAt(0+a)) { total+=1; } a++; } if (total == len) //If else Statement
Nitesh Bhura 11

Java particle record


{ System.out.println(" The Entered string "+str+" is a palindrom "); } else { System.out.println("Entered string "+str+" is not a palindrom "); } } catch(Exception a) //Catch Exception Starts { } } }

2011

Nitesh Bhura

12

Java particle record

2011

Output:-

Nitesh Bhura

13

Java particle record

2011

Q7. Write a program to arrange the string in alphabetical order?


/****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\sort.java System No : L1S7 Date : 27-08-2010 ******************************************************************/

Coding:
import java.io.DataInputStream; //Package Declaration class sort //Class Declaration { static DataInputStream x= new DataInputStream(System.in); public static void main(String[] args) { try //Try Exception Starts { String str[] = new String[50]; String temp = null ; System.out.print("How many Strings you wants to enter :> "); int n= Integer.parseInt(x.readLine()); for (int i=0;i<n;i++ ) //For loop starts { System.out.print("\nEnter String no. " + (i+1) +" :> "); str[i] = x.readLine(); } for (int j=0; j<n ;j++ ) //For loop starts { for (int i=0;i<n-1;i++ ) //For Loop Starts { if (str[i].compareTo(str[i+1]) > 0) //If Statement { temp = str[i];
Nitesh Bhura 14

Java particle record


str[i] = str[i+1] ; str[i+1] = temp; } } } System.out.print("\nSorted Strings are ::\n"); for (int i=0;i<n;i++ ) //For loop Starts { System.out.println(i+1 + ") " + str[i]); } } catch(Exception a ) { } } } Output :

2011

Nitesh Bhura

15

Java particle record

2011

Q.8 WAP for string Buffer Class which perform the all method of that class?
/****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\stringBuffer.java System No : L1S7 Date : 27-08-2010. ******************************************************************/

Coding:
class stringBuffer //Class Declaration { public static void main(String args[]) //Main Method { StringBuffer str=new StringBuffer("Nitesh Bhura"); System.out.println("original string : "+str); //obtaining length System.out.println("Length of string" +str.length()); //Accessing characters in a string for (int i=0;i<str.length();i++) { int p=i+1; System.out.println("Character at position : " + p + " } //Inserting string in the middle String aString = new String(str.toString()); int pos=aString.indexOf(" Bhura"); str.insert(pos," Nitesh"); System.out.println("Modified string "+ str); //Modifying characters
Nitesh Bhura 16

is " + str.charAt(i));

Java particle record


str.setCharAt(9, ' '); System.out.println("String now : " + str); //Appending a string at the end str.append(" is my name."); System.out.println("Appending string : " +str); } }

2011

Output :

Nitesh Bhura

17

Java particle record

2011

Q9. Write a program to calculate simple interest using the Wrapper class?
/****************************************************************** Unit No. :1 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ Simple_Interest.java System No : L1S7 Date : 27-09-2010 ******************************************************************/

Coding:
import java.io.DataInputStream; //Package Declaration class Simple_Interest //class declaration { static DataInputStream x = new DataInputStream(System.in); public static void main(String[] args) { try //Try Exception Starts { float p,r,t; System.out.print("Enter Principal :> "); p = Float.parseFloat(x.readLine());//Input Principal System.out.print("Enter Rate :> "); r = Float.parseFloat(x.readLine());

//Input Rate

System.out.print("Enter Time in month :> "); t = Float.parseFloat(x.readLine()); //Input Time System.out.print("Simple Interest :> " + ((p * r * t)/100)); //Output Simple Interest } catch(Exception a) //Catch Exception { } } }
Nitesh Bhura 18

Java particle record


Output :

2011

Nitesh Bhura

19

Java particle record

2011

Q10. Write a program to calculate area of various geometrical figures using the abstract class?
/****************************************************************** Unit No. :2 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\area.java System No : L1S7 Date : 30-09-2010 ******************************************************************/

Coding:
abstract class figure //Figure Abstract class Declaration { double dim1; double dim2; //Variable Declarations figure(double a,double b) //Parameter Constructor { dim1 = a; dim2 = b; } abstract double area(); } class rectangle extends figure //Rectangle class Declaration { rectangle(double a,double b) //Constructor Declared { super(a,b); } double area() //Method for Area calculation { System.out.println("Inside area of Rectangle."); return dim1 * dim2; } }
Nitesh Bhura 20

Java particle record


class triangle extends figure //Triangle class Declared { triangle(double a,double b) //constructor Declared { super(a,b); } double area() //Method Declared { System.out.println("Inside area of Triangle."); return dim1 * dim2/2; } } class area //Main class Declaration { public static void main(String[] args) //main method { rectangle r = new rectangle(8,9); //objects Declared triangle t = new triangle(2,2); figure f; f = r; System.out.println("Area is :> " + f.area()); f = t; System.out.println("Area is :> " + t.area()); } } Output :

2011

Nitesh Bhura

21

Java particle record

2011

Q11. Write a program where single class implements more than one interfaces and with help of interface reference variables user call the method?
/****************************************************************** Unit No. :2 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\main.java System No : L1S7 Date : 04-09-2010 ******************************************************************/

Coding:
class main //Main class Declared { public static void main(String[] args) //Main class { sub a = new sub(); //creating object for interface 1st a.sub11(6); //passing value to interface 1st sub b = new sub(); //creating object for interface 2nd b.sub22(8); //passing value to interface 2nd } } class sub implements sub1,sub2 //class that implements interface { public void sub11(int p) { System.out.print("In 1st interface "); System.out.println("P = " + p); } public void sub22(int p) { System.out.print("In 2nd interface "); System.out.println("P = " + p * p);
Nitesh Bhura 22

Java particle record


} } interface sub1 { void sub11(int y); } interface sub2 { void sub22(int y); } //interface 1st //calling the mathod sub1 in class sub

2011

//interface 2nd //calling the mathod sub2 in class sub

Output :

Nitesh Bhura

23

Java particle record

2011

Q12. Write a program that uses multiple catch statements within the try-catch mechanism?
/****************************************************************** Unit No. :3 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ multiple_exception.java System No : L1S7 Date : 08-09-2010. ******************************************************************/

Coding:
class NewThread implements Runnable { String name; Thread t;

NewThread(String threadname) { name=threadname; t=new Thread(this, name); System.out.println("New thread = "+t); t.start(); } public void run() { try { for (int i=5;i>0 ;i-- ) { System.out.println(name+" : "+i); Thread.sleep(1000); } }
Nitesh Bhura 24

Java particle record


catch (InterruptedException e) { System.out.println(name+ "interrupted."); } System.out.println(name+"exiting"); } } class DemoJoin { public static void main(String[] args) { NewThread ob1 = new NewThread ("One"); NewThread ob2 = new NewThread ("Two"); NewThread ob3 = new NewThread ("Three"); System.out.println("Thread One Is alive() : "+ob1.t.isAlive()); System.out.println("Thread One Is alive() : "+ob2.t.isAlive()); System.out.println("Thread One Is alive() : "+ob3.t.isAlive()); try { System.out.println("waiting for threads to finish"); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { System.out.println("main thread is interrupted"); } System.out.println("Thread one is alive()"+ob1.t.isAlive()); System.out.println("Thread one is alive()"+ob2.t.isAlive()); System.out.println("Thread one is alive()"+ob3.t.isAlive()); System.out.println("Main thread existing"); } }

2011

Nitesh Bhura

25

Java particle record


OUTPUT:-

2011

Q13. Write a program where user will create a self Exception using

the throw keyword?


Nitesh Bhura 26

Java particle record

2011

/****************************************************************** Unit No. :3 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\NeoRocks.java System No : L1S7 Date : 08-09-2010. ******************************************************************/

Coding:
class TestMyException { public static void main(String[] args) { int x=5,y=5000; try { float z=(float) x / (float) y; if(z<0.01) { throw new MyException("Number is too Small"); } } catch (MyException e) { System.out.println("Caught My Exception"); System.out.println(e.getMessage()); } finally { System.out.println("WELCOME TO JAVA WORLD!"); } } }

Output :

Nitesh Bhura

27

Java particle record

2011

Q14. Write a program to create a package using command and one package will import another package?
Nitesh Bhura 28

Java particle record

2011

/****************************************************************** Unit No. :2 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ mainclass.java System No : L1S7 Date : 10-09-2010 ******************************************************************/

Coding:
//Main program package test; public class classa { public void disp(int n) { int x,fact=1; for (x=1;x<=n ;x++ ) { fact=fact*x; } System.out.println("Factorial : "+fact); } }
--------------------------------------------------------------------------------------------------------------

//calling the package import test.classa; class trial { public static void main(String[] args) { classa A=new classa(); A.disp(5); }
Nitesh Bhura 29

Java particle record


}

2011

Output:

Q15. WAP for multithread using theisAlive ( ), join ( ), Synchronized ( ) method of Thread class?
Nitesh Bhura 30

Java particle record

2011

/****************************************************************** Unit No. : 03 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ karamkey.java System No : L1S7 Date : 11-01-2011 ******************************************************************/

Coding:
class NewThread implements Runnable { String name; Thread t;

NewThread(String threadname) { name=threadname; t=new Thread(this, name); System.out.println("New thread = "+t); t.start(); } public void run() { try { for (int i=5;i>0 ;i-- ) { System.out.println(name+" : "+i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name+ "interrupted.");
Nitesh Bhura 31

Java particle record


} System.out.println(name+"exiting"); } } class DemoJoin { public static void main(String[] args) { NewThread ob1 = new NewThread ("One"); NewThread ob2 = new NewThread ("Two"); NewThread ob3 = new NewThread ("Three"); System.out.println("Thread One Is alive() : "+ob1.t.isAlive()); System.out.println("Thread One Is alive() : "+ob2.t.isAlive()); System.out.println("Thread One Is alive() : "+ob3.t.isAlive()); try { System.out.println("waiting for threads to finish"); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { System.out.println("main thread is interrupted"); } System.out.println("Thread one is alive()"+ob1.t.isAlive()); System.out.println("Thread one is alive()"+ob2.t.isAlive()); System.out.println("Thread one is alive()"+ob3.t.isAlive()); System.out.println("Main thread existing"); } } Output :

2011

Nitesh Bhura

32

Java particle record

2011

Q16. Write a program for Applet that handles the keyboard event?
/******************************************************************
Nitesh Bhura 33

Java particle record

2011

Unit No. : 05 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ karamkey.java System No : L1S7 Date : 11-01-2011 ******************************************************************/

Coding:
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code = simpelkey .class width=400 height=400> </applet> */ public class simpelkey extends Applet implements KeyListener { String msg="Hellow "; int x=10,y=20; public void init() { addKeyListener(this); requestFocus(); } public void keyPressed(KeyEvent ke) { showStatus("KeyDown"); } public void keyReleased(KeyEvent ke) { showStatus("Keyup"); } public void keyTyped(KeyEvent ke) {

Nitesh Bhura

34

Java particle record


msg+=ke.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg,x,y); } }

2011

Nitesh Bhura

35

Java particle record


Output :

2011

Nitesh Bhura

36

Java particle record

2011

Q17. Write a program for JDBC to insert the values into the existing table by using prepares statement?
/****************************************************************** Unit No. :4 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ MyDbTest1.java System No : L1S7 Date : 23-01-2011 ******************************************************************/

Coding:
import java.lang.*; import java.sql.*; import java.io.*; //Package Declarations class MyDbTest1 //Class Declared { public static void main(String[] args) throws Exception { String Eno,EName; Connection con=null; PreparedStatement stmt; ResultSet Rs; int x; //Variables Declared try //Try Exception Statement { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:MYDSN","scott","tiger");

//Entering records into the emp stmt=con.prepareStatement("insert into emp(empno,ename) values (113,'Naveen')"); x=stmt.executeUpdate();

Nitesh Bhura

37

Java particle record

2011

//Updating records of the emp stmt=con.prepareStatement("update emp set ename='Aditi' where empno=111"); x=stmt.executeUpdate();

//Now reading the records of the emp stmt=con.prepareStatement("Select * from emp"); Rs=stmt.executeQuery(); while(Rs.next()) { Eno=Rs.getString(1); EName=Rs.getString(2); System.out.println(Eno+" "+EName); } stmt.close(); con.close(); } catch(Exception e) //Catch Exception { System.out.println("Exception is : "+e.getMessage());//Print Statement } } }

Nitesh Bhura

38

Java particle record


Output :

2011

Nitesh Bhura

39

Java particle record

2011

Q18. Write a program for JDBC to display the record form the existing table?
/****************************************************************** Unit No. : 4 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ MyDbTest.java System No : L1S7 Date : 23-01-2011 ******************************************************************/

Coding:
import java.lang.*; import java.sql.*; import java.io.*; //Package Declarations class MyDbTest //Declared Class { public static void main(String[] args) throws Exception { String Eno,EName; Connection con=null; PreparedStatement stmt; ResultSet Rs; //Variable Declarations try //Try Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:MYDSN","s cott","tiger"); stmt=con.prepareStatement("Select * from emp"); Rs=stmt.executeQuery(); while(Rs.next()) { Eno=Rs.getString(1); EName=Rs.getString(2);
Nitesh Bhura 40

Java particle record


System.out.println(Eno+" } "+EName);

2011

stmt.close(); con.close(); } catch(Exception e) //Catch Exception Statement { System.out.println("Exception is : "+e.getMessage()); //Print Statement } } }

Output :

Nitesh Bhura

41

Java particle record

2011

Q19. Write a program to demonstrate the border layout using applet?


/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ BorderLayoutDemo.java System No : L1S7 Date : 16-01-2011 ******************************************************************/

Coding:
import java.awt.*; import java.applet.*; //Packages Declararions import java.util.*; /* <applet code="BorderLayoutDemo" width=400 height=200> </applet> */ //HTML Coding public class BorderLayoutDemo extends Applet { public void init() //Method declared { setLayout(new BorderLayout()); add(new Button("this is across the top."),BorderLayout.NORTH); add(new Label("the footer message might go here.") ,BorderLayout.SOUTH); add(new Button("Right"),BorderLayout.EAST); add(new Button("Left"),BorderLayout.WEST);

Nitesh Bhura

42

Java particle record

2011

String msg="The six colors " + ", including the white background;\n" + "represents the colours of all the World's flags..." + "this is a true \n" + "international emblem. " + " - Peirre de Coubertin\n\n"; //To insert text in form add(new TextArea(msg),BorderLayout.CENTER); } }

Output :

Nitesh Bhura

43

Java particle record

2011

Q20. Write a program for Applet who generates the mouse motion listener event?
/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ App.java System No : L1S7 Date : 18-01-2011 ******************************************************************/

Coding:
import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code =freedraw.class width=400 height=400> </applet> */ public class freedraw extends Applet implements MouseMotionListener { int x,y; public void init() { setLayout(new GridLayout(5,2)); x=0; y=0; addMouseMotionListener(this); } public void mouseDragged(MouseEvent me) { x=me.getX(); y=me.getY(); repaint(); } public void mouseMoved(MouseEvent me) { } public void update(Graphics g)
Nitesh Bhura 44

Java particle record


{ paint(g); } public void paint (Graphics g) { g.fillOval(x,y,5,5); } }

2011

Output:

Nitesh Bhura

45

Java particle record

2011

Q21. Write a program for display the checkbox, labels and text fields on an AWT?
/****************************************************************** Unit No. : 5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ Karamyo.java System No : L1S7 Date : ******************************************************************/

Coding:
import java.awt.*; import java.applet.*; /*<applet code=label.class height=400 width=400></applet>*/ public class label extends Applet { CheckboxGroup cb; Checkbox cb1,cb2; List L; TextField p,q; TextArea ta; Choice c; public void init() { //creating label obj Label x=new Label("CCJ"); Label y=new Label("JDP"); add(x); add(y); //creating combo obj and elements to combo c=new Choice(); c.add("BCA");
Nitesh Bhura 46

Java particle record


c.add("BBA"); c.add("BSC"); c.add("MA"); c.add("BA"); c.add("PHD"); add(c); //creating option button cb=new CheckboxGroup(); cb1=new Checkbox ("Graduate",cb,true); cb2=new Checkbox ("Post Graduate",cb,false); add(cb1); add(cb2); //creating list box L=new List(1,true); L.add("BCA"); L.add("PGDCA"); L.add("BBA"); L.add("M.COM"); L.add("B.COM"); L.add("BA"); add(L); //creating textbox and textfeilds p=new TextField(10); q=new TextField(10); q.setEchoChar('*'); add(p); add(q);

2011

//creating text area String s="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; ta=new TextArea(s,10,30); add(ta); } }


Nitesh Bhura 47

Java particle record


OUTPUT:

2011

Nitesh Bhura

48

Java particle record


file.(Using the file Writer IO stream)?

2011

Q22. Write a program for creating a file and to store data into that

/****************************************************************** Unit No. : 4 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ System No : L1S7 Date : 22-04-2010 ******************************************************************/

Coding: import java.io.*; //Package Declaration class copycharacters //Main class declared { public static void main(String[] args) //Main Method { //Declare ad create input and output files File inFile = new File("input.dat"); File outFile = new File("output.dat"); FileReader ins =null; FileWriter outs=null; try //Try Exception { ins = new FileReader("inFile"); outs=new FileWriter("outFile"); //Read and write till the end int ch; while ((ch=ins.read())!=-1) { outs.write(ch); }
Nitesh Bhura 49

Java particle record


} catch (IOException e) //Catch Exception { System.out.println("File created"); System.out.println("NiteshIoFile"); System.exit(-1); }

2011

finally { try { ins.close(); outs.close(); } catch(IOException e) { } } } }


Output :

//close files

Nitesh Bhura

50

Java particle record

2011

Q23. Write a program to create an Applet using the HTML file, where Parameter pass font size and font type and Applet msg. will change to corresponding parameter?
/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ PassParam.java System No : L1S7 Date : 16-01-2011 ******************************************************************/

Coding:
import java.awt.*; import java.applet.*; // Package Declarations /* <applet code = "PassParam" width =200 height = 80> <param name=fontName value=Karamveer> <param name=fontSize value=18> <param name=leading value=4> <param name=accountEnabled value=true> </applet>*/ //Applet Coding in HTML public class PassParam extends Applet //Class Declaration { String fontName; //Variable Declarations int fontSize; float leading; boolean active; public void start() //Start Mathod { String Param; //Variable Declaration fontName = getParameter("fontName"); if (fontName == null)
Nitesh Bhura

//if Statement
51

Java particle record


fontName = "Not Found"; Param = getParameter ("FontSize"); try //Try catch Exception Statement { if (Param !=null ) //if else statement fontSize = Integer.parseInt (Param); else fontSize = 0 ; } catch (NumberFormatException E) { fontSize = -1; } Param = getParameter("Leading"); try //try catch Exception { if (Param != null) //if else statement leading = Float.valueOf (Param).floatValue(); else leading = 0; } catch (NumberFormatException e) { leading =-1; } Param = getParameter("Account Enabled"); if (Param != null) active = Boolean.valueOf (Param).booleanValue(); } public void paint(Graphics G) //Paint Method { G.drawString("FontName = " + fontName,0,10); G.drawString("FontSize = " + fontSize,0,26); G.drawString("Leading = " + leading,0,42); G.drawString("AccountActive = " + active,0,58);
Nitesh Bhura

2011

52

Java particle record


} }

2011

Output :

Nitesh Bhura

53

Java particle record

2011

Q24. Write a program for AWT to create Menu and Popup Menu for Frame?
/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ MenuDemo.java System No : L1S7 Date : 02-01-2011 ******************************************************************/

Coding:
import java.awt.*; import javax.swing.*; //Importing Packages and classes import javax.swing.event.*; public class MenuDemo extends JFrame //class name { public MenuDemo() //Constructor Declaration { JMenuBar mb1=new JMenuBar(); //Declare he object and Instantiate the object setJMenuBar(mb1); JMenu m1=new JMenu("File"); //Statement for File menu JMenu m2=new JMenu("Edit"); //Statement for Edit menu JMenu m3=new JMenu("Search"); //Statement for Search menu JTextArea ta=new JTextArea(); mb1.add(m1); mb1.add(m2); mb1.add(m3); m1.add(new JMenuItem("New",new ImageIcon("new.jpg"))); //Popup for File menu m1.add(new JMenuItem("Open",new ImageIcon("open.jpg"))); m1.add(new JMenuItem("Save",new ImageIcon("save.jpg")));
Nitesh Bhura 54

Java particle record

2011

m1.addSeparator(); //Inserts line to separate m1.add(new JMenuItem("Page Setup",new ImageIcon("pagesetup.jpg"))); m1.add(new JMenuItem("Print",new ImageIcon("print.jpg"))); m2.add(new JMenuItem("Undo",new ImageIcon("Undo.jpg"))); //Popup for Edit menu m2.add(new JMenuItem("Redo",new ImageIcon("redo.jpg"))); m2.addSeparator(); //Inserts line m2.add(new JMenuItem("Cut",new ImageIcon("cut.jpg"))); m2.add(new JMenuItem("Copy",new ImageIcon("copy.jpg"))); m2.add(new JMenuItem("Paste",new ImageIcon("paste.jpg"))); m3.add(new JMenuItem("Find",new ImageIcon("find.jpg"))); //Popup for Search menu this.getContentPane().add(ta); } //Closing Constructor public static void main(String str[]) // Main emthod Declaration { MenuDemo md1=new MenuDemo(); // Calling of MenuDemo class md1.setVisible(true); md1.setSize(300,300); }

Nitesh Bhura

55

Java particle record


Output :

2011

Nitesh Bhura

56

Java particle record

2011

Q24. WAP to create an Applet using the HTML file where parameter pass for font size and font type and applet message will change to corresponding parameters ?
/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ MenuDemo.java System No : L1S7 Date : 02-01-2011 ******************************************************************/

Coding:
import java.awt.*; import java.applet.*; /* <applet code = PassParam.class width =300 height = 300> <param name=fontName value=Lucida> <param name=fontSize value=14> <param name=leading value=2> <param name=accountEnabled value=true> </applet>*/ public class PassParam extends Applet { String fontName; int fontSize; float leading; boolean active; public void start() { String Param; fontName = getParameter("fontName"); if (fontName == null) fontName = "Not Found";
Nitesh Bhura 57

Java particle record


Param = getParameter ("FontSize"); try { if (Param !=null ) fontSize = Integer.parseInt (Param); else fontSize = 0 ; } catch (NumberFormatException E) { fontSize = -1; } Param = getParameter("Leading"); try { if (Param != null) leading = Float.valueOf (Param).floatValue(); else leading = 0; } catch (NumberFormatException e) { leading =-1; } Param = getParameter("Account Enabled"); if (Param != null) active = Boolean.valueOf(Param).booleanValue(); } public void paint(Graphics G) { G.drawString("FontName = " + fontName,0,10); G.drawString("FontSize = " + fontSize,0,26); G.drawString("Leading = " + leading,0,42); G.drawString("AccountActive = " + active,0,58); } }

2011

Nitesh Bhura

58

Java particle record


Output:

2011

To Write
Nitesh Bhura 59

Java particle record

2011

Q25. WAP to display your file in DOS console use the input /output stream?
/****************************************************************** Unit No. :5 Author : Nitesh Bhura Class : BCA III rd year Path : C:\java\ MenuDemo.java System No : L1S7 Date : 02-01-2011 ******************************************************************/

Coding:
import java.io.*; class FileWriteChar { public static void main(String[] args) throws IOException { String s="Welcome to JAVA world"; FileWriter fw=new FileWriter("msg.txt",true); char ch[]=s.toCharArray(); fw.write(ch); fw.close(); } }

import java.io.*; class FileReadChar { public static void main(String[] args) throws IOException { FileReader fr=new FileReader("msg.txt"); String s="";

Nitesh Bhura

60

Java particle record


int i=fr.read();// assigning first character of file //fr.read() will return ascii code of characters in the file while(i!=-1)// upto last character whose ascii value is -1 { char ch=(char)i; s=s+ch; i=fr.read(); } System.out.println(s); fr.close(); } }
OUTPUT

2011

Nitesh Bhura

61

Você também pode gostar