diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 18ad91c..51e1fb8 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -15,6 +15,7 @@ import static java.lang.Math.log; import static java.lang.Math.log10; import static java.lang.Math.pow; +import static java.lang.Math.exp; public class Calculator { @@ -23,7 +24,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, fact, e } private Double num1, num2; @@ -118,7 +119,15 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (newMode == MonoOperatorModes.abs){ return Math.abs(num); } - + if (newMode == MonoOperatorModes.fact) { + int fact = 1; + for(int i = 1;i <= num; i++) { + fact=fact*i; + } + } + if (newMode == MonoOperatorModes.e) { + return Math.exp(num); + } // never reach throw new Error(); } diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 8e9cc4a..a2f98e7 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, butFact, butE; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -105,7 +105,9 @@ public UI() throws IOException { butrate = new JButton("x%"); butabs = new JButton("abs(x)"); butCancel = new JButton("C"); - butBinary = new JButton("Bin"); + butBinary = new JButton("Bin"); + butFact = new JButton("x!"); + butE = new JButton("e^x"); calc = new Calculator(); @@ -141,6 +143,8 @@ public void init() { butabs.setFont(font); butCancel.setFont(font); butBinary.setFont(font); + butFact.setFont(font); + butE.setFont(font); panel.add(Box.createHorizontalStrut(100)); panelSub1.add(text); @@ -166,8 +170,11 @@ public void init() { panelSub4.add(but[8]); panelSub4.add(but[9]); panelSub4.add(Box.createHorizontalStrut(15)); + panelSub4.add(butFact); + panelSub4.add(butE); panelSub4.add(butEqual); panelSub4.add(butCancel); + panel.add(panelSub4); panelSub5.add(Box.createHorizontalStrut(92)); @@ -211,6 +218,8 @@ public void init() { butabs.addActionListener(this); butBinary.addActionListener(this); + butFact.addActionListener(this); + butE.addActionListener(this); butEqual.addActionListener(this); butCancel.addActionListener(this); @@ -288,6 +297,12 @@ public void actionPerformed(ActionEvent e) { if (source == butabs) writer(calc.calculateMono(Calculator.MonoOperatorModes.abs, reader())); + + if (source == butFact) + writer(calc.calculateMono(Calculator.MonoOperatorModes.fact, reader())); + + if (source == butE) + writer(calc.calculateMono(Calculator.MonoOperatorModes.e, reader())); if (source == butEqual) writer(calc.calculateEqual(reader())); diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index 1ea20f7..082e6c4 100755 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -115,5 +115,21 @@ void CalculateMonoAbsTest() { Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); } + + @Test + void CalculateMonoFactTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(6, calculator.calculateMono(Calculator.MonoOperatorModes.fact, 3.0)); + Assertions.assertEquals(120, calculator.calculateMono(Calculator.MonoOperatorModes.fact, 5.0)); + } + + @Test + void CalculateMonoFactE() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(1, calculator.calculateMono(Calculator.MonoOperatorModes.e, 0.0)); + Assertions.assertEquals(54.59815003, calculator.calculateMono(Calculator.MonoOperatorModes.e, 4.0)); + } + + }