Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
2011
Nitesh Bhura
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
2011
Nitesh Bhura
2011
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
2011
2011
Output :
Nitesh Bhura
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
2011
Nitesh Bhura
10
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
2011
Nitesh Bhura
12
2011
Output:-
Nitesh Bhura
13
2011
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
2011
Nitesh Bhura
15
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));
2011
Output :
Nitesh Bhura
17
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
2011
Nitesh Bhura
19
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
2011
Nitesh Bhura
21
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
2011
Output :
Nitesh Bhura
23
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
2011
Nitesh Bhura
25
2011
Q13. Write a program where user will create a self Exception using
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
2011
Q14. Write a program to create a package using command and one package will import another package?
Nitesh Bhura 28
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
2011
Output:
Q15. WAP for multithread using theisAlive ( ), join ( ), Synchronized ( ) method of Thread class?
Nitesh Bhura 30
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
2011
Nitesh Bhura
32
2011
Q16. Write a program for Applet that handles the keyboard event?
/******************************************************************
Nitesh Bhura 33
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
2011
Nitesh Bhura
35
2011
Nitesh Bhura
36
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
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
2011
Nitesh Bhura
39
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
2011
stmt.close(); con.close(); } catch(Exception e) //Catch Exception Statement { System.out.println("Exception is : "+e.getMessage()); //Print Statement } } }
Output :
Nitesh Bhura
41
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
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
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
2011
Output:
Nitesh Bhura
45
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
2011
2011
Nitesh Bhura
48
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
2011
//close files
Nitesh Bhura
50
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
2011
52
2011
Output :
Nitesh Bhura
53
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
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
2011
Nitesh Bhura
56
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
2011
Nitesh Bhura
58
2011
To Write
Nitesh Bhura 59
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
2011
Nitesh Bhura
61