public void setAngleMode(boolean degree) this.degreeMode = degree;
public static void main(String[] args) SwingUtilities.invokeLater(() -> try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception e) e.printStackTrace(); new ScientificCalculator().setVisible(true); );
// Inner class for expression evaluation using Shunting-yard algorithm private class ExpressionEvaluator public double evaluate(String expression) return evaluateExpression(expression); private double evaluateExpression(String expr) Stack<Double> values = new Stack<>(); Stack<Character> operators = new Stack<>(); for (int i = 0; i < expr.length(); i++) c == '.') else if (c == '(') operators.push(c); else if (c == ')') while (operators.peek() != '(') values.push(applyOperation(operators.pop(), values.pop(), values.pop())); operators.pop(); else if (isOperator(c)) while (!operators.isEmpty() && hasPrecedence(c, operators.peek())) values.push(applyOperation(operators.pop(), values.pop(), values.pop())); operators.push(c); while (!operators.isEmpty()) values.push(applyOperation(operators.pop(), values.pop(), values.pop())); return values.pop(); private boolean isOperator(char c) private boolean hasPrecedence(char op1, char op2) private double applyOperation(char op, double b, double a) switch (op) case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (b == 0) throw new ArithmeticException("Division by zero"); return a / b; case '%': return a % b; default: return 0; scientific calculator source code in java free download
public void clear() memory = 0;
(Logic Engine) import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Stack; public class CalculatorEngine private double memory; private boolean degreeMode = true; public void setAngleMode(boolean degree) this
# Save the code as ScientificCalculator.java javac ScientificCalculator.java CalculatorEngine.java java ScientificCalculator For any issues, check that all three Java files are in the same directory and compiled together. The calculator will open as a standalone window with full scientific functionality.
private class ButtonClickListener implements ActionListener private String command; public ButtonClickListener(String command) this.command = command; @Override public void actionPerformed(ActionEvent e) String currentText = displayField.getText(); switch (command) case "C": engine.clear(); displayField.setText("0"); break; case "CE": engine.clearEntry(); displayField.setText("0"); break; case "=": try String result = engine.calculate(currentText, isDegree); displayField.setText(result); catch (Exception ex) displayField.setText("Error"); break; case "+/-": if (currentText.startsWith("-")) displayField.setText(currentText.substring(1)); else if (!currentText.equals("0")) displayField.setText("-" + currentText); break; case "sin": case "cos": case "tan": case "asin": case "acos": case "atan": case "sinh": case "cosh": case "tanh": case "log": case "ln": case "√": case "∛": case "x²": case "x³": case "1/x": case " catch (Exception e) e.printStackTrace()
public String calculateUnary(String operation, String value, boolean isDegree) try double num = Double.parseDouble(value); double result = 0; switch (operation) x return String.valueOf(result); catch (Exception e) return "Error";
public CalculatorEngine() this.memory = 0;
public ScientificCalculator() engine = new CalculatorEngine(); initializeUI(); setTitle("Scientific Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); pack(); setLocationRelativeTo(null);
private void addButtons() GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1; gbc.weighty = 1; gbc.insets = new Insets(2, 2, 2, 2); String[][] buttons = "sin", "cos", "tan", "asin", "acos", "atan", "sinh", "cosh", "tanh", "log", "ln", "√", "x²", "x³", "xʸ", "eˣ", "10ˣ", "∛", ", "floor", "ceil", "!", "%", "π", "e", "(", ")", "C", "CE", "7", "8", "9", "/", "mod", "rand", "4", "5", "6", "*", "xʸ", "xʸ", "1", "2", "3", "-", "xʸ", "xʸ", "0", ".", "+/-", "+", "=", "xʸ" ; int row = 0; for (String[] buttonRow : buttons) int col = 0; for (String btnText : buttonRow) gbc.gridx = col; gbc.gridy = row; JButton button = createStyledButton(btnText); // Special sizing for equals button if (btnText.equals("=")) gbc.gridheight = 2; button.setBackground(new Color(76, 175, 80)); button.setForeground(Color.WHITE); else gbc.gridheight = 1; button.addActionListener(new ButtonClickListener(btnText)); buttonPanel.add(button, gbc); col++; row++; gbc.gridheight = 1; // Reset