From 13b8430d1d44ae2b761d0871af412262cf68ee08 Mon Sep 17 00:00:00 2001 From: cgmpinos Date: Mon, 13 Feb 2023 19:13:28 +0200 Subject: [PATCH 1/3] Add div and mod operations --- src/simplejavacalculator/Calculator.java | 28 +++++++++++++++- src/simplejavacalculator/UI.java | 22 +++++++++++-- .../CalculatorTest.java | 33 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 18ad91c..5ba9d50 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -19,7 +19,7 @@ public class Calculator { public enum BiOperatorModes { - normal, add, minus, multiply, divide , xpowerofy + normal, add, minus, multiply, divide , xpowerofy, div, mod } public enum MonoOperatorModes { @@ -52,6 +52,32 @@ private Double calculateBiImpl() { if (mode == BiOperatorModes.xpowerofy) { return pow(num1,num2); } + if (mode == BiOperatorModes.div) { + if(num1 % 1==0 && num2 % 1==0){ //check if both numbers are integers + double temp1 = num1; + double temp2 = num2; + int numa = (int) temp1; + int numb = (int) temp2; + int result = numa/numb; + return 1.0*result; + } + else{ + return NaN; + } + } + if (mode == BiOperatorModes.mod) { + if(num1 % 1==0 && num2 % 1==0){ //check if both numbers are integers + double temp1 = num1; + double temp2 = num2; + int numa = (int) temp1; + int numb = (int) temp2; + int result = numa % numb; + return 1.0*result; + } + else{ + return NaN; + } + } // never reach throw new Error(); diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 8e9cc4a..adaf3a2 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -51,7 +51,7 @@ public class UI implements ActionListener { private final JTextArea text; private final JButton but[], butAdd, butMinus, butMultiply, butDivide, butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy, - butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary; + butCos, butSin, butTan, butxpowerofy, butdiv, butmod, butlog, butrate, butabs, butBinary; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -100,7 +100,9 @@ public UI() throws IOException { butCos = new JButton("Cos"); butSin = new JButton("Sin"); butTan = new JButton("Tan"); - butxpowerofy = new JButton("x^y"); + butxpowerofy = new JButton("x^y"); + butdiv = new JButton("div"); + butmod = new JButton("mod"); butlog = new JButton("log10(x)"); butrate = new JButton("x%"); butabs = new JButton("abs(x)"); @@ -136,6 +138,8 @@ public void init() { butSin.setFont(font); butTan.setFont(font); butxpowerofy.setFont(font); + butdiv.setFont(font); + butmod.setFont(font); butlog.setFont(font); butrate.setFont(font); butabs.setFont(font); @@ -159,7 +163,7 @@ public void init() { panelSub3.add(but[6]); panelSub3.add(Box.createHorizontalStrut(15)); panelSub3.add(butMultiply); - panelSub3.add(butDivide); + panelSub3.add(butDivide); panel.add(panelSub3); panelSub4.add(but[7]); @@ -184,6 +188,8 @@ public void init() { panelSub7.add(butCos); panelSub7.add(butSin); panelSub7.add(butTan); + panelSub7.add(butdiv); + panelSub7.add(butmod); panel.add(panelSub7); panelSub8.add(butlog); @@ -199,6 +205,8 @@ public void init() { butMinus.addActionListener(this); butMultiply.addActionListener(this); butDivide.addActionListener(this); + butdiv.addActionListener(this); + butmod.addActionListener(this); butSquare.addActionListener(this); butSquareRoot.addActionListener(this); butOneDividedBy.addActionListener(this); @@ -256,6 +264,14 @@ public void actionPerformed(ActionEvent e) { writer(calc.calculateBi(Calculator.BiOperatorModes.divide, reader())); text.replaceSelection(butDivide.getText()); } + if (source == butdiv) { + writer(calc.calculateBi(Calculator.BiOperatorModes.div, reader())); + text.replaceSelection(butdiv.getText()); + } + if (source == butmod) { + writer(calc.calculateBi(Calculator.BiOperatorModes.mod, reader())); + text.replaceSelection(butmod.getText()); + } if (source == butxpowerofy) { writer(calc.calculateBi(Calculator.BiOperatorModes.xpowerofy, reader())); diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index 1ea20f7..a32b89e 100755 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -44,6 +44,39 @@ void calculateBiDivideTest() { calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); } + + @Test + void calculateBiDivTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.div, 5.0); + Assertions.assertEquals(2.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + calculator.calculateBi(Calculator.BiOperatorModes.div, 13.0); + Assertions.assertEquals(4.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); + calculator.calculateBi(Calculator.BiOperatorModes.div, 18.0); + Assertions.assertEquals(4.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 4.0)); + calculator.calculateBi(Calculator.BiOperatorModes.div, 5.1); + Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + calculator.calculateBi(Calculator.BiOperatorModes.div, 5.0); + Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.3)); + } + + + @Test + void calculateModTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.0); + Assertions.assertEquals(1.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 14.0); + Assertions.assertEquals(2.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 20.0); + Assertions.assertEquals(0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 4.0)); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.1); + Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.0); + Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.3)); + } + + @Test void calculateEqualTest() { From 4ad647cbb1f07d7668cc97128901455880c3c520 Mon Sep 17 00:00:00 2001 From: cgmpinos Date: Tue, 14 Feb 2023 12:19:03 +0200 Subject: [PATCH 2/3] add pi button and solve test fails with sin, cos, tan. --- src/simplejavacalculator/Calculator.java | 16 ++++++++++++---- src/simplejavacalculator/UI.java | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 5ba9d50..f7860ea 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -120,10 +120,16 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { return 1 / num; } if (newMode == MonoOperatorModes.cos) { - return Math.cos(Math.toRadians(num)); + double wantedCos; + wantedCos = Math.cos(num); + wantedCos= Math.round(wantedCos*10000000.0)/10000000.0; //round to 7th decimal place + return wantedCos; } if (newMode == MonoOperatorModes.sin) { - return Math.sin(Math.toRadians(num)); + double wantedSin; + wantedSin = Math.sin(num); + wantedSin= Math.round(wantedSin*10000000.0)/10000000.0; + return wantedSin; } if (newMode == MonoOperatorModes.tan) { if (num == 0 || num % 180 == 0) { @@ -132,8 +138,10 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (num % 90 == 0 && num % 180 != 0) { return NaN; } - - return Math.tan(Math.toRadians(num)); + double wantedTan; + wantedTan = Math.tan(num); + wantedTan = Math.round(wantedTan*10000000.0)/10000000.0; + return wantedTan; } if (newMode == MonoOperatorModes.log) { return log10(num); diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index adaf3a2..312767a 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -51,7 +51,7 @@ public class UI implements ActionListener { private final JTextArea text; private final JButton but[], butAdd, butMinus, butMultiply, butDivide, butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy, - butCos, butSin, butTan, butxpowerofy, butdiv, butmod, butlog, butrate, butabs, butBinary; + butCos, butSin, butTan, butxpowerofy, butdiv, butmod, butlog, butrate, butabs, butBinary, butPI; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -88,7 +88,8 @@ public UI() throws IOException { but = new JButton[10]; for (int i = 0; i < 10; i++) { but[i] = new JButton(String.valueOf(i)); - } + } + butPI = new JButton("π"); butAdd = new JButton("+"); butMinus = new JButton("-"); butMultiply = new JButton("*"); @@ -125,7 +126,8 @@ public void init() { for (int i = 0; i < 10; i++) { but[i].setFont(font); - } + } + butPI.setFont(font); butAdd.setFont(font); butMinus.setFont(font); butMultiply.setFont(font); @@ -176,6 +178,7 @@ public void init() { panelSub5.add(Box.createHorizontalStrut(92)); panelSub5.add(but[0]); + panelSub5.add(butPI); panelSub5.add(Box.createHorizontalStrut(210)); panel.add(panelSub5); @@ -200,7 +203,8 @@ public void init() { for (int i = 0; i < 10; i++) { but[i].addActionListener(this); - } + } + butPI.addActionListener(this); butAdd.addActionListener(this); butMinus.addActionListener(this); butMultiply.addActionListener(this); @@ -230,7 +234,10 @@ public void init() { public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); Double checkNum = null; - + if(source == butPI){ + text.replaceSelection("3.14159265358979323846"); + return; + } for (int i = 0; i < 10; i++) { if (source == but[i]) { text.replaceSelection(buttonValue[i]); From 750be43101f72da0282f2c8492aa714163038075 Mon Sep 17 00:00:00 2001 From: cgmpinos Date: Tue, 14 Feb 2023 13:12:15 +0200 Subject: [PATCH 3/3] add x factorial (x!) --- src/simplejavacalculator/Calculator.java | 33 ++++++++++++++----- src/simplejavacalculator/UI.java | 9 ++++- .../CalculatorTest.java | 15 +++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index f7860ea..3d735c5 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -23,7 +23,7 @@ public enum BiOperatorModes { } public enum MonoOperatorModes { - square, squareRoot, oneDividedBy, cos, sin, tan ,log , rate, abs + square, squareRoot, oneDividedBy, cos, sin, tan ,log , rate, abs, xfact } private Double num1, num2; @@ -122,25 +122,24 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (newMode == MonoOperatorModes.cos) { double wantedCos; wantedCos = Math.cos(num); - wantedCos= Math.round(wantedCos*10000000.0)/10000000.0; //round to 7th decimal place + wantedCos= Math.round(wantedCos*10000000000.0)/10000000000.0; //round to 10th decimal place return wantedCos; } if (newMode == MonoOperatorModes.sin) { double wantedSin; wantedSin = Math.sin(num); - wantedSin= Math.round(wantedSin*10000000.0)/10000000.0; + wantedSin= Math.round(wantedSin*10000000000.0)/10000000000.0; return wantedSin; } if (newMode == MonoOperatorModes.tan) { - if (num == 0 || num % 180 == 0) { - return 0.0; - } - if (num % 90 == 0 && num % 180 != 0) { + double cos = Math.cos(num); + cos = Math.round(cos*10000000000.0)/10000000000.0; + if(cos==0.0){ return NaN; } double wantedTan; wantedTan = Math.tan(num); - wantedTan = Math.round(wantedTan*10000000.0)/10000000.0; + wantedTan = Math.round(wantedTan*10000000000.0)/10000000000.0; return wantedTan; } if (newMode == MonoOperatorModes.log) { @@ -152,6 +151,24 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (newMode == MonoOperatorModes.abs){ return Math.abs(num); } + if(newMode == MonoOperatorModes.xfact){ + if(num % 1==0 && num>=0 && num<=20){ //check if num is integer and 0<=num<=20. For numbers>= 20! long int is not enough to calculate + if(num==1 || num==0){ + return 1.0; + } + long result =1; + for(int i=2;i<=num;i++){ + result = result*i; + } + double doubleResult = result*1.0; + return doubleResult; + } + else{ + return NaN; + } + + + } // never reach throw new Error(); diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 312767a..52c85ee 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -51,7 +51,7 @@ public class UI implements ActionListener { private final JTextArea text; private final JButton but[], butAdd, butMinus, butMultiply, butDivide, butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy, - butCos, butSin, butTan, butxpowerofy, butdiv, butmod, butlog, butrate, butabs, butBinary, butPI; + butCos, butSin, butTan, butxpowerofy, butdiv, butmod, butlog, butrate, butabs, butBinary, butPI, butxfact; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -102,6 +102,7 @@ public UI() throws IOException { butSin = new JButton("Sin"); butTan = new JButton("Tan"); butxpowerofy = new JButton("x^y"); + butxfact = new JButton("x!"); butdiv = new JButton("div"); butmod = new JButton("mod"); butlog = new JButton("log10(x)"); @@ -140,6 +141,7 @@ public void init() { butSin.setFont(font); butTan.setFont(font); butxpowerofy.setFont(font); + butxfact.setFont(font); butdiv.setFont(font); butmod.setFont(font); butlog.setFont(font); @@ -186,6 +188,7 @@ public void init() { panelSub6.add(butSquareRoot); panelSub6.add(butOneDividedBy); panelSub6.add(butxpowerofy); + panelSub6.add(butxfact); panel.add(panelSub6); panelSub7.add(butCos); @@ -218,6 +221,7 @@ public void init() { butSin.addActionListener(this); butTan.addActionListener(this); butxpowerofy.addActionListener(this); + butxfact.addActionListener(this); butlog.addActionListener(this); butrate.addActionListener(this); butabs.addActionListener(this); @@ -287,6 +291,9 @@ public void actionPerformed(ActionEvent e) { if (source == butSquare) { writer(calc.calculateMono(Calculator.MonoOperatorModes.square, reader())); } + if (source == butxfact){ + writer(calc.calculateMono(Calculator.MonoOperatorModes.xfact, reader())); + } if (source == butSquareRoot) writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, reader())); diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index a32b89e..378e9fc 100755 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -99,6 +99,21 @@ void CalculateMonoSquareTest() { Calculator calculator = new Calculator(); Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); } + + @Test + void CalculateMonXFactTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 0.0)); + Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 1.0)); + Assertions.assertEquals(6.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 3.0)); + Assertions.assertEquals(362880.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 9.0)); + Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, -5.0)); + Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 21.0)); + Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 56.0)); + Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 3.2)); + + + } @Test void CalculateMonoSquareRootTest() {