From 7785325c6059578646e4577728cec81989563a04 Mon Sep 17 00:00:00 2001 From: PriscaTan Date: Tue, 9 Aug 2022 23:37:11 +0800 Subject: [PATCH 1/5] Adding cube in Calculator.java --- .classpath | 11 +++++++++++ .gitignore | 1 + .project | 17 +++++++++++++++++ src/simplejavacalculator/Calculator.java | 2 +- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .classpath create mode 100644 .project diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..539dba0 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index b0cbf6e..85e6efb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ # Mac OS System .DS_Store* ._* +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..02992da --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Simple-Java-Calculator + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 18ad91c..4678efa 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, cube } private Double num1, num2; From eab13e850e47f942b60ab9eb78e67e0212b5cbc8 Mon Sep 17 00:00:00 2001 From: PriscaTan Date: Tue, 9 Aug 2022 23:42:30 +0800 Subject: [PATCH 2/5] Adding cube button in UI.java --- src/simplejavacalculator/UI.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 8e9cc4a..2a325fb 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, butlog, butrate, butabs, butBinary, butCube; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -98,14 +98,15 @@ public UI() throws IOException { butSquare = new JButton("x*x"); butOneDividedBy = new JButton("1/x"); butCos = new JButton("Cos"); - butSin = new JButton("Sin"); + butSin = new JButton("Sin"); butTan = new JButton("Tan"); butxpowerofy = new JButton("x^y"); butlog = new JButton("log10(x)"); butrate = new JButton("x%"); butabs = new JButton("abs(x)"); butCancel = new JButton("C"); - butBinary = new JButton("Bin"); + butBinary = new JButton("Bin"); + butCube = new JButton("x^3"); calc = new Calculator(); From e6f51e9cdb6f31b7f87b862b0a82e827ad12a28a Mon Sep 17 00:00:00 2001 From: PriscaTan Date: Tue, 9 Aug 2022 23:58:18 +0800 Subject: [PATCH 3/5] Adding cube in Calculator.java (v2) --- src/simplejavacalculator/Calculator.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 4678efa..26bd7c2 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -87,6 +87,9 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (newMode == MonoOperatorModes.square) { return num * num; } + if (newMode == MonoOperatorModes.cube) { + return num * num * num; + } if (newMode == MonoOperatorModes.squareRoot) { return Math.sqrt(num); } From ad6db0548e5095d11307ba300553956c5415f22d Mon Sep 17 00:00:00 2001 From: PriscaTan Date: Tue, 9 Aug 2022 23:58:50 +0800 Subject: [PATCH 4/5] Adding cube button in UI.java (v2) --- src/simplejavacalculator/UI.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 2a325fb..7c8d70e 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -132,6 +132,7 @@ public void init() { butEqual.setFont(font); butSquareRoot.setFont(font); butSquare.setFont(font); + butCube.setFont(font); butOneDividedBy.setFont(font); butCos.setFont(font); butSin.setFont(font); @@ -177,6 +178,7 @@ public void init() { panel.add(panelSub5); panelSub6.add(butSquare); + panelSub6.add(butCube); panelSub6.add(butSquareRoot); panelSub6.add(butOneDividedBy); panelSub6.add(butxpowerofy); @@ -201,6 +203,7 @@ public void init() { butMultiply.addActionListener(this); butDivide.addActionListener(this); butSquare.addActionListener(this); + butCube.addActionListener(this); butSquareRoot.addActionListener(this); butOneDividedBy.addActionListener(this); butCos.addActionListener(this); @@ -265,6 +268,10 @@ public void actionPerformed(ActionEvent e) { if (source == butSquare) { writer(calc.calculateMono(Calculator.MonoOperatorModes.square, reader())); } + + if (source == butCube) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.cube, reader())); + } if (source == butSquareRoot) writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, reader())); From 17f649b9b2833dc7aafe3b9178d53a9e074aa546 Mon Sep 17 00:00:00 2001 From: PriscaTan Date: Wed, 10 Aug 2022 00:00:00 +0800 Subject: [PATCH 5/5] Adding cube test function in CalculatorTest.java --- src/simplejavacalculatorTest/CalculatorTest.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index 1ea20f7..05438ab 100755 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -67,6 +67,12 @@ void CalculateMonoSquareTest() { Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); } + @Test + void CalculateMonoCubeTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(27.0, calculator.calculateMono(Calculator.MonoOperatorModes.cube, 3.0)); + } + @Test void CalculateMonoSquareRootTest() { Calculator calculator = new Calculator(); @@ -82,19 +88,19 @@ void CalculateMonoOneDividedByTest() { @Test void CalculateMonoSinTest() { Calculator calculator = new Calculator(); - Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001); + Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, Math.toDegrees(java.lang.Math.PI) / 6), 0.0000000001); } @Test void CalculateMonoCosTest() { Calculator calculator = new Calculator(); - Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, java.lang.Math.PI / 3), 0.0000000001); + Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, Math.toDegrees(java.lang.Math.PI) / 3), 0.0000000001); } @Test void CalculateMonoTanTest() { Calculator calculator = new Calculator(); - Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); + Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, Math.toDegrees(java.lang.Math.PI) / 4), 0.0000000001); } @Test @@ -115,5 +121,4 @@ void CalculateMonoAbsTest() { Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); } - }