Top Banner

of 28

Oos Practical File

Apr 07, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/4/2019 Oos Practical File

    1/28

    Anand Engineering College, Agra

    Department Of Master of Computer Application

    A PRACTICAL FILE

    OF

    Object Oriented Systems (OOS)MCA- 412

    MASTER OF COMPUTER APPLICATIONSESSION (2010-2011)

    Under the guidance of Submitted By:-Mr. Kamlesh Deshmukh KULTAR SINGH

    (Asst. Professor & HOD, MCA Deptt.) 0900114019

  • 8/4/2019 Oos Practical File

    2/28

    PROGRAM1: Output.java

    import java.io.*;class Area{int area1(int x,int y){return(x*y);}int area1(int l){ return(l*l);}double area2(double s){double m=3.14*s*s;return(m);}

    }class Output{public static void main(String args[]) throws IOException{InputStreamReader ir = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(ir);int input1, input2, input3;double input4;

    System.out.println("Enter Input1 for Rectangle :- ");input1 = Integer.parseInt(br.readLine());System.out.println("Enter Input2 for Rectangle :- ");input2 = Integer.parseInt(br.readLine());Area obj1=new Area();int ara;ara=obj1.area1(input1,input2);System.out.println("Area of Rectangle:"+ara);System.out.println("Enter Input for Square :-");input3 = Integer.parseInt(br.readLine());

    ara=obj1.area1(input3);System.out.println("Area of Square:"+ara);double ara2;System.out.println("Enter Input for Circle :-");input4 = Double.parseDouble(br.readLine());ara2=obj1.area2(input4);System.out.println("Area of Circle:"+ara2);}}

  • 8/4/2019 Oos Practical File

    3/28

    Program2: Operation.java

    import java.io.*;class Bank{public static int deposit, withdraw, balance = 10000;}

    class Operation extends Bank{public static void main(String []args) throws IOException{InputStreamReader ir = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(ir);System.out.println("Enter the amount to be withdrawan :-");withdraw = Integer.parseInt(br.readLine());

    if(withdraw > balance){System.out.println("Invalid Transaction");}else{System.out.println("Transaction Successful");}

    }}

  • 8/4/2019 Oos Practical File

    4/28

    Program3: Factorial.java

    import java.io.*;public class Factorial{

    public static void main (String[] args) throws IOException{

    int theNum, theFact;

    InputStreamReader ir = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(ir);

    System.out.println("This program computes the factorial of a number."System.out.print("Enter a number:- ");

    theNum=Integer.parseInt(br.readLine());

    theFact = fact(theNum);

    System.out.println(theNum + "! = " + theFact + ".");}

    static int fact(int n)

    { if (n

  • 8/4/2019 Oos Practical File

    5/28

    Program4:Override.java

    class A{int i,j;

    A(int a, int b){i = a;j = b;}

    void show(){System.out.println("i and j:- " + i + " "+ j);}}

    class B extends A{int k;

    B(int a, int b, int c){super(a,b);k = c;

    }

    void show(){System.out.println("k :- " + k);}}

    class Override{

    public static void main(String[] args){B subOb = new B(1,2,3);subOb.show();}}

  • 8/4/2019 Oos Practical File

    6/28

    Program5: CompString.java

    import java.lang.*;import java.io.*;

    public class CompString{public static void main(String[] args) throws IOException{

    System.out.println("String equals or not example!");BufferedReader bf = new BufferedReader(new

    InputStreamReader(System.in));System.out.println("Please enter first string:");String str1 = bf.readLine();System.out.println("Please enter second string:");String str2 = bf.readLine();if (str1.equals(str2)){

    System.out.println("The given string is equals");}

    else{System.out.println("The given string is not equals");

    }}

    }

    Program6: stringBuffer.java

    class stringBuffer{public static void main(String[] args){StringBuffer sb1 = new StringBuffer("Java");StringBuffer sb2 = new StringBuffer("Java");System.out.println(sb1 == sb2);System.out.println(sb1.equals(sb2));

    System.out.println(sb1.toString().equals(sb2.toString()));System.out.println(sb1.toString().compareTo(sb2.toString()));}}

  • 8/4/2019 Oos Practical File

    7/28

    Program7: Exec.java

    import java.io.*;class Exec{

    public static void main(String []args) throws IOException{try{double num1 = Double.parseDouble(args[0]);double num2 = Double.parseDouble(args[1]);double result = (num1/num2);System.out.println("Before Catch Statement");System.out.println("The result is = " + result);}

    catch(NumberFormatException nfe){System.out.println("The Entered Key is not a double value" + nfe);}

    catch(ArithmeticException ae){System.out.println("The Attempt to divide a number by zero" + ae);}

    System.out.println("After Catch Statement");}}

  • 8/4/2019 Oos Practical File

    8/28

    Program8: Arithmetic.java

    import java.awt.*;import java.awt.event.*;import java.applet.*;public class Arithmetic extends Applet implements ActionListener{Label lbl1,lbl2,lbl3;TextField tf1,tf2,tf3;public void init(){setLayout(null);lbl1 = new Label("Enter Number1:");tf1=new TextField(20);lbl2 = new Label("Enter Number2:");tf2=new TextField(20);lbl3 = new Label("Output:");

    tf3=new TextField(20);

    Button b=new Button("+");Button b1=new Button("-");Button b2=new Button("*");

    lbl1.setBounds(350,100,100,20);tf1.setBounds(450,100,200,20);

    lbl2.setBounds(350,130,100,20);tf2.setBounds(450,130,200,20);

    lbl3.setBounds(350,160,100,20);tf3.setBounds(450,160,200,20);

    b.setBounds(450,190,200,20);b1.setBounds(450,220,200,20);b2.setBounds(450,250,200,20);

    add(lbl1);add(tf1);add(lbl2);add(tf2);add(lbl3);add(tf3);add(b);add(b1);add(b2);

  • 8/4/2019 Oos Practical File

    9/28

    b.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);

    setVisible(true);setSize(1024,768);

    }public void actionPerformed(ActionEvent e){String q=e.getActionCommand();String z=tf1.getText();String z1=tf2.getText();if (q.equals("+")){int b=Integer.parseInt(z)+Integer.parseInt(z1);tf3.setText(Integer.toString(b));

    }if (q.equals("-")){int b=Integer.parseInt(z)-Integer.parseInt(z1);tf3.setText(Integer.toString(b));}if (q.equals("*")){int b=Integer.parseInt(z)*Integer.parseInt(z1);tf3.setText(Integer.toString(b));}

    }}

    Arithmetic.html

  • 8/4/2019 Oos Practical File

    10/28

    Program9: CopyFile.java

    import java.io.*;

    public class CopyFile{private static void copyfile(String srFile, String dtFile){

    try{File f1 = new File(srFile);File f2 = new File(dtFile);InputStream in = new FileInputStream(f1);

    //For Append the file.// OutputStream out = new FileOutputStream(f2,true);

    //For Overwrite the file.OutputStream out = new FileOutputStream(f2);byte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0){

    out.write(buf, 0, len);}in.close();out.close();System.out.println("File copied.");

    }catch(FileNotFoundException ex){

    System.out.println(ex.getMessage() + " in the specifieddirectory.");

    System.exit(0);}catch(IOException e){

    System.out.println(e.getMessage());}

    }public static void main(String[] args){

    switch(args.length){case 0: System.out.println("File has not mentioned.");

    System.exit(0);

    case 1: System.out.println("Destination file has notmentioned.");

    System.exit(0);case 2: copyfile(args[0],args[1]);

    System.exit(0);default : System.out.println("Multiple files are not allow.");

    System.exit(0);}

    }}

  • 8/4/2019 Oos Practical File

    11/28

    Program10: ServerApplication.java

    import java.net.*;import java.io.*;

    public class ServerApplication{

    ServerSocket server=null;Socket clientSocket=null;BufferedWriter br=null;static int hitCount=1;public String startServer(){

    try{

    server=new ServerSocket(95);}catch(Exception e)

    {return "Problem creating the ServerSocket";

    }try{

    clientSocket=server.accept();}catch(Exception e){

    return "Problem creating the Socket";

    }

    return "Connection established from"+clientSocket.getInetAddress();}public void sendData(String response){if(hitCount>1){

    String s=startServer();}

    try{

    br=new BufferedWriter(newOutputStreamWriter(clientSocket.getOutputStream()));

    br.write(response);br.close();stopServer();hitCount++;

    }catch(Exception e)

  • 8/4/2019 Oos Practical File

    12/28

    {System.out.println("Problem with OutputStream"+e);

    }}public String stopServer(){try{ if(clientSocket !=null && server!=null)

    {clientSocket.close();server.close();

    }}catch(Exception e){

    return "Problem Closing the Connection";}return "Server Stopped..";

    }}

    ClientApplication.java

    import java.net.*;import java.io.*;

    public class ClientApplication{

    Socket clientSocket=null;int hitCount=1;public String connectToServer(){

    try{

    clientSocket=new Socket(InetAddress.getLocalHost(),95);return "Client Services Started"+"\n";

    }catch(Exception e)

    {return "Client problem.."+e;

    }}public StringBuffer recieveData()throws IOException{

    if(hitCount >1){

    connectToServer();

  • 8/4/2019 Oos Practical File

    13/28

    }BufferedReader br=new BufferedReader(new

    InputStreamReader(clientSocket.getInputStream()));String line;StringBuffer sb=new StringBuffer();while((line=br.readLine()) !=null){

    sb.append(line+"\n");}br.close();if(hitCount >=1){

    clientSocket.close();}hitCount++;return sb;

    }

    }

    ServerFrame.java

    import java.awt.*;import java.awt.event.*;

    public class ServerFrame extends Frame implements ActionListener{

    TextArea txtStatus,txtData;Button bConnect,bSend,bClose;Label lMessage;Panel pChoiceSegment,pDataSegment;ServerApplication sap;ServerFrame(){

    super("Server Application");pChoiceSegment=new Panel();

    pDataSegment=new Panel();bConnect=new Button("Start Server");bConnect.setActionCommand("Start");bConnect.addActionListener(this);bSend=new Button("Send Data");bSend.setActionCommand("Send");bSend.addActionListener(this);bClose=new Button("ShutDown");bClose.setActionCommand("Close");

  • 8/4/2019 Oos Practical File

    14/28

    bClose.addActionListener(this);sap=new ServerApplication();pChoiceSegment.add(bConnect);pChoiceSegment.add(bSend);pChoiceSegment.add(bClose);txtStatus=new TextArea(3,25);txtStatus.setEnabled(false);lMessage=new Label("Enter the values in the text area ..");txtData=new TextArea(13,38);pDataSegment.add(lMessage);pDataSegment.add(txtData);add(txtStatus,BorderLayout.SOUTH);add(pDataSegment,BorderLayout.CENTER);add(pChoiceSegment,BorderLayout.NORTH);setSize(300,400);show();

    }

    public static void main(String args[]){

    ServerFrame sf=new ServerFrame();}public void actionPerformed(ActionEvent e){

    String prompt=e.getActionCommand();if(prompt=="Start"){

    txtStatus.append("Server Started...."+"\n");try{txtStatus.append(sap.startServer());}catch(Exception se){

    txtStatus.setText(se.toString());}

    }else if(prompt=="Send")

    {try{sap.sendData(txtData.getText());

    }catch(Exception ie){

    txtData.setText("Inside Send "+ie.toString());}

    }

  • 8/4/2019 Oos Practical File

    15/28

    else if(prompt=="Close"){

    txtStatus.setText(sap.stopServer());txtData.setText("");dispose();System.exit(0);

    }}

    }

    ClientFrame.java

    import java.awt.*;import java.awt.event.*;

    public class ClientFrame extends Frame implements ActionListener

    {TextArea txt;Button bConnect,bRecieve;Panel p;ClientApplication cap;ClientFrame(){

    super("Client Application");p=new Panel();bConnect=new Button("Connect to Server");bConnect.setActionCommand("Start");bConnect.addActionListener(this);bRecieve=new Button("Recieve the Data");bRecieve.setActionCommand("Recieve");bRecieve.addActionListener(this);cap=new ClientApplication();p.add(bConnect);p.add(bRecieve);txt=new TextArea();

    add(txt,BorderLayout.CENTER);add(p,BorderLayout.SOUTH);setSize(300,400);show();

    }public static void main(String args[]){

    ClientFrame cf=new ClientFrame();}

  • 8/4/2019 Oos Practical File

    16/28

    public void actionPerformed(ActionEvent e){

    String prompt=e.getActionCommand();if(prompt=="Start"){

    try{txt.append(cap.connectToServer());}catch(Exception se){

    txt.setText(se.toString());}

    }else if(prompt=="Recieve"){

    try{

    txt.append(cap.recieveData().toString());}catch(Exception ie){

    txt.setText(ie.toString());}

    }}

    }

  • 8/4/2019 Oos Practical File

    17/28

    Program11: LoginForm.java

    import java.awt.*;import java.awt.event.*;import java.applet.*;public class LoginForm extends Applet{Label lbl1,lbl2;TextField tf1, tf2;public void init(){setLayout(null);lbl1 = new Label("UserName:");tf1=new TextField(20);lbl2 = new Label("Password:");tf2=new TextField(20);Button b=new Button("OK");Button b1=new Button("CANCEL");lbl1.setBounds(350,100,100,20);

    tf1.setBounds(450,100,200,20);

    lbl2.setBounds(350,130,100,20);tf2.setBounds(450,130,200,20);

    //b.setBounds(450,190,200,20);//b1.setBounds(450,220,200,20);

    add(lbl1);

    add(tf1);add(lbl2);add(tf2);tf2.setEchoChar('*');

    add(b);add(b1);setVisible(true);

    setSize(1024,768);}}

    LoginForm.html

  • 8/4/2019 Oos Practical File

    18/28

    Program12: MenuDemo

    import java.awt.*;import java.awt.event.*;import java.applet.*;

    class MenuFrame extends Frame{String msg = "";CheckboxMenuItem debug, test;

    MenuFrame(String title){super(title);

    MenuBar mbar = new MenuBar();setMenuBar(mbar);

    Menu file = new Menu("File");MenuItem item1, item2, item3, item4, item5;file.add(item1 = new MenuItem("New..."));file.add(item2 = new MenuItem("Open..."));file.add(item3 = new MenuItem("Close..."));file.add(item4 = new MenuItem("-"));file.add(item5 = new MenuItem("Quit..."));mbar.add(file);

    Menu edit = new Menu("Edit");MenuItem item6, item7, item8, item9;edit.add(item6 = new MenuItem("Cut"));edit.add(item7 = new MenuItem("Copy"));edit.add(item8 = new MenuItem("paste"));edit.add(item9 = new MenuItem("-"));

    Menu sub = new Menu("Special");

    MenuItem item10, item11, item12;sub.add(item10 = new MenuItem("First"));sub.add(item11 = new MenuItem("Second"));sub.add(item12 = new MenuItem("Third"));edit.add(sub);

    debug = new CheckboxMenuItem("Debug");edit.add(debug);test = new CheckboxMenuItem("Testing");

  • 8/4/2019 Oos Practical File

    19/28

    edit.add(test);

    mbar.add(edit);

    MyMenuHandler handler = new MyMenuHandler(this);

    debug.addItemListener(handler);test.addItemListener(handler);

    MyWindowAdapter adapter = new MyWindowAdapter(this);addWindowListener(adapter);}

    public void paint(Graphics g){if(debug.getState())g.drawString("Debug is on.", 10, 220);else

    g.drawString("Debug is off.", 10,220);

    if(test.getState())g.drawString("Testing is on.", 10,240);elseg.drawString("Testing is off.", 10,240);}}class MyWindowAdapter extends WindowAdapter{MenuFrame menuFrame;public MyWindowAdapter(MenuFrame menuFrame){this.menuFrame = menuFrame;}public void windowClosing(WindowEvent we){menuFrame.setVisible(false);}

    }

    class MyMenuHandler implements ActionListener, ItemListener{MenuFrame menuFrame;public MyMenuHandler(MenuFrame menuFrame){this.menuFrame = menuFrame;}

  • 8/4/2019 Oos Practical File

    20/28

    public void actionPerformed(ActionEvent ae){String msg = "You selected";String arg = ae.getActionCommand();if(arg.equals("New..."))msg+= "New.";

    else if(arg.equals("open..."))msg+= "open.";

    else if(arg.equals("Close"))msg+= "Close";

    else if(arg.equals("Quit..."))msg+= "open.";

    else if(arg.equals("Edit"))msg+= "Edit.";

    else if(arg.equals("Cut"))msg+= "Cut.";

    else if(arg.equals("Copy"))msg+= "Copy.";

    else if(arg.equals("Paste"))msg+= "Paste.";

    else if(arg.equals("First"))

    msg+= "First.";

    else if(arg.equals("Second"))msg+= "Second.";

    else if(arg.equals("Third"))msg+= "Third.";else if(arg.equals("Debug..."))

  • 8/4/2019 Oos Practical File

    21/28

    msg+= "Debug.";

    else if(arg.equals("Testing"))msg+= "Testing.";

    menuFrame.msg = msg;menuFrame.repaint();}

    public void itemStateChanged(ItemEvent ie){menuFrame.repaint();}}public class MenuDemo extends Applet{

    Frame f;public void init(){f = new MenuFrame("Menu Demo");int width = Integer.parseInt(getParameter("width"));int height = Integer.parseInt(getParameter("height"));

    setSize(new Dimension(width, height));f.setVisible(true);}public void start(){f.setVisible(true);}public void stop(){f.setVisible(false);}}

    MenuDemo.html

  • 8/4/2019 Oos Practical File

    22/28

    Program13: DrawRectangle.java

    import java.awt.*;import java.applet.*;

    public class DrawRectangle extends Applet{public void paint(Graphics g){g.drawRect(10,60,40,30);g.fillRect(60,10,30,80);g.drawRoundRect(10,100,80,50,10,10);g.fillRoundRect(20,110,60,30,5,5);

    g.drawOval(20,20,200,120);g.setColor(Color.green);g.fillOval(70,30,100,100);

    }}

    DrawRectangle.html

  • 8/4/2019 Oos Practical File

    23/28

    Program14: Layout Managers

    BorderLayout.java//the demo of border layoutimport java.awt.*;import javax.swing.*;

    public class borderLayout extends JApplet{

    public void init(){

    setLayout(new BorderLayout());

    add(new JButton("CBOT"), BorderLayout.NORTH);add(new JButton("IJP"), BorderLayout.EAST);add(new JButton("DBMS"), BorderLayout.SOUTH);

    add(new JButton("OS"), BorderLayout.WEST);add(new JButton("DAA"), BorderLayout.CENTER);

    }public Insets getInsets(){return new Insets(20,40,20,40);}}

    /* */

    FlowLayout.java

    //the demo of flow layoutimport java.awt.*;import javax.swing.*;

    public class flowLayout extends JApplet

    { JButton b1,b2,b3;//JButton b4,b5,b6;FlowLayout f1;

    public void init(){ f1=new FlowLayout(FlowLayout.LEFT);

    JPanel p1=new JPanel();//getContentPane().add(p1);

    Container c = getContentPane();c.add(p1);

  • 8/4/2019 Oos Practical File

    24/28

    p1.setLayout(f1);b1=new JButton("Left");b2=new JButton("Center");b3=new JButton("Right");

    //b4=new JButton("Other");//b5=new JButton("Other1");//b6=new JButton("Other2");

    p1.add(b1);p1.add(b2);p1.add(b3);

    //p1.add(b4);//p1.add(b5);//p1.add(b6);

    }}/* */

    GridLayout.java

    import java.awt.*;import javax.swing.*;

    public class gridLayout extends JApplet{

    public void init(){setSize(600,140);setLayout(new GridLayout(3,4));for(int i = 1; i

  • 8/4/2019 Oos Practical File

    25/28

    Program15:UserForm.java

    import javax.swing.*;import java.awt.*;

    class UserForm extends JFrame{JButton SUBMIT;JLabel label1,label2,label3,label4,label5,label6, label7;final JTextField text1,text2,text3,text4,text5,text6, text7;UserForm(){

    setTitle("User Form");setLayout(null);

    label1 = new JLabel();label1.setText("First Name:");text1 = new JTextField(15);

    label2 = new JLabel();label2.setText("Last Name:");text2 = new JTextField(15);

    label3 = new JLabel();

    label3.setText("Age:");text3 = new JTextField(15);

    label4 = new JLabel();label4.setText("Occupation:");text4 = new JTextField(15);

    label5 = new JLabel();label5.setText("Address:");text5 = new JTextField(15);

    label6 = new JLabel();label6.setText("Email:");text6 = new JTextField(15);

    label7 = new JLabel();label7.setText("Mobile:");text7 = new JTextField(15);

  • 8/4/2019 Oos Practical File

    26/28

    SUBMIT=new JButton("SUBMIT");label1.setBounds(350,100,100,20);text1.setBounds(450,100,200,20);label2.setBounds(350,130,100,20);text2.setBounds(450,130,200,20);

    label3.setBounds(350,160,100,20);text3.setBounds(450,160,200,20);

    label4.setBounds(350,190,100,20);text4.setBounds(450,190,200,20);

    label5.setBounds(350,220,100,20);text5.setBounds(450,220,200,20);

    label6.setBounds(350,250,100,20);text6.setBounds(450,250,200,20);

    label7.setBounds(350,280,100,20);text7.setBounds(450,280,200,20);

    SUBMIT.setBounds(450,310,100,20);

    add(label1);add(text1);add(label2);add(text2);

    add(label3);add(text3);

    add(label4);add(text4);

    add(label5);add(text5);

    add(label6);add(text6);

    add(label7);add(text7);add(SUBMIT);

    setVisible(true);setSize(1024,768);

    }public static void main(String arg[])

    {new UserForm();

    }}

  • 8/4/2019 Oos Practical File

    27/28

    Program 16:EmployeeDetails.java

    /* Import the required packages. */import java.io.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;

    public class EmployeeDetails extends HttpServlet{

    static int i;Connection con;PrintWriter out;ResultSet rs;public void init(){

    i = 0;

    con=null;out=null;rs=null;

    }public void doGet(HttpServletRequest request, HttpServletResponse

    response) throws ServletException, IOException{

    i++;out=response.getWriter();

    out.println("You are user no. " + i +" to visit thissite.

    ");

    try{

    /*Load the JDBC-ODBC bridge driver*/Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    /*Obtain a Connection object*/con=DriverManager.getConnection("jdbc:odbc:rahul","sa","1

    PreparedStatement pstmt=null;String query=null;

    /*Create SQL query*/query= "select fname from dd where empid=?";

    /*Create PreparedStatement object*/pstmt=con.prepareStatement(query);

    /*Obtain the request parameter and specify it as value of theSQL statement parameter*/

  • 8/4/2019 Oos Practical File

    28/28

    pstmt.setInt(1,Integer.parseInt(request.getParameter("id"))/*Execute PreparedStatement*/rs=pstmt.executeQuery();out.println("Employee Details



    ");/*Obtain the ResultSet meta data*/ResultSetMetaData rsmd= rs.getMetaData();

    /*Obtain the number of columns in the ResultSet*/int colcount=rsmd.getColumnCount();out.println("");out.println("");

    /*Display column names in table*/for(int i=1; i