Scientific Calculator Source Code In Java Free Download Apr 2026

// 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++) char c = expr.charAt(i); if (Character.isDigit(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) op2 == ')') return false; if ((op1 == '*' 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;

public String calculateUnary(String operation, String value, boolean isDegree) try double num = Double.parseDouble(value); double result = 0; switch (operation) ": result = Math.abs(num); break; case "floor": result = Math.floor(num); break; case "ceil": result = Math.ceil(num); break; case "!": result = factorial((int) num); break; case "%": result = num / 100; break; case "π": result = Math.PI; break; case "e": result = Math.E; break; case "rand": result = Math.random(); break; return String.valueOf(result); catch (Exception e) return "Error";

public ScientificCalculator() engine = new CalculatorEngine(); initializeUI(); setTitle("Scientific Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); pack(); setLocationRelativeTo(null);

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 " scientific calculator source code in java free download

public void clear() memory = 0;

public CalculatorEngine() this.memory = 0;

📋 Overview A scientific calculator built in Java Swing with support for basic arithmetic, trigonometric functions, logarithms, exponents, and more. This is a complete, ready-to-run project. 📁 Project Structure ScientificCalculator/ ├── src/ │ ├── ScientificCalculator.java │ ├── CalculatorEngine.java │ └── CalculatorUI.java ├── README.md └── build.bat (Windows) / build.sh (Linux/Mac) 💻 Complete Source Code 1. ScientificCalculator.java (Main Class) import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.math.BigDecimal; import java.math.RoundingMode; public class ScientificCalculator extends JFrame private JTextField displayField; private JPanel buttonPanel; private CalculatorEngine engine; private boolean isDegree = true; // true = DEG, false = RAD // Inner class for expression evaluation using Shunting-yard

public String calculate(String expression, boolean isDegree) try // Remove whitespace expression = expression.replaceAll("\\s", ""); // Evaluate expression double result = evaluateExpression(expression); // Format result if (Double.isNaN(result) catch (Exception e) return "Error";

private double evaluateExpression(String expression) return new ExpressionEvaluator().evaluate(expression);

public void setAngleMode(boolean degree) this.degreeMode = degree; ScientificCalculator

@echo off echo Compiling Scientific Calculator... javac -d bin src/*.java if %errorlevel%==0 ( echo Compilation successful! echo Running Calculator... java -cp bin ScientificCalculator ) else ( echo Compilation failed! ) pause

public static void main(String[] args) SwingUtilities.invokeLater(() -> try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception e) e.printStackTrace(); new ScientificCalculator().setVisible(true); );

public void clearEntry() // Implementation for clear entry