Results 1 to 5 of 5

Thread: help

  1. #1
    java_jay05 is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default help

    hi Guys!

    Please help me about my calculator project, I am using netbeans 6.1 i do not know how to code my program using java language,.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please ask your question in right place. And also in right way. No one here to write code for you. Have to make an attempt first.

  3. #3
    palanikumark is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Check this one....

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Calculator extends JPanel implements ActionListener {
    private JTextField display = new JTextField("0");

    private String buttonText = "789/456*123-0.=+";

    private double result = 0;

    private String operator = "=";

    private boolean calculating = true;

    public Calculator() {
    setLayout(new BorderLayout());

    display.setEditable(false);
    add(display, "North");

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(4, 4));

    for (int i = 0; i < buttonText.length(); i++) {
    JButton b = new JButton(buttonText.substring(i, i + 1));
    p.add(b);
    b.addActionListener(this);

    }
    add(p, "Center");
    }

    public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();
    if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) {
    if (calculating)
    display.setText(cmd);
    else
    display.setText(display.getText() + cmd);
    calculating = false;
    } else {
    if (calculating) {
    if (cmd.equals("-")) {
    display.setText(cmd);
    calculating = false;
    } else
    operator = cmd;
    } else {
    double x = Double.parseDouble(display.getText());
    calculate(x);
    operator = cmd;
    calculating = true;
    }
    }
    }

    private void calculate(double n) {
    if (operator.equals("+"))
    result += n;
    else if (operator.equals("-"))
    result -= n;
    else if (operator.equals("*"))
    result *= n;
    else if (operator.equals("/"))
    result /= n;
    else if (operator.equals("="))
    result = n;
    display.setText("" + result);
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Calculator");
    frame.setSize(200, 200);
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new Calculator());
    frame.show();
    }
    }

    Thanks

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when you posting next time. It's really helpful to all who reads this thread. :)

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    I'm confused. the OP used a useless subject, and showed no work.

    Is this supposed to be a code mill? or do we expect folks to actually do some work before asking questions?

Posting Permissions

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