From bbd59fa8d6d71ea3010728dc79e5da517de9a9c9 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Sun, 28 Sep 2025 12:00:06 +0300 Subject: [PATCH 1/3] singleton --- src/Calc/Calculator.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index 7e2a37c..d5196d1 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -3,6 +3,7 @@ import java.awt.Color; import java.awt.event.*; import javax.swing.JButton; +import java.beans.Beans; /** * @@ -15,8 +16,26 @@ public final class Calculator extends javax.swing.JFrame { private String operation; private int x, y; + + // --- Singleton --- + private static Calculator INSTANCE; + + public static Calculator getInstance() { + if (INSTANCE == null) { + INSTANCE = new Calculator(); + } + return INSTANCE; + } public Calculator() { + + if (INSTANCE != null && !Beans.isDesignTime()) { + throw new IllegalStateException("Use Calculator.getInstance()"); + } + if (INSTANCE == null) { + INSTANCE = this; + } + initComponents(); getContentPane().setSize(400, 700); this.clear(); From 314ab90a6e39d597b7fee5b623a1352be94c7382 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Sun, 28 Sep 2025 22:58:59 +0300 Subject: [PATCH 2/3] singleton and factorial --- src/Calc/App.java | 6 ++++-- src/Calc/Calculator.java | 44 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index 4101058..dc15a1d 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -7,8 +7,10 @@ public class App { - public static void main(String[] args) { - new Calculator().setVisible(true); + public static void main(String[] args) { + //deleting this line to test the singleton --> new Calculator().setVisible(true); + //testing the sigelton + Calculator.getInstance().setVisible(true); } } diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index d5196d1..bec9da6 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -17,7 +17,7 @@ public final class Calculator extends javax.swing.JFrame { private int x, y; - // --- Singleton --- + // --- Singleton Method --- private static Calculator INSTANCE; public static Calculator getInstance() { @@ -26,6 +26,29 @@ public static Calculator getInstance() { } return INSTANCE; } + + // --- Factory Method --- +@FunctionalInterface +private interface Operation {double apply(double a, double b); +} + +private Operation makeOperation(String symbol) { + switch (symbol) { + case "+" -> { + return (a, b) -> a + b; + } + case "-" -> { + return (a, b) -> a - b; + } + case "×", "*" -> { + return (a, b) -> a * b; + } + case "÷", "/" -> { + return (a, b) -> (b == 0) ? Double.NaN : a / b; + } + default -> throw new IllegalArgumentException("Unknown operation: " + symbol); + } +} public Calculator() { @@ -138,7 +161,9 @@ public void compute() { if (Float.isNaN(curr) || Float.isNaN(prev)) { return; } - + + //deleting the switch + /*.. switch (this.operation) { case "+" -> computation = prev + curr; @@ -158,6 +183,21 @@ public void compute() { return; } } +..*/ + + try { + if (("÷".equals(this.operation) || "/".equals(this.operation)) && curr == 0f) { + this.clear(); + this.currentOperand = "Error"; + return; + } + + Operation op = makeOperation(this.operation); + computation = (float) op.apply(prev, curr); + } catch (IllegalArgumentException ex) { + return; + } + this.currentOperand = (computation - (int) computation) != 0 ? Float.toString(computation) : Integer.toString((int) computation); this.previousOperand = ""; From 0699874870eb5b682cfedcf917aa5152eee5f7f1 Mon Sep 17 00:00:00 2001 From: sultanax100 Date: Mon, 29 Sep 2025 22:57:22 +0300 Subject: [PATCH 3/3] singleton and factorial --- src/Calc/App.java | 7 ++- src/Calc/Calculator.java | 99 +++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/Calc/App.java b/src/Calc/App.java index dc15a1d..75f9a61 100644 --- a/src/Calc/App.java +++ b/src/Calc/App.java @@ -10,7 +10,12 @@ public class App { public static void main(String[] args) { //deleting this line to test the singleton --> new Calculator().setVisible(true); //testing the sigelton - Calculator.getInstance().setVisible(true); + javax.swing.SwingUtilities.invokeLater(() -> { + Calculator calculator1 = Calculator.getInstance(); + Calculator calculator2 = Calculator.getInstance(); + System.out.println("same instance? " + (calculator1 == calculator2)); + calculator1.setVisible(true); + }); } } diff --git a/src/Calc/Calculator.java b/src/Calc/Calculator.java index bec9da6..a8561d8 100644 --- a/src/Calc/Calculator.java +++ b/src/Calc/Calculator.java @@ -3,7 +3,6 @@ import java.awt.Color; import java.awt.event.*; import javax.swing.JButton; -import java.beans.Beans; /** * @@ -14,7 +13,6 @@ public final class Calculator extends javax.swing.JFrame { private String currentOperand; private String previousOperand; private String operation; - private int x, y; // --- Singleton Method --- @@ -28,37 +26,48 @@ public static Calculator getInstance() { } // --- Factory Method --- -@FunctionalInterface -private interface Operation {double apply(double a, double b); + private interface Operation { float apply(float a, float b); } + + private static class AddOperation implements Operation { + @Override + public float apply(float a,float b){ + return a+b;} } -private Operation makeOperation(String symbol) { - switch (symbol) { - case "+" -> { - return (a, b) -> a + b; - } - case "-" -> { - return (a, b) -> a - b; - } - case "×", "*" -> { - return (a, b) -> a * b; - } - case "÷", "/" -> { - return (a, b) -> (b == 0) ? Double.NaN : a / b; + private static class SubOperation implements Operation { + @Override + public float apply(float a,float b){ + return a-b;} + } + + private static class MultOperation implements Operation { + @Override + public float apply(float a,float b){ + return a*b;} + } + + private static class DivOperation implements Operation { + @Override + public float apply(float a,float b){ + if (b == 0f) { + throw new ArithmeticException("Error! Division by zero is not accepted"); } - default -> throw new IllegalArgumentException("Unknown operation: " + symbol); + return a/b;} } -} - public Calculator() { - - if (INSTANCE != null && !Beans.isDesignTime()) { - throw new IllegalStateException("Use Calculator.getInstance()"); - } - if (INSTANCE == null) { - INSTANCE = this; + private static class OperationFactory { + static Operation getOperation(String op) { + switch (op) { + case "+": return new AddOperation(); + case "-": return new SubOperation(); + case "×": return new MultOperation(); + case "÷": return new DivOperation(); + default: throw new IllegalArgumentException("Unknown operation: " + op); + } } - + } + + private Calculator() { initComponents(); getContentPane().setSize(400, 700); this.clear(); @@ -151,18 +160,12 @@ public void chooseOperation(String operation) { } public void compute() { - float computation; + if (this.currentOperand.equals("") || this.previousOperand.equals("")) { return; } - - float curr = Float.parseFloat(this.currentOperand); - float prev = Float.parseFloat(this.previousOperand); - if (Float.isNaN(curr) || Float.isNaN(prev)) { - return; - } - //deleting the switch + //deleting the switch, we already have the operation interface /*.. switch (this.operation) { case "+" -> @@ -184,22 +187,22 @@ public void compute() { } } ..*/ - try { - if (("÷".equals(this.operation) || "/".equals(this.operation)) && curr == 0f) { - this.clear(); - this.currentOperand = "Error"; - return; - } - - Operation op = makeOperation(this.operation); - computation = (float) op.apply(prev, curr); - } catch (IllegalArgumentException ex) { + float curr = Float.parseFloat(this.currentOperand); + float prev = Float.parseFloat(this.previousOperand); + Operation op = OperationFactory.getOperation(this.operation); + float result = op.apply(prev, curr); + + this.currentOperand = (result - (int) result ) != 0 + ? Float.toString(result ) + : Integer.toString((int) result ); + + } catch (IllegalArgumentException | ArithmeticException ex) { + this.clear(); + this.currentOperand = "Error"; return; } - - - this.currentOperand = (computation - (int) computation) != 0 ? Float.toString(computation) : Integer.toString((int) computation); + this.previousOperand = ""; this.operation = ""; }