Sign in to follow this  

Creating a calculator in java

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a school project to where I have to create a basic calculator in java. I was able to do some of but I don't know how to have the calculator add, subtract, multiply, or devide and then display the results. can anyone help? Here's the code that I have so far. /* begin code package edu.itttech.labs.week09; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator extends JFrame { private static final long serialVersionUID = 1L; private static final int OPERATION_CLEAR = 10; private static final int OPERATION_ADD = 11; private static final int OPERATION_SUBTRACT = 12; private static final int OPERATION_MULTIPLY = 13; private static final int OPERATION_DIVIDE = 14; private static final int OPERATION_EQUALS = 15; private static final int OPERATION_DOT = 16; private JTextField field; private float total; boolean clearDisplay = false; private final Font CALC_FONT = new Font("monspaced", Font.PLAIN, 40); public Calculator() { setLayout(new BorderLayout()); ButtonMouseAdapter mouseAdapter = new ButtonMouseAdapter(); JPanel northPanel = new JPanel(new BorderLayout()); JPanel centerPanel = new JPanel(new GridLayout(4, 4)); JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); field = new JTextField(); field.setFont(CALC_FONT); northPanel.add(field); JButton btnSeven = new JButton("7"); btnSeven.addMouseListener(mouseAdapter); centerPanel.add(btnSeven); JButton btnEight = new JButton("8"); btnEight.addMouseListener(mouseAdapter); centerPanel.add(btnEight); JButton btnNine = new JButton("9"); btnNine.addMouseListener(mouseAdapter); centerPanel.add(btnNine); centerPanel.add(new JButton("/")); JButton btnFour = new JButton("4"); btnFour.addMouseListener(mouseAdapter); centerPanel.add(btnFour); JButton btnFive = new JButton("5"); btnFive.addMouseListener(mouseAdapter); centerPanel.add(btnFive); JButton btnSix = new JButton("6"); btnSix.addMouseListener(mouseAdapter); centerPanel.add(btnSix); centerPanel.add(new JButton("X")); JButton btnOne = new JButton("1"); btnOne.addMouseListener(mouseAdapter); centerPanel.add(btnOne); JButton btnTwo = new JButton("2"); btnTwo.addMouseListener(mouseAdapter); centerPanel.add(btnTwo); JButton btnThree = new JButton("3"); btnThree.addMouseListener(mouseAdapter); centerPanel.add(btnThree); centerPanel.add(new JButton("-")); JButton btnZero = new JButton("0"); btnZero.addMouseListener(mouseAdapter); centerPanel.add(btnZero); JButton btnDot = new JButton("."); btnDot.addMouseListener(mouseAdapter); centerPanel.add(btnDot); centerPanel.add(new JButton("=")); JButton btnAdd = new JButton("+"); btnAdd.addMouseListener(mouseAdapter); centerPanel.add(btnAdd); JButton btnClear = new JButton("Clear", new ImageIcon("eraser.png")); btnClear.addMouseListener(mouseAdapter); southPanel.add(btnClear); add(northPanel, BorderLayout.NORTH); add(centerPanel, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); } public static void main(String[] args) { Calculator app = new Calculator(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setSize(250, 300); app.setLocationRelativeTo(null); app.setResizable(false); app.setTitle("Calculator"); app.setVisible(true); } class ButtonMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { JButton button = (JButton) e.getSource(); int code = Integer.parseInt(button.getActionCommand()); switch (code) { case OPERATION_CLEAR: field.setText("0"); break; case OPERATION_ADD: total = Float.parseFloat(field.getText()); clearDisplay = true; break; case OPERATION_EQUALS: break; case OPERATION_SUBTRACT: break; case OPERATION_MULTIPLY: break; case OPERATION_DIVIDE: break; case OPERATION_DOT: field.setText(field.getText() + "."); break; case 1: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "1"); break; case 2: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "2"); break; case 3: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "3"); break; case 4: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "4"); break; case 5: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "5"); break; case 6: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "6"); break; case 7: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "7"); break; case 8: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "8"); break; case 9: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "9"); break; case 0: if(clearDisplay == true) field.setText(""); field.setText(field.getText() + "0"); } } } } */ end code

Share this post


Link to post
Share on other sites
One way to have things working easily could be to have 2 member variables for the first operand and the operator. When the user clicks an operator you store the current displayed value in the first operand variable. When the user clicks '=' you take the stored operand and evaluate the stored operator with the second operand (the currently displayed value) and show the result.
How you store the operand is up to you: you can simply use an enumerator and then switch/case, or you can create operators classes wich implement a Evaluate(float a, float b) method. You then store an instance of the appropriate class on the operator click and execute the Evaluate() method on '=' click.

Share this post


Link to post
Share on other sites

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this