Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: My calculator doesn't work right....

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default My calculator doesn't work right....

    So when I use this calculator code when i do 8-6 it equals -2 and 6-8 equals 2, so its backwards. Is there a way I can fix this.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class JavaCalculator extends JFrame {
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JButton jbtNum1;
        private JButton jbtNum2;
        private JButton jbtNum3;
        private JButton jbtNum4;
        private JButton jbtNum5;
        private JButton jbtNum6;
        private JButton jbtNum7;
        private JButton jbtNum8;
        private JButton jbtNum9;
        private JButton jbtNum0;
        private JButton jbtAdd;
        private JButton jbtSubtract;
        private JButton jbtMultiply;
        private JButton jbtDivide;
        private JButton jbtSolve;
        private JButton jbtClear;
        private double TEMP;
        private double SolveTEMP;
    private JTextField jtfResult;
     
    Boolean addBool = false ;
    Boolean subBool = false ;
    Boolean divBool = false ;
    Boolean mulBool = false ;
     
     
    String display = "";
     
    public JavaCalculator() {
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4, 3));
        p1.add(jbtNum1 = new JButton("1"));
        p1.add(jbtNum2 = new JButton("2"));
        p1.add(jbtNum3 = new JButton("3"));
        p1.add(jbtNum4 = new JButton("4"));
        p1.add(jbtNum5 = new JButton("5"));
        p1.add(jbtNum6 = new JButton("6"));
        p1.add(jbtNum7 = new JButton("7"));
        p1.add(jbtNum8 = new JButton("8"));
        p1.add(jbtNum9 = new JButton("9"));
        p1.add(jbtNum0 = new JButton("0"));
        p1.add(jbtClear = new JButton("C"));
     
     
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(jtfResult = new JTextField(20));
        jtfResult.setHorizontalAlignment(JTextField.RIGHT);
        jtfResult.setEditable(false);
     
                JPanel p3 = new JPanel();
                p3.setLayout(new GridLayout(5, 1));
                p3.add(jbtAdd = new JButton("+"));
                p3.add(jbtSubtract = new JButton("-"));
                p3.add(jbtMultiply = new JButton("*"));
                p3.add(jbtDivide = new JButton("/"));
                p3.add(jbtSolve = new JButton("="));
     
        JPanel p = new JPanel();
        p.setLayout(new GridLayout());
        p.add(p2, BorderLayout.NORTH);
        p.add(p1, BorderLayout.SOUTH);
        p.add(p3, BorderLayout.EAST);
     
     
        add(p);
     
        jbtNum1.addActionListener(new ListenToOne());
        jbtNum2.addActionListener(new ListenToTwo());
        jbtNum3.addActionListener(new ListenToThree());
        jbtNum4.addActionListener(new ListenToFour());
        jbtNum5.addActionListener(new ListenToFive());
        jbtNum6.addActionListener(new ListenToSix());
        jbtNum7.addActionListener(new ListenToSeven());
        jbtNum8.addActionListener(new ListenToEight());
        jbtNum9.addActionListener(new ListenToNine());
        jbtNum0.addActionListener(new ListenToZero());
     
        jbtAdd.addActionListener(new ListenToAdd());
        jbtSubtract.addActionListener(new ListenToSubtract());
        jbtMultiply.addActionListener(new ListenToMultiply());
        jbtDivide.addActionListener(new ListenToDivide());
        jbtSolve.addActionListener(new ListenToSolve());
        jbtClear.addActionListener(new ListenToClear());
     
     
     
    } //JavaCaluclator()
     
    class ListenToClear implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //display = jtfResult.getText();
            jtfResult.setText("");
            addBool = false ;
            subBool = false ;
            mulBool = false ;
            divBool = false ;
     
            TEMP = 0;
            SolveTEMP =0 ;
        }
    }
     
    class ListenToOne implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "1");
        }
    }
    class ListenToTwo implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "2");
        }
    }
    class ListenToThree implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "3");
        }
    }
    class ListenToFour implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "4");
        }
    }
    class ListenToFive implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "5");
        }
    }
    class ListenToSix implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "6");
        }
    }
    class ListenToSeven implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "7");
        }
    }
    class ListenToEight implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "8");
        }
    }
    class ListenToNine implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "9");
        }
    }
    class ListenToZero implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            display = jtfResult.getText();
            jtfResult.setText(display + "0");
        }
    }
     
        class ListenToAdd implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
                        jtfResult.setText("");
                        addBool = true ;
     
        }
    }
    class ListenToSubtract implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
            jtfResult.setText("");
            subBool =true;
        }
    }
    class ListenToMultiply implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
            jtfResult.setText("");
            mulBool =true;
     
        }
    }
    class ListenToDivide implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
            jtfResult.setText("");
            divBool =true;
        }
    }
    class ListenToSolve implements ActionListener {
        public void actionPerformed(ActionEvent e) {
                        SolveTEMP = Double.parseDouble(  jtfResult.getText() );
                        if ( addBool == true  )
                            SolveTEMP = SolveTEMP + TEMP;
     
                        else if ( subBool == true  )
                            SolveTEMP = SolveTEMP - TEMP;
                        else if ( mulBool == true  )
                            SolveTEMP = SolveTEMP * TEMP;
                        else if ( divBool == true  )
                            SolveTEMP = SolveTEMP / TEMP;
            jtfResult.setText(  Double.toString( SolveTEMP ) );
     
            addBool = false ;
            subBool = false ;
            mulBool = false ;
            divBool = false ;
     
     
        }
    }
     
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JavaCalculator calc = new JavaCalculator();
        calc.pack();
        calc.setLocationRelativeTo(null);
                calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calc.setVisible(true);
    }
     
    } //JavaCalculator


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My calculator doesn't work right....

    Is this your code? Why isn't it commented? At least you've given your variables, methods, and classes decent names.

    Yes, you can fix it by finding the subtraction operation and either change the sign of the result or change the order of the operands.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My calculator doesn't work right....

    Are the operands being reversed before the operator is applied?
    The expression should be firstOperand operator secondOperand
    not secondOperand operator firstOperand

    This is a good example where poorly named variables can not help the programmer write good code
    and correct names will help.

    Change the variables names:
    TEMP to firstOperand
    SolveTEMP to secondOperand

    and look at the expressions where the results are calculted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code doesn't work
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2014, 04:24 AM
  2. [SOLVED] .equals doesn't work?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:20 AM
  3. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  4. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  5. pathSeparatorChar doesn't work
    By ordonezc78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 12:25 PM