Some sort of confusion!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    Some sort of confusion!!!

    I write java codes a little ,they run and everything is ok.But still I cant understand what it means by OOPS.I fill very sorry when after seeing my codes my seniors tell that,"it runs,but this is very much procedure oriented",try adapting "object oriented " style.I have added a sample code written by me,wid reference to that,someone please explain,what oops actually means and how can I convert this code into an oops style.Thanks in regards...

    Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import javax.swing.*;
    import javax.swing.text.JTextComponent;
    
    public class Examples extends JApplet implements ActionListener {
    	JTextField jtf, jtf1;
    
    	JPasswordField pass = new JPasswordField(15);
    
    	Container contentpane = getContentPane();
    
    	String ss[] = new String[100];
    
    	JTextField jtf2, jtf3;
    
    	JLabel label1 = new JLabel("FIRST FILE");
    
    	JLabel label2 = new JLabel("SECOND FILE");
    
    	String s1[] = new String[2];
    
    	int i = 0, flag = 0,back=0;
    	//Frame frame1=new JFrame("LOGIN PAGE");
    	
    	Container contentPane =getContentPane();
    
    	public void init() {
    		 
    		jtf = new JTextField(3);
    		jtf1 = new JTextField(15);
    		JPanel p1 = new JPanel(new GridLayout(3, 1));
    		p1.add(new JLabel("SID"));
    		p1.add(jtf);
    		p1.add(new JLabel("User name:"));
    
    		System.out.println(jtf.getText());
    		p1.add(jtf1);
    		p1.add(new JLabel("Password:"));
    		p1.add(pass);
    		contentPane.add("North", p1);
    		JButton submit = new JButton("SUBMIT");
    		submit.setActionCommand("submit");
    		submit.addActionListener(this);
    		// submit.doClick();
    		JButton reset = new JButton("RESET");
    		reset.setActionCommand("reset");
    		reset.addActionListener(this);
    		// reset.doClick();
    		JPanel p2 = new JPanel();
    		p2.add(submit);
    
    		p2.add(reset);
    		contentPane.add("Center", p2);
    		// setSize(240, 120);
    
    	}
    
    	public void actionPerformed(ActionEvent ae) {
    		String comm = null;
    		int i = 0;
    		comm = ae.getActionCommand();
    		if (comm == "submit" && i == 0) {
    			i++;
    			getValue(jtf.getText(), jtf1.getText(), pass.getText());
    			// System.out.println(pass.getText());
    		}
    		if (comm == "reset") {
    			jtf.setText("");
    			jtf1.setText("");
    			pass.setText("");
    		
    			return;
    		}
    
    		if (comm == "submit1") {
    
    		}
    		if (flag == 1 && comm != "submit1"&&comm!="submit") {
    
    			s1[0] = comm;
    			jtf2.setText(comm);
    		}
    		if (flag == 0 && comm != "submit1"&&comm!="submit") {
    			s1[1] = comm;
    			jtf3.setText(comm);
    		}
    		flag++;
    		if (flag == 2)
    			flag = 0;
    
    	}
    
    	public void getValue(String sid, String user, String pass) {
    		// System.out.println(pass);
    		String s1 = "jdbc:oracle:thin:@157.227.93.32:1521:"+sid;
    		int i = 0;
    		try {
    			// System.out.println(pass);
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    			Connection conn = DriverManager.getConnection(s1, user,pass);
    			Statement stmt = conn.createStatement();
    			ResultSet rs = stmt
    					.executeQuery("select distinct TABLE_NAME  from  USER_CONSTRAINTS");
    			while (rs != null && rs.next()) {
    				// System.out.println(pass);
    				ss[i] = rs.getString("TABLE_NAME");
    				i++;
    			}
    
    		} catch (Exception e) {
    			JOptionPane.showConfirmDialog(null, 
    					"ERROR LOGIN", "ERROR IN LOGIN,TRY AGAIN?", JOptionPane.YES_NO_OPTION);
    
    
    			//e.printStackTrace();
    		}
    		int ii = 0;
    		JFrame theFrame = new JFrame("SELECT TWO TABLES");
    		theFrame.pack();
    		theFrame.setVisible(true);
    		Container content = theFrame.getContentPane();
    		jtf2 = new JTextField("FIRST FILE", 15);
    		jtf2.setEditable(false);
    		jtf2.setBackground(Color.WHITE);
    		jtf3 = new JTextField("SECOND FILE", 15);
    		jtf3.setEditable(false);
    		jtf3.setBackground(Color.WHITE);
    		content.setLayout(new FlowLayout());
    		label1.setLabelFor(jtf);
    		label2.setLabelFor(jtf1);
    		JButton jb[] = new JButton[i];
    		JButton jb1 = new JButton(
    				"<html><body  ><font  type=Comic Sans MS size=5><b><i>SUBMIT</i></b></font></body></html>");
    		// ImageIcon icon=new ImageIcon("action.gif");
    		content.add(label1);
    		content.add(jtf2);
    		content.add(label2);
    		content.add(jtf3);
    
    		// contentPane.add(label2);
    		/*
    		 * label2.setAlignmentX(10); label2.setAlignmentY(3);
    		 */
    		for (ii = 0; ii < i; ii++) {
    
    			jb[ii] = new JButton(
    					"<html><body ><font  color=black type=Comic Sans MS size=4><b><i>"
    							+ ss[ii] + "</i></b></font></body></html>");
    			jb[ii].setActionCommand(ss[ii]);
    			jb[ii].addActionListener(this);
    			jb[ii].doClick();
    			jb[ii]
    					.setToolTipText("<html><body><font  color=black type=Comic Sans MS size=4><b><i>"
    							+ ss[ii] + "</i></b></font></body></html>");
    			jb[ii].setBackground(Color.CYAN);
    			jb[ii].setBorderPainted(true);
    
    			jb[ii].setRequestFocusEnabled(true);
    			content.add(jb[ii]);
    
    		}
    
    		jb1.setActionCommand("submit1");
    		jb1.addActionListener(this);
    		jb1.doClick();
    		jb1
    				.setToolTipText("<html><body bgcolor ><font  type=Comic Sans MS size=5><b><i>SUBMIT</i></b></font></body></html>");
    		jb1.setBackground(Color.red);
    		content.add(jb1);
    
    	}
    
    }
    Last edited by horace1; Jan 22 '07, 09:50 AM. Reason: put in code tags
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    Some sort of confusion!!!

    I write java codes a little ,they run and everything is ok.But still I cant understand what it means by OOPS.I fill very sorry when after seeing my codes my seniors tell that,"it runs,but this is very much procedure oriented",try adapting "object oriented " style.I have added a sample code written by me,wid reference to that,someone please explain,what oops actually means and how can I convert this code into an oops style.Thanks in regards...

    [CODE]


    import java.awt.*;
    import java.awt.event. *;
    import java.sql.Connec tion;
    import java.sql.Driver Manager;
    import java.sql.Result Set;
    import java.sql.Statem ent;

    import javax.swing.*;
    import javax.swing.tex t.JTextComponen t;

    public class Examples extends JApplet implements ActionListener {
    JTextField jtf, jtf1;

    JPasswordField pass = new JPasswordField( 15);

    Container contentpane = getContentPane( );

    String ss[] = new String[100];

    JTextField jtf2, jtf3;

    JLabel label1 = new JLabel("FIRST FILE");

    JLabel label2 = new JLabel("SECOND FILE");

    String s1[] = new String[2];

    int i = 0, flag = 0,back=0;
    //Frame frame1=new JFrame("LOGIN PAGE");

    Container contentPane =getContentPane ();

    public void init() {

    jtf = new JTextField(3);
    jtf1 = new JTextField(15);
    JPanel p1 = new JPanel(new GridLayout(3, 1));
    p1.add(new JLabel("SID"));
    p1.add(jtf);
    p1.add(new JLabel("User name:"));

    System.out.prin tln(jtf.getText ());
    p1.add(jtf1);
    p1.add(new JLabel("Passwor d:"));
    p1.add(pass);
    contentPane.add ("North", p1);
    JButton submit = new JButton("SUBMIT ");
    submit.setActio nCommand("submi t");
    submit.addActio nListener(this) ;
    // submit.doClick( );
    JButton reset = new JButton("RESET" );
    reset.setAction Command("reset" );
    reset.addAction Listener(this);
    // reset.doClick() ;
    JPanel p2 = new JPanel();
    p2.add(submit);

    p2.add(reset);
    contentPane.add ("Center", p2);
    // setSize(240, 120);

    }

    public void actionPerformed (ActionEvent ae) {
    String comm = null;
    int i = 0;
    comm = ae.getActionCom mand();
    if (comm == "submit" && i == 0) {
    i++;
    getValue(jtf.ge tText(), jtf1.getText(), pass.getText()) ;
    // System.out.prin tln(pass.getTex t());
    }
    if (comm == "reset") {
    jtf.setText("") ;
    jtf1.setText("" );
    pass.setText("" );

    return;
    }

    if (comm == "submit1") {

    }
    if (flag == 1 && comm != "submit1"&&comm !="submit") {

    s1[0] = comm;
    jtf2.setText(co mm);
    }
    if (flag == 0 && comm != "submit1"&&comm !="submit") {
    s1[1] = comm;
    jtf3.setText(co mm);
    }
    flag++;
    if (flag == 2)
    flag = 0;

    }

    public void getValue(String sid, String user, String pass) {
    // System.out.prin tln(pass);
    String s1 = "jdbc:oracle:th in:@157.227.93. 32:1521:"+sid;
    int i = 0;
    try {
    // System.out.prin tln(pass);
    Class.forName(" oracle.jdbc.dri ver.OracleDrive r");
    Connection conn = DriverManager.g etConnection(s1 , user,pass);
    Statement stmt = conn.createStat ement();
    ResultSet rs = stmt
    .executeQuery(" select distinct TABLE_NAME from USER_CONSTRAINT S");
    while (rs != null && rs.next()) {
    // System.out.prin tln(pass);
    ss[i] = rs.getString("T ABLE_NAME");
    i++;
    }

    } catch (Exception e) {
    JOptionPane.sho wConfirmDialog( null,
    "ERROR LOGIN", "ERROR IN LOGIN,TRY AGAIN?", JOptionPane.YES _NO_OPTION);


    //e.printStackTra ce();
    }
    int ii = 0;
    JFrame theFrame = new JFrame("SELECT TWO TABLES");
    theFrame.pack() ;
    theFrame.setVis ible(true);
    Container content = theFrame.getCon tentPane();
    jtf2 = new JTextField("FIR ST FILE", 15);
    jtf2.setEditabl e(false);
    jtf2.setBackgro und(Color.WHITE );
    jtf3 = new JTextField("SEC OND FILE", 15);
    jtf3.setEditabl e(false);
    jtf3.setBackgro und(Color.WHITE );
    content.setLayo ut(new FlowLayout());
    label1.setLabel For(jtf);
    label2.setLabel For(jtf1);
    JButton jb[] = new JButton[i];
    JButton jb1 = new JButton(
    "<html><bod y ><font type=Comic Sans MS size=5><b><i>SU BMIT</i></b></font></body></html>");
    // ImageIcon icon=new ImageIcon("acti on.gif");
    content.add(lab el1);
    content.add(jtf 2);
    content.add(lab el2);
    content.add(jtf 3);

    // contentPane.add (label2);
    /*
    * label2.setAlign mentX(10); label2.setAlign mentY(3);
    */
    for (ii = 0; ii < i; ii++) {

    jb[ii] = new JButton(
    "<html><bod y ><font color=black type=Comic Sans MS size=4><b><i>"
    + ss[ii] + "</i></b></font></body></html>");
    jb[ii].setActionComma nd(ss[ii]);
    jb[ii].addActionListe ner(this);
    jb[ii].doClick();
    jb[ii]
    .setToolTipText ("<html><body>< font color=black type=Comic Sans MS size=4><b><i>"
    + ss[ii] + "</i></b></font></body></html>");
    jb[ii].setBackground( Color.CYAN);
    jb[ii].setBorderPaint ed(true);

    jb[ii].setRequestFocu sEnabled(true);
    content.add(jb[ii]);

    }

    jb1.setActionCo mmand("submit1" );
    jb1.addActionLi stener(this);
    jb1.doClick();
    jb1
    .setToolTipText ("<html><bod y bgcolor ><font type=Comic Sans MS size=5><b><i>SU BMIT</i></b></font></body></html>");
    jb1.setBackgrou nd(Color.red);
    content.add(jb1 );

    }

    }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by hirak1984
      I write java codes a little ,they run and everything is ok.But still I cant understand what it means by OOPS.I fill very sorry when after seeing my codes my seniors tell that,"it runs,but this is very much procedure oriented",try adapting "object oriented " style.I have added a sample code written by me,wid reference to that,someone please explain,what oops actually means and how can I convert this code into an oops style.Thanks in regards...

      [CODE]


      import java.awt.*;
      import java.awt.event. *;
      import java.sql.Connec tion;
      import java.sql.Driver Manager;
      import java.sql.Result Set;
      import java.sql.Statem ent;

      import javax.swing.*;
      import javax.swing.tex t.JTextComponen t;

      public class Examples extends JApplet implements ActionListener {
      JTextField jtf, jtf1;

      JPasswordField pass = new JPasswordField( 15);

      Container contentpane = getContentPane( );

      String ss[] = new String[100];

      JTextField jtf2, jtf3;

      JLabel label1 = new JLabel("FIRST FILE");

      JLabel label2 = new JLabel("SECOND FILE");

      String s1[] = new String[2];

      int i = 0, flag = 0,back=0;
      //Frame frame1=new JFrame("LOGIN PAGE");

      Container contentPane =getContentPane ();

      public void init() {

      jtf = new JTextField(3);
      jtf1 = new JTextField(15);
      JPanel p1 = new JPanel(new GridLayout(3, 1));
      p1.add(new JLabel("SID"));
      p1.add(jtf);
      p1.add(new JLabel("User name:"));

      System.out.prin tln(jtf.getText ());
      p1.add(jtf1);
      p1.add(new JLabel("Passwor d:"));
      p1.add(pass);
      contentPane.add ("North", p1);
      JButton submit = new JButton("SUBMIT ");
      submit.setActio nCommand("submi t");
      submit.addActio nListener(this) ;
      // submit.doClick( );
      JButton reset = new JButton("RESET" );
      reset.setAction Command("reset" );
      reset.addAction Listener(this);
      // reset.doClick() ;
      JPanel p2 = new JPanel();
      p2.add(submit);

      p2.add(reset);
      contentPane.add ("Center", p2);
      // setSize(240, 120);

      }

      public void actionPerformed (ActionEvent ae) {
      String comm = null;
      int i = 0;
      comm = ae.getActionCom mand();
      if (comm == "submit" && i == 0) {
      i++;
      getValue(jtf.ge tText(), jtf1.getText(), pass.getText()) ;
      // System.out.prin tln(pass.getTex t());
      }
      if (comm == "reset") {
      jtf.setText("") ;
      jtf1.setText("" );
      pass.setText("" );

      return;
      }

      if (comm == "submit1") {

      }
      if (flag == 1 && comm != "submit1"&&comm !="submit") {

      s1[0] = comm;
      jtf2.setText(co mm);
      }
      if (flag == 0 && comm != "submit1"&&comm !="submit") {
      s1[1] = comm;
      jtf3.setText(co mm);
      }
      flag++;
      if (flag == 2)
      flag = 0;

      }

      public void getValue(String sid, String user, String pass) {
      // System.out.prin tln(pass);
      String s1 = "jdbc:oracle:th in:@157.227.93. 32:1521:"+sid;
      int i = 0;
      try {
      // System.out.prin tln(pass);
      Class.forName(" oracle.jdbc.dri ver.OracleDrive r");
      Connection conn = DriverManager.g etConnection(s1 , user,pass);
      Statement stmt = conn.createStat ement();
      ResultSet rs = stmt
      .executeQuery(" select distinct TABLE_NAME from USER_CONSTRAINT S");
      while (rs != null && rs.next()) {
      // System.out.prin tln(pass);
      ss[i] = rs.getString("T ABLE_NAME");
      i++;
      }

      } catch (Exception e) {
      JOptionPane.sho wConfirmDialog( null,
      "ERROR LOGIN", "ERROR IN LOGIN,TRY AGAIN?", JOptionPane.YES _NO_OPTION);


      //e.printStackTra ce();
      }
      int ii = 0;
      JFrame theFrame = new JFrame("SELECT TWO TABLES");
      theFrame.pack() ;
      theFrame.setVis ible(true);
      Container content = theFrame.getCon tentPane();
      jtf2 = new JTextField("FIR ST FILE", 15);
      jtf2.setEditabl e(false);
      jtf2.setBackgro und(Color.WHITE );
      jtf3 = new JTextField("SEC OND FILE", 15);
      jtf3.setEditabl e(false);
      jtf3.setBackgro und(Color.WHITE );
      content.setLayo ut(new FlowLayout());
      label1.setLabel For(jtf);
      label2.setLabel For(jtf1);
      JButton jb[] = new JButton[i];
      JButton jb1 = new JButton(
      "<html><bod y ><font type=Comic Sans MS size=5><b><i>SU BMIT</i></b></font></body></html>");
      // ImageIcon icon=new ImageIcon("acti on.gif");
      content.add(lab el1);
      content.add(jtf 2);
      content.add(lab el2);
      content.add(jtf 3);

      // contentPane.add (label2);
      /*
      * label2.setAlign mentX(10); label2.setAlign mentY(3);
      */
      for (ii = 0; ii < i; ii++) {

      jb[ii] = new JButton(
      "<html><bod y ><font color=black type=Comic Sans MS size=4><b><i>"
      + ss[ii] + "</i></b></font></body></html>");
      jb[ii].setActionComma nd(ss[ii]);
      jb[ii].addActionListe ner(this);
      jb[ii].doClick();
      jb[ii]
      .setToolTipText ("<html><body>< font color=black type=Comic Sans MS size=4><b><i>"
      + ss[ii] + "</i></b></font></body></html>");
      jb[ii].setBackground( Color.CYAN);
      jb[ii].setBorderPaint ed(true);

      jb[ii].setRequestFocu sEnabled(true);
      content.add(jb[ii]);

      }

      jb1.setActionCo mmand("submit1" );
      jb1.addActionLi stener(this);
      jb1.doClick();
      jb1
      .setToolTipText ("<html><bod y bgcolor ><font type=Comic Sans MS size=5><b><i>SU BMIT</i></b></font></body></html>");
      jb1.setBackgrou nd(Color.red);
      content.add(jb1 );

      }

      }
      Please do not double post. OOP is not something you can learn with one snippet of code. Get a text to read about it e.g Thinking in Java or visit www.oopweb.com.

      Comment

      • abctech
        New Member
        • Dec 2006
        • 157

        #4
        Hi hirak1984

        To get an idea about the basic difference read this article:-
        Last edited by abctech; Jan 20 '07, 12:46 PM. Reason: submitted too soon

        Comment

        • hirak1984
          Contributor
          • Jan 2007
          • 316

          #5
          hey thanx for the two links.they r really helpful.But i must mention another book,which also helped me,and i hope will help others with similar queries.
          it is SCJP 5 by Kathey Sierra and Bert Bates

          Comment

          • RohiniSrinivasan
            New Member
            • Jan 2007
            • 2

            #6
            OOPs is just an object oriented programming strategy.
            In OOPs all that we handle is in the form of objects.Even when we want to access a function,or a member of a class then we need the object of that particular class.Object is just an instance of a class or they are called as basic run time entities.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by hirak1984
              hey thanx for the two links.they r really helpful.But i must mention another book,which also helped me,and i hope will help others with similar queries.
              it is SCJP 5 by Kathey Sierra and Bert Bates
              Thanks for the other reference as well.

              Comment

              Working...