Results 1 to 5 of 5
  1. #1
    SlickJava is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Problem with calculator

    The code below is my calculator, it does not work on minus and divide that well. When it divides, it answers are a decimal e.g. 6/3 = 0.5, when it subtracts, it adds a negative sign to the answer e.g. 5 - 3 = -2
    Java Code:
    import java.awt.*;
    
    
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator 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 Calculator() {
        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.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        	}
        });
        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("x"));
                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);
    
    
        getContentPane().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());
    
    
    
    } 
    
    class ListenToClear implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            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
        Calculator calc = new Calculator();
        calc.pack();
        calc.setLocationRelativeTo(null);
                calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calc.setVisible(true);
    }
    
    }
    Last edited by SlickJava; 12-18-2012 at 02:53 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Problem with calculator

    What does "does not work" mean?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SlickJava is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with calculator

    Quote Originally Posted by Tolls View Post
    What does "does not work" mean?
    it does not work on minus and divide that well. When it divides, it answers are a decimal e.g. 6/3 = 0.5, when it subtracts, it adds a negative sign to the answer e.g. 5 - 3 = -2

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Problem with calculator

    That looks to me like you have your left and rights the wrong way round.
    You seem to be doing 3/6 when you should be doing 6/3, and 3 - 5 when you should be doing 5 - 3.

    So look at the order you are working with your values.
    You might want to use some println() debugging statements in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SlickJava is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with calculator

    Quote Originally Posted by Tolls View Post
    That looks to me like you have your left and rights the wrong way round.
    You seem to be doing 3/6 when you should be doing 6/3, and 3 - 5 when you should be doing 5 - 3.

    So look at the order you are working with your values.
    You might want to use some println() debugging statements in there.
    Thank you sooooooooooo much! That helped a lot! Thanks!

Similar Threads

  1. Problem with grade calculator?
    By MysteryForumGuy in forum New To Java
    Replies: 3
    Last Post: 02-15-2012, 01:14 AM
  2. Volume Calculator Problem
    By Sathelan in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 04:21 AM
  3. Problem with NumberFormatException in calculator
    By kumarv75 in forum CLDC and MIDP
    Replies: 17
    Last Post: 06-28-2010, 12:47 PM
  4. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 06:53 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 04:01 PM

Posting Permissions

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