Mga bro, good day!
Naa koy nahimo nga Calculator application using Java and I encountered a problem: Di mugawas ang sakto nga result because ang result nga mugawas kay pirmi 0.0
Note:
- Most sa mga code ani kay ako gahimo, although I borrowed some ideas from other programmers' calculator
- The calculator by itself is not yet complete and I wish to add more operations when this problem is already solved..
So, here's the code:
1st Class: Calculator
2nd Class: OperationCode:import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; public class Calculator extends JFrame implements ActionListener { final int MAX_INPUT_LENGTH = 20; private JPanel jplButtons, jplMaster, jplOperations; private JTextField output; private JButton jbtnButtons[]; private Operation operation; private double operandOne; private double operandTwo; private String operator = ""; private boolean clearOnNextDigit = false; public Calculator() { jplMaster = new JPanel(); jbtnButtons = new JButton[18]; jplButtons = new JPanel(); jplButtons.setLayout(new GridLayout(4, 5, 2, 2)); for (int i = 9; i >= 0; i--) { jbtnButtons[i] = new JButton(String.valueOf(i)); jplButtons.add(jbtnButtons[i]); jbtnButtons[i].addActionListener(this); } jplOperations = new JPanel(); jplOperations.setLayout(new GridLayout(4, 2, 2, 2)); jbtnButtons[10] = new JButton("+"); jbtnButtons[11] = new JButton("-"); jbtnButtons[12] = new JButton("*"); jbtnButtons[13] = new JButton("/"); jbtnButtons[14] = new JButton("%"); jbtnButtons[15] = new JButton("="); jbtnButtons[16] = new JButton("CE"); jbtnButtons[17] = new JButton("C"); for (int i = 10; i < jbtnButtons.length; i++){ jplOperations.add(jbtnButtons[i]); jbtnButtons[i].addActionListener(this); } output = new JTextField(MAX_INPUT_LENGTH); setOutput("0"); jplMaster.setLayout(new BorderLayout()); jplMaster.add(output, BorderLayout.NORTH); jplMaster.add(jplButtons, BorderLayout.CENTER); jplMaster.add(jplOperations, BorderLayout.EAST); add(jplMaster); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public String getOutput() { return output.getText(); } public void setOutput(String string) { output.setText(string); } public void addDigit (int digit) { if (clearOnNextDigit) { output.setText(""); } String currentDisplay = getOutput(); if (currentDisplay.equals("0")){ currentDisplay = ""; } setOutput(currentDisplay + digit); clearOnNextDigit = false; } public void processOperator(String operator) { operandOne = getNumberInDisplay(); clearOnNextDigit = true; this.operator = operator; } public double getNumberInDisplay() { String input = getOutput(); return Double.parseDouble(input); } public void clearAll() { setOutput("0"); operator = "0"; operandOne = operandTwo = 0; clearOnNextDigit = true; } public void clearExisting() { setOutput("0"); clearOnNextDigit = true; } public static void main(String args[]) { Calculator calculator = new Calculator(); calculator.setResizable(false); calculator.setVisible(true); calculator.setTitle("My Calculator"); calculator.setSize(241, 217); calculator.setLocation(400, 250); calculator.setBackground(Color.gray); } public void actionPerformed(ActionEvent e) { String inputNumber = getOutput(); for (int i = 0; i < jbtnButtons.length; i++) { if (e.getSource() == jbtnButtons[i] && inputNumber.length() < MAX_INPUT_LENGTH){ switch (i) { case 0: addDigit(i); break; case 1: addDigit(i); break; case 2: addDigit(i); break; case 3: addDigit(i); break; case 4: addDigit(i); break; case 5: addDigit(i); break; case 6: addDigit(i); break; case 7: addDigit(i); break; case 8: addDigit(i); break; case 9: addDigit(i); break; case 10: processOperator("+"); break; case 11: processOperator("-"); break; case 12: processOperator("*"); break; case 13: processOperator("/"); break; case 14: processOperator("%"); break; case 15: operandTwo = getNumberInDisplay(); operation = new Operation(operandOne, operandTwo, operator); setOutput(Double.toString(operation.processOperation())); clearOnNextDigit = true; break; case 16: clearExisting(); break; case 17: clearAll(); break; } } } } }
Mga bro, I hope maka tabang mo in any way. I very much appreciate any suggestion and comments. Thanks!Code:public class Operation { private double firstOperand; private double secondOperand; private String operator; private Operation operate; public Operation() { } public Operation(double firstOperand, double secondOperand, String operator) { this.firstOperand = firstOperand; this.secondOperand = secondOperand; this.operator = operator; } public double calculate() { return 0; } public double getFirstOperand() { return firstOperand; } public double getSecondOperand() { return secondOperand; } public double processOperation(){ if (operator.equals("+")){ operate = new Addition(); } else if (operator.equals("-")){ operate = new Subtraction(); } else if (operator.equals("*")){ operate = new Multiplication(); } else if (operator.equals("/")){ operate = new Division(); } else { operate = new Modulo(); } return operate.calculate(); } class Addition extends Operation { @Override public double calculate() { return (super.getFirstOperand() + super.getSecondOperand()); } } class Subtraction extends Operation { @Override public double calculate() { return (super.getFirstOperand() - super.getSecondOperand()); } } class Multiplication extends Operation { @Override public double calculate() { return (super.getFirstOperand() * super.getSecondOperand()); } } class Division extends Operation { @Override public double calculate() { return (super.getFirstOperand() / super.getSecondOperand()); } } class Modulo extends Operation { @Override public double calculate() { return (super.getFirstOperand() % super.getSecondOperand()); } } }![]()




Reply With Quote

