A Java calculator with standard operations
A desktop calculator built with Java Swing (NetBeans GUI) that supports standard arithmetic plus a few scientific functions, with smart output formatting and guardrails.
- Arithmetic:
+,−,×,÷ - Scientific:
x²(square)√x(square root)π×x(multiply current value by π)
- Input & Display UX:
- Sign toggle
+/− - Decimal guard (prevents multiple
.) - Backspace
<, C (clear entry), AC (full reset) - Output formatting:
- Trims trailing
.0(e.g.,12.0 → 12) - Caps display length to 10 characters while preserving scientific notation (e.g.,
1.23E9)
- Trims trailing
- Sign toggle
- Chaining: Pressing an operator stores the current number; selecting another operator applies the previous one (sequential evaluation).
- Polish: Prevents editing a finalized answer until new input; friendly warnings; a tiny easter egg button (“Don’t Push”).
- Main class:
finalproject.FormB(JFramewith right-alignedJTextFielddisplay and button grid) - State variables:
input1,input2,outcome- Operation flags:
mul,div,add,sub ansDisplay(locks display after=),limit(max input length),curOP(op sequence counter)
- Core methods:
operate()— executes+ / − / × / ÷oninput1andinput2extraOP(int)— appliesx²,√x,π×ximmediately to current displaycutzero()&cutLimit()— trims trailing.0and enforces 10-char display, respecting scientific notationtypeNumber(String)/delete()— controlled input & backspace behavior
Operator precedence is sequential (in the order you press buttons), not algebraic precedence.
