Results 1 to 4 of 4
  1. #1
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default I want to know if my code is correctly Object Oriented!!

    //Simple Calculator
    //Manuel Berrios
    //June 18, 2013

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

    public class Calculator extends JFrame
    {
    private long counter;
    private JButton[] button;
    private JPanel panel1;
    private JPanel panel2;
    private JTextField textf;

    public Calculator()
    {
    counter = 0;
    textf = new JTextField(500);
    button = new JButton[13];

    Panels_Buttons();
    Frames();
    }
    public void Frames()
    {
    setVisible(true);
    setSize(170,210);
    setTitle("Hello World");
    setResizable(false);

    add(panel1, BorderLayout.NORTH);
    add(panel2);

    }
    public void Panels_Buttons()
    {
    textf.setComponentOrientation(getComponentOrientat ion().RIGHT_TO_LEFT);

    FlowLayout fl = new FlowLayout();

    panel1 = new JPanel(new BorderLayout());
    panel1.add(textf);

    button[0] = new JButton("1");
    button[1] = new JButton("2");
    button[2] = new JButton("3");
    button[3] = new JButton("4");
    button[4] = new JButton("5");
    button[5] = new JButton("6");
    button[6] = new JButton("7");
    button[7] = new JButton("8");
    button[8] = new JButton("9");
    button[9] = new JButton("+");
    button[10] = new JButton("-");
    button[11] = new JButton("=");
    button[12] = new JButton("text");

    panel2 = new JPanel(new BorderLayout());
    panel2.setLayout(fl);
    panel2.setBorder(BorderFactory.createLineBorder(Co lor.black));
    panel2.setComponentOrientation(getComponentOrienta tion().LEFT_TO_RIGHT);

    for(int i=0;i<13;i++)
    {
    panel2.add(button[i]);
    }
    }
    public long getValue()//---(getValue)
    {
    return counter;
    }
    public JButton[] getButtons()//---(getButton)
    {
    return button;
    }
    public JTextField getTextField()//---(getString)
    {
    return textf;
    }
    public static void main(String[] args)
    {
    new Calc_Arithmetics();
    }

    }

  2. #2
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default Re: I want to know if my code is correctly Object Oriented!!

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

    public class Calc_Arithmetics implements ActionListener
    {
    private long counter;
    private JButton[] button;
    private JTextField textf;
    private boolean validate;
    private String symbol;

    public Calc_Arithmetics()
    {
    Calculator calc=new Calculator();

    symbol="";
    validate=false;
    counter=calc.getValue();
    button=calc.getButtons();
    textf=calc.getTextField();

    Settings();
    }
    public void Settings()//---Add Buttons Listeners
    {
    for(int i=0;i<13;i++)
    button[i].addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)//---Button Listeners
    {
    if(validate==false)
    {
    for(int i=0;i<9;i++)//---(numbers)
    {
    if(e.getSource()==button[i])
    textf.setText(textf.getText()+button[i].getText());
    }
    }
    else if(validate=true)
    {
    textf.setText("");
    validate=false;

    for(int i=0;i<9;i++)//---(numbers)
    {
    if(e.getSource()==button[i])
    textf.setText(textf.getText()+button[i].getText());
    }
    }
    if(e.getSource()==button[9])//---(+) setSum
    {
    if(validate==false)
    {
    if(textf.getText().equals(""))
    {}
    else
    counter=counter+Long.parseLong(textf.getText().toS tring());
    textf.setText(Long.toString(counter));
    }
    validate = true;
    symbol="+";
    }
    if(e.getSource()==button[10])//---(-) setSubtraction
    {
    if(validate==false)
    {
    if(textf.getText().equals(""))
    {}
    else if(counter==0)
    {
    counter=Long.parseLong(textf.getText().toString()) ;
    textf.setText(Long.toString(counter));
    }
    else
    counter=counter-Long.parseLong(textf.getText().toString());
    textf.setText(Long.toString(counter));
    }
    validate = true;
    symbol="-";
    }
    if(e.getSource()==button[11])//---(=) getEquality
    {
    if(validate==false)
    {
    if(textf.getText().equals(""))
    {}
    else if(symbol=="+")
    {
    counter=counter+Long.parseLong(textf.getText().toS tring());
    textf.setText(Long.toString(counter));
    }
    else if(symbol=="-")
    {
    counter=counter-Long.parseLong(textf.getText().toString());
    textf.setText(Long.toString(counter));
    }
    }
    validate = true;
    }
    if(e.getSource()==button[12])//---(test)
    {

    }
    }
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: I want to know if my code is correctly Object Oriented!!

    I doubt anyone will read a ton of unformatted code to answer a vague question.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: I want to know if my code is correctly Object Oriented!!

    Moved from New to Java.

    You've been told about the code tags in your last thread. Revisit it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help for Object Oriented Programming RMI
    By clapton in forum New To Java
    Replies: 0
    Last Post: 03-20-2012, 06:42 PM
  2. New to object-oriented
    By fluteattack in forum New To Java
    Replies: 6
    Last Post: 03-28-2011, 01:43 PM
  3. Help in object oriented programming .
    By renu in forum New To Java
    Replies: 18
    Last Post: 10-26-2010, 07:57 PM
  4. Object Oriented
    By GraemeH in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 07:50 PM
  5. Object- Oriented guide?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •