Results 1 to 4 of 4
  1. #1
    karl_koch_89 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Help making a calculator

    Here is the code I have. So far everything works the way I wanted it too except I am not sure how to make the decimal button work. For example, I can't punch in the number 5.3343, I can only put in whole numbers. If anyone knows how I can make it so I can use decimals, I would really appreciate the help. Thanks.

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    @SuppressWarnings("serial")
    public class Calculator extends JFrame 
                            implements ActionListener
    {
        public static final int WIDTH = 400;
        public static final int HEIGHT = 200;
        public static final int NUMBER_OF_DIGITS = 15;
    
        private JTextField resultField;
        private JTextField operandField;
        private double result = 0.0;
        private double operand = 0.0;
    
        public static void main(String[] args)
        {
            Calculator aCalculator = new Calculator( );
            aCalculator.setVisible(true);
        }
    
        public Calculator( )
        {
            setTitle("Calculator");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(WIDTH, HEIGHT);
            setLayout(new FlowLayout( ));
    
            JPanel textPanel = new JPanel( );
            textPanel.setLayout(new FlowLayout( ));
            resultField = 
                 new JTextField("Result", NUMBER_OF_DIGITS);
            resultField.setBackground(Color.WHITE);
            resultField.setEditable(false);
          
            operandField = 
                new JTextField("Operand", NUMBER_OF_DIGITS);        
            operandField.setBackground(Color.WHITE);
            operandField.setEditable(false);
            textPanel.add(operandField);
            textPanel.add(resultField);
            add(textPanel);
    
            JPanel buttonPanel = new JPanel( );
            buttonPanel.setBackground(Color.BLUE);
            buttonPanel.setLayout(new GridLayout(4,5)); 
    
            JButton oneButton = new JButton("1");
            oneButton.addActionListener(this);
            buttonPanel.add(oneButton); 
            JButton twoButton = new JButton("2");
            twoButton.addActionListener(this);
            buttonPanel.add(twoButton); 
            JButton threeButton = new JButton("3");
            threeButton.addActionListener(this);
            buttonPanel.add(threeButton); 
            JButton addButton = new JButton("+"); 
            addButton.addActionListener(this);
            buttonPanel.add(addButton);
            JButton blankButton = new JButton(" "); 
            blankButton.addActionListener(this);
            buttonPanel.add(blankButton); 
            
            JButton fourButton = new JButton("4");
            fourButton.addActionListener(this);
            buttonPanel.add(fourButton);        
            JButton fiveButton = new JButton("5"); 
            fiveButton.addActionListener(this);
            buttonPanel.add(fiveButton);
            JButton sixButton = new JButton("6"); 
            sixButton.addActionListener(this);
            buttonPanel.add(sixButton);        
            JButton subtractButton = new JButton("-"); 
            subtractButton.addActionListener(this);
            buttonPanel.add(subtractButton);
            JButton clearButton = new JButton("Clear"); 
            clearButton.addActionListener(this);
            buttonPanel.add(clearButton);        
    
            JButton sevenButton = new JButton("7");
            sevenButton.addActionListener(this);
            buttonPanel.add(sevenButton);        
            JButton eightButton = new JButton("8"); 
            eightButton.addActionListener(this);
            buttonPanel.add(eightButton);
            JButton nineButton = new JButton("9"); 
            nineButton.addActionListener(this);
            buttonPanel.add(nineButton);        
            JButton multButton = new JButton("x");
            multButton.addActionListener(this);
            buttonPanel.add(multButton);
            JButton resetButton = new JButton("Reset"); 
            resetButton.addActionListener(this);
            buttonPanel.add(resetButton);
            
            JButton signChangeButton = new JButton("+/-"); 
            signChangeButton.addActionListener(this);
            buttonPanel.add(signChangeButton);        
            JButton zeroButton = new JButton("0"); 
            zeroButton.addActionListener(this);
            buttonPanel.add(zeroButton);
            JButton decimalButton = new JButton(".");
            decimalButton.addActionListener(this);
            buttonPanel.add(decimalButton);
            JButton divButton = new JButton("/");
            divButton.addActionListener(this);
            buttonPanel.add(divButton);
            JButton blankButton2 = new JButton(" ");
            blankButton2.addActionListener(this);
            buttonPanel.add(blankButton2);
            
            add(buttonPanel, BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                assumingCorrectNumberFormats(e);
            }
            catch (NumberFormatException e2)
            {
                operandField.setText("Error: Reenter Number.");
            }
        }
    
    
        //Throws NumberFormatException.
        public void assumingCorrectNumberFormats(ActionEvent e)
        {
            String actionCommand = e.getActionCommand( );
            
            if (actionCommand.equals("1"))
            {
                operand = 1 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("2"))
            {
                operand = 2 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("3"))
            {
                operand = 3 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("4"))
            {
                operand = 4 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("5"))
            {
                operand = 5 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("6"))
            {
                operand = 6 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("7"))
            {
                operand = 7 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("8"))
            {
                operand = 8 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("9"))
            {
                operand = 9 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("0"))
            {
                operand = 0 + (operand*10);
            	operandField.setText(Double.toString(operand));
            }
            
            else if (actionCommand.equals("."))
            {
            	
            }
            
            else if (actionCommand.equals("+"))
            {
                result = result + stringToDouble(operandField.getText( ));
                operand = 0.0;
            	operandField.setText("");
                resultField.setText(Double.toString(result));
            }
            
            else if (actionCommand.equals("-"))
            {
                result = result - stringToDouble(operandField.getText( ));
                operand = 0.0;
            	operandField.setText("");
                resultField.setText(Double.toString(result));
            }
            
            else if (actionCommand.equals("x"))
            {
            	result = result * stringToDouble(operandField.getText( ));
                operand = 0.0;
            	operandField.setText("");
                resultField.setText(Double.toString(result));        	
            }
            
            else if (actionCommand.equals("/"))
            {
            	try
            	{
            		if (stringToDouble(operandField.getText( )) <= 1.0e-10 
            				&& stringToDouble(operandField.getText( )) >= -1.0e-10)
            		{
            			throw new DivisionByZeroException();
            		}
    
            		else
                    	result = result / stringToDouble(operandField.getText( ));
                    	operand = 0.0;
                    	operandField.setText("");
                    	resultField.setText(Double.toString(result));        	
            	}
            	
            	catch(DivisionByZeroException e1)
            	{
            	    	operandField.setText("Cannot divide by zero");
            	}		
            }
            
            else if (actionCommand.equals("+/-"))
            {
            	result = (-1) * stringToDouble(operandField.getText( ));
                resultField.setText(Double.toString(result));        	
            }
            
            else if (actionCommand.equals("Reset"))
            {
            	result = 0;
                resultField.setText("0.0");
            }
            
            else if (actionCommand.equals("Clear"))
            {
            	operand = 0;
                operandField.setText("");
            }
            
            else
                resultField.setText("Unexpected error.");
         }
        
        private static double stringToDouble(String stringObject)
        {
            return Double.parseDouble(stringObject.trim( ));
        }
    }
    
    @SuppressWarnings("serial")
    class DivisionByZeroException extends RuntimeException
    {
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    how to make the decimal button work
    can you describe what it means for the decimal button to "work"?
    Normally buttons have action listeners that are called when the button is pressed.
    Then what?

    A comment on your code:
    Have different action listeners for different groups of buttons.
    For example for the buttons: 0-9 the listener could extract the actionCommand's digit and do the math in one step, vs having the 10 steps you now have.
    Last edited by Norm; 08-25-2010 at 11:03 PM.

  3. #3
    karl_koch_89 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    A comment on your code:
    Have different action listeners for different groups of buttons.
    For example for the buttons: 0-9 the listener could extract the actionCommand's digit and do the math in one step, vs having the 10 steps you now have.
    Thank you Norm. I have some expirence with Java, but these swing applets are new to me. I appreciate your help. I knew my code was really long, but I understand what you are talking about. That should simplify what I am working on. Thanks again

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    too right it is long, you could have saved time and used an array of buttons and used a for loop.

    I think you are over thinking the whole problem as a whole. Have a review of your code

Similar Threads

  1. Fraction Calculator need help
    By askinne2 in forum New To Java
    Replies: 15
    Last Post: 07-10-2012, 03:33 AM
  2. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 11:16 PM
  3. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 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
  •