Calculator App Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deezle
    New Member
    • Nov 2007
    • 2

    #1

    Calculator App Help

    Hello,
    I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help and I'll leave my code in here
    =============== =============== =============== ============
    package chapfour;

    /**
    *
    * @author Deezle
    */
    import java.awt.*;
    import javax.swing.JBu tton;
    import javax.swing.JFr ame;
    import javax.swing.JTe xtField;
    import java.awt.event. ActionListener;
    import java.awt.event. ActionEvent;

    public class Calc2 {

    Container pane;
    static JFrame frame;
    static JButton back;
    static JButton ce;
    static JButton cc;
    static JButton mc;
    static JButton mr;
    static JButton ms;
    static JButton mplus;
    static JButton seven;
    static JButton eight;
    static JButton nine;
    static JButton four;
    static JButton three;
    static JButton five;
    static JButton six;
    static JButton one;
    static JButton two;
    static JButton slash;
    static JButton multi;
    static JButton div;
    static JButton sub;
    static JButton add;
    static JButton root;
    static JButton modulus;
    static JButton frac;
    static JButton equal;
    static JButton zero;
    static JButton plusmin;
    static JButton decimal;
    static JTextField tf2;
    static JTextField tf;

    double total = 0;
    double total2 = 0;
    String operation;
    double dTotal;
    /** Creates a new instance of Calc */
    public Calc2 () {
    frame = new JFrame ("Calculator ");
    pane = frame.getConten tPane ();
    pane.setLayout (new GridBagLayout ());
    tf = new JTextField ("0");
    GridBagConstrai nts c = new GridBagConstrai nts ();
    c.fill = GridBagConstrai nts.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 2;
    back = new JButton ("Back");
    back.addActionL istener (new MyListener ());
    pane.add (back, c);
    c.gridx = 3;
    c.gridy = 1;
    c.gridwidth = 1;
    ce = new JButton ("CE");
    ce.addActionLis tener (new MyListener ());
    pane.add (ce, c);
    c.gridx = 4;
    c.gridy = 1;
    cc = new JButton ("C");
    cc.addActionLis tener (new MyListener ());
    pane.add (cc, c);
    c.gridx = 0;
    c.gridy = 2;
    mc = new JButton ("MC");
    mc.addActionLis tener (new MyListener ());
    pane.add (mc, c);
    c.gridx = 0;
    c.gridy = 3;
    mr = new JButton ("MR");
    mr.addActionLis tener (new MyListener ());
    pane.add (mr, c);
    c.gridx = 0;
    c.gridy = 4;
    ms = new JButton ("MS");
    ms.addActionLis tener (new MyListener ());
    pane.add (ms, c);
    c.gridx = 0;
    c.gridy = 5;
    mplus = new JButton ("M+");
    mplus.addAction Listener (new MyListener ());
    pane.add (mplus, c);
    c.gridx = 1;
    c.gridy = 2;
    seven = new JButton ("7");
    seven.addAction Listener (new MyListener ());
    pane.add (seven, c);
    c.gridx = 2;
    c.gridy = 2;
    eight = new JButton ("8");
    eight.addAction Listener (new MyListener ());
    pane.add (eight, c);
    c.gridx = 3;
    c.gridy = 2;
    nine = new JButton ("9");
    nine.addActionL istener (new MyListener ());
    pane.add (nine, c);
    c.gridx = 1;
    c.gridy = 3;
    four = new JButton ("4");
    four.addActionL istener (new MyListener ());
    pane.add (four, c);
    c.gridx = 2;
    c.gridy = 3;
    five = new JButton ("5");
    five.addActionL istener (new MyListener ());
    pane.add (five, c);
    c.gridx = 1;
    c.gridy = 4;
    three = new JButton ("3");
    three.addAction Listener (new MyListener ());
    pane.add (three, c);
    c.gridx = 3;
    c.gridy = 3;
    six = new JButton ("6");
    six.addActionLi stener (new MyListener ());
    pane.add (six, c);
    c.gridx = 2;
    c.gridy = 4;
    two = new JButton ("2");
    two.addActionLi stener (new MyListener ());
    pane.add (two, c);
    c.gridx = 3;
    c.gridy = 4;
    one = new JButton ("1");
    one.addActionLi stener (new MyListener ());
    pane.add (one, c);
    c.gridx = 1;
    c.gridy = 5;
    zero = new JButton ("0");
    zero.addActionL istener (new MyListener ());
    pane.add (zero, c);
    c.gridx = 2;
    c.gridy = 5;
    plusmin = new JButton ("+/-");
    plusmin.addActi onListener (new MyListener ());
    pane.add (plusmin, c);
    c.gridx = 3;
    c.gridy = 5;
    decimal = new JButton (".");
    decimal.addActi onListener (new MyListener ());
    pane.add (decimal, c);
    c.gridx = 5;
    c.gridy = 1;
    slash = new JButton ("/");
    slash.addAction Listener (new MyListener ());
    pane.add (slash, c);
    c.gridx = 4;
    c.gridy = 2;
    multi = new JButton ("*");
    multi.addAction Listener (new MyListener ());
    pane.add (multi, c);
    c.gridx = 4;
    c.gridy = 3;
    div = new JButton ("/");
    slash.addAction Listener (new MyListener ());
    pane.add (slash, c);
    c.gridx = 4;
    c.gridy = 4;
    sub = new JButton ("-");
    sub.addActionLi stener (new MyListener ());
    pane.add (sub, c);
    c.gridx = 4;
    c.gridy = 5;
    add = new JButton ("+");
    add.addActionLi stener (new MyListener ());
    pane.add (add, c);
    c.gridx = 5;
    c.gridy = 2;
    root = new JButton ("sqrt");
    root.addActionL istener (new MyListener ());
    pane.add (root, c);
    c.gridx = 5;
    c.gridy = 3;
    modulus = new JButton ("%");
    modulus.addActi onListener (new MyListener ());
    pane.add (modulus, c);
    c.gridx = 5;
    c.gridy = 4;
    frac = new JButton ("1/x");
    frac.addActionL istener (new MyListener ());
    pane.add (frac, c);
    c.gridx = 5;
    c.gridy = 5;
    equal = new JButton ("=");
    equal.addAction Listener (new MyListener ());
    pane.add (equal, c);

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 6;
    tf.addActionLis tener (new MyListener ());
    pane.add (tf, c);
    tf2 = new JTextField();
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    tf2.addActionLi stener (new MyListener ());
    pane.add (tf2, c);
    frame.pack ();
    frame.setVisibl e (true);
    }
    /**
    * @param args the command line arguments
    */
    public static void main (String[] args) {
    Calc2 calc = new Calc2 ();
    // TODO code application logic here
    }
    class MyListener
    implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    if (e.getSource () == back) {
    tf.setText ("Backspace" );
    }
    if (e.getSource () == ce) {
    total = 0;
    tf.setText ("0");
    }
    if (e.getSource () == cc) {
    total = 0;
    tf.setText ("0");
    }
    if (e.getSource () == mc) {
    tf.setText ("MC");
    }
    if (e.getSource () == mr) {
    tf.setText ("MR");
    }
    if (e.getSource () == ms) {
    tf.setText ("MS");
    }
    if (e.getSource () == mplus) {
    tf.setText ("M+");
    }
    if (e.getSource () == seven) {
    total = (total*10)+7;
    tf.setText(""+t otal);
    }
    if (e.getSource () == eight) {
    total = (total*10)+8;
    tf.setText (""+total);
    }
    if (e.getSource () == nine) {
    total = (total*10)+9;
    tf.setText (""+total);
    }
    if (e.getSource () == four) {
    total = (total*10)+4;
    tf.setText (""+total);
    }
    if (e.getSource () == five) {
    total = (total*10)+5;
    tf.setText (""+total);
    }
    if (e.getSource () == six) {
    total = (total*10)+6;
    tf.setText (""+total);
    }
    if (e.getSource () == three) {
    total = (total*10)+3;
    tf.setText (""+total);
    }
    if (e.getSource () == two) {
    total = (total*10)+2;
    tf.setText (""+total);
    }
    if (e.getSource () == one) {
    total = (total*10)+1;
    tf.setText (""+total);
    }
    if (e.getSource () == zero) {
    total = (total*10);
    tf.setText (""+total);
    }
    if (e.getSource () == plusmin) {
    total*=-1;
    tf.setText (""+total);
    }
    if (e.getSource () == decimal) {
    tf.setText (".");
    }
    if (e.getSource () == slash) {
    total2 = total;
    total = 0;
    operation = "divide";
    }
    if (e.getSource () == multi) {
    total2 = total;
    total = 0;
    operation = "multiply";
    }
    if (e.getSource () == sub) {
    total2 = total;
    total = 0;
    operation = "subtract";
    }
    if (e.getSource () == add) {
    total2 = total;
    total = 0;
    operation = "add";
    }
    if (e.getSource () == root) {
    dTotal = Math.sqrt(total );
    tf.setText ("" + dTotal);
    }
    if (e.getSource () == modulus) {
    tf.setText ("%");
    }
    if (e.getSource () == frac) {
    tf.setText ("1/X");
    }
    if (e.getSource () == equal) {
    if (operation=="ad d"){
    total = total+total2;
    tf.setText(""+t otal);
    total = 0;
    total2 = 0;
    }
    if (operation=="mu ltiply"){
    total = total*total2;
    tf.setText(""+t otal);
    total = 0;
    total2 = 0;
    }
    if (operation=="di vide"){

    dTotal = (total2/ total);
    tf.setText(""+d Total);
    total = 0;
    total2 = 0;
    dTotal = 0;
    }
    if (operation=="su btract"){
    total = total2-total;
    tf.setText(""+t otal);
    total = 0;
    total2 = 0;
    }
    }
    }
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by deezle
    Hello,
    I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help and I'll leave my code in here
    Don't mind all that code; I assume that the division operator should divide both
    of its operands; like the multiplication operator multiplies them. What does the
    division operator do instead?

    kind regards,

    Jos

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      What isn't working about the division? Is it displaying nothing, displaying something incorrect, or giving you errors?

      Comment

      Working...