By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

Some sort of confusion!!!

hirak1984
100+
P: 316
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.text.JTextComponent;
  12.  
  13. public class Examples extends JApplet implements ActionListener {
  14.     JTextField jtf, jtf1;
  15.  
  16.     JPasswordField pass = new JPasswordField(15);
  17.  
  18.     Container contentpane = getContentPane();
  19.  
  20.     String ss[] = new String[100];
  21.  
  22.     JTextField jtf2, jtf3;
  23.  
  24.     JLabel label1 = new JLabel("FIRST FILE");
  25.  
  26.     JLabel label2 = new JLabel("SECOND FILE");
  27.  
  28.     String s1[] = new String[2];
  29.  
  30.     int i = 0, flag = 0,back=0;
  31.     //Frame frame1=new JFrame("LOGIN PAGE");
  32.  
  33.     Container contentPane =getContentPane();
  34.  
  35.     public void init() {
  36.  
  37.         jtf = new JTextField(3);
  38.         jtf1 = new JTextField(15);
  39.         JPanel p1 = new JPanel(new GridLayout(3, 1));
  40.         p1.add(new JLabel("SID"));
  41.         p1.add(jtf);
  42.         p1.add(new JLabel("User name:"));
  43.  
  44.         System.out.println(jtf.getText());
  45.         p1.add(jtf1);
  46.         p1.add(new JLabel("Password:"));
  47.         p1.add(pass);
  48.         contentPane.add("North", p1);
  49.         JButton submit = new JButton("SUBMIT");
  50.         submit.setActionCommand("submit");
  51.         submit.addActionListener(this);
  52.         // submit.doClick();
  53.         JButton reset = new JButton("RESET");
  54.         reset.setActionCommand("reset");
  55.         reset.addActionListener(this);
  56.         // reset.doClick();
  57.         JPanel p2 = new JPanel();
  58.         p2.add(submit);
  59.  
  60.         p2.add(reset);
  61.         contentPane.add("Center", p2);
  62.         // setSize(240, 120);
  63.  
  64.     }
  65.  
  66.     public void actionPerformed(ActionEvent ae) {
  67.         String comm = null;
  68.         int i = 0;
  69.         comm = ae.getActionCommand();
  70.         if (comm == "submit" && i == 0) {
  71.             i++;
  72.             getValue(jtf.getText(), jtf1.getText(), pass.getText());
  73.             // System.out.println(pass.getText());
  74.         }
  75.         if (comm == "reset") {
  76.             jtf.setText("");
  77.             jtf1.setText("");
  78.             pass.setText("");
  79.  
  80.             return;
  81.         }
  82.  
  83.         if (comm == "submit1") {
  84.  
  85.         }
  86.         if (flag == 1 && comm != "submit1"&&comm!="submit") {
  87.  
  88.             s1[0] = comm;
  89.             jtf2.setText(comm);
  90.         }
  91.         if (flag == 0 && comm != "submit1"&&comm!="submit") {
  92.             s1[1] = comm;
  93.             jtf3.setText(comm);
  94.         }
  95.         flag++;
  96.         if (flag == 2)
  97.             flag = 0;
  98.  
  99.     }
  100.  
  101.     public void getValue(String sid, String user, String pass) {
  102.         // System.out.println(pass);
  103.         String s1 = "jdbc:oracle:thin:@157.227.93.32:1521:"+sid;
  104.         int i = 0;
  105.         try {
  106.             // System.out.println(pass);
  107.             Class.forName("oracle.jdbc.driver.OracleDriver");
  108.             Connection conn = DriverManager.getConnection(s1, user,pass);
  109.             Statement stmt = conn.createStatement();
  110.             ResultSet rs = stmt
  111.                     .executeQuery("select distinct TABLE_NAME  from  USER_CONSTRAINTS");
  112.             while (rs != null && rs.next()) {
  113.                 // System.out.println(pass);
  114.                 ss[i] = rs.getString("TABLE_NAME");
  115.                 i++;
  116.             }
  117.  
  118.         } catch (Exception e) {
  119.             JOptionPane.showConfirmDialog(null, 
  120.                     "ERROR LOGIN", "ERROR IN LOGIN,TRY AGAIN?", JOptionPane.YES_NO_OPTION);
  121.  
  122.  
  123.             //e.printStackTrace();
  124.         }
  125.         int ii = 0;
  126.         JFrame theFrame = new JFrame("SELECT TWO TABLES");
  127.         theFrame.pack();
  128.         theFrame.setVisible(true);
  129.         Container content = theFrame.getContentPane();
  130.         jtf2 = new JTextField("FIRST FILE", 15);
  131.         jtf2.setEditable(false);
  132.         jtf2.setBackground(Color.WHITE);
  133.         jtf3 = new JTextField("SECOND FILE", 15);
  134.         jtf3.setEditable(false);
  135.         jtf3.setBackground(Color.WHITE);
  136.         content.setLayout(new FlowLayout());
  137.         label1.setLabelFor(jtf);
  138.         label2.setLabelFor(jtf1);
  139.         JButton jb[] = new JButton[i];
  140.         JButton jb1 = new JButton(
  141.                 "<html><body  ><font  type=Comic Sans MS size=5><b><i>SUBMIT</i></b></font></body></html>");
  142.         // ImageIcon icon=new ImageIcon("action.gif");
  143.         content.add(label1);
  144.         content.add(jtf2);
  145.         content.add(label2);
  146.         content.add(jtf3);
  147.  
  148.         // contentPane.add(label2);
  149.         /*
  150.          * label2.setAlignmentX(10); label2.setAlignmentY(3);
  151.          */
  152.         for (ii = 0; ii < i; ii++) {
  153.  
  154.             jb[ii] = new JButton(
  155.                     "<html><body ><font  color=black type=Comic Sans MS size=4><b><i>"
  156.                             + ss[ii] + "</i></b></font></body></html>");
  157.             jb[ii].setActionCommand(ss[ii]);
  158.             jb[ii].addActionListener(this);
  159.             jb[ii].doClick();
  160.             jb[ii]
  161.                     .setToolTipText("<html><body><font  color=black type=Comic Sans MS size=4><b><i>"
  162.                             + ss[ii] + "</i></b></font></body></html>");
  163.             jb[ii].setBackground(Color.CYAN);
  164.             jb[ii].setBorderPainted(true);
  165.  
  166.             jb[ii].setRequestFocusEnabled(true);
  167.             content.add(jb[ii]);
  168.  
  169.         }
  170.  
  171.         jb1.setActionCommand("submit1");
  172.         jb1.addActionListener(this);
  173.         jb1.doClick();
  174.         jb1
  175.                 .setToolTipText("<html><body bgcolor ><font  type=Comic Sans MS size=5><b><i>SUBMIT</i></b></font></body></html>");
  176.         jb1.setBackground(Color.red);
  177.         content.add(jb1);
  178.  
  179.     }
  180.  
  181. }
  182.  
Jan 19 '07 #1
Share this Question
Share on Google+
6 Replies


hirak1984
100+
P: 316
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);

}

}
Jan 19 '07 #2

10K+
P: 13,264
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);

}

}
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.
Jan 19 '07 #3

100+
P: 157
Hi hirak1984

To get an idea about the basic difference read this article:-
http://en.wikipedia.org/wiki/Procedural_programming
Jan 20 '07 #4

hirak1984
100+
P: 316
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
Jan 22 '07 #5

P: 2
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.
Jan 22 '07 #6

10K+
P: 13,264
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.
Jan 22 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.