diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 18ad91c..3d735c5 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -19,11 +19,11 @@ public class Calculator { public enum BiOperatorModes { - normal, add, minus, multiply, divide , xpowerofy + normal, add, minus, multiply, divide , xpowerofy, div, mod } 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; @@ -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(); @@ -94,20 +120,27 @@ 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*10000000000.0)/10000000000.0; //round to 10th decimal place + return wantedCos; } if (newMode == MonoOperatorModes.sin) { - return Math.sin(Math.toRadians(num)); + double wantedSin; + wantedSin = Math.sin(num); + 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; } - - return Math.tan(Math.toRadians(num)); + double wantedTan; + wantedTan = Math.tan(num); + wantedTan = Math.round(wantedTan*10000000000.0)/10000000000.0; + return wantedTan; } if (newMode == MonoOperatorModes.log) { return log10(num); @@ -118,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 8e9cc4a..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, butlog, butrate, butabs, butBinary; + 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", @@ -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("*"); @@ -100,7 +101,10 @@ 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"); + butxfact = new JButton("x!"); + butdiv = new JButton("div"); + butmod = new JButton("mod"); butlog = new JButton("log10(x)"); butrate = new JButton("x%"); butabs = new JButton("abs(x)"); @@ -123,7 +127,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); @@ -136,6 +141,9 @@ public void init() { butSin.setFont(font); butTan.setFont(font); butxpowerofy.setFont(font); + butxfact.setFont(font); + butdiv.setFont(font); + butmod.setFont(font); butlog.setFont(font); butrate.setFont(font); butabs.setFont(font); @@ -159,7 +167,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]); @@ -172,6 +180,7 @@ public void init() { panelSub5.add(Box.createHorizontalStrut(92)); panelSub5.add(but[0]); + panelSub5.add(butPI); panelSub5.add(Box.createHorizontalStrut(210)); panel.add(panelSub5); @@ -179,11 +188,14 @@ public void init() { panelSub6.add(butSquareRoot); panelSub6.add(butOneDividedBy); panelSub6.add(butxpowerofy); + panelSub6.add(butxfact); panel.add(panelSub6); panelSub7.add(butCos); panelSub7.add(butSin); panelSub7.add(butTan); + panelSub7.add(butdiv); + panelSub7.add(butmod); panel.add(panelSub7); panelSub8.add(butlog); @@ -194,11 +206,14 @@ 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); butDivide.addActionListener(this); + butdiv.addActionListener(this); + butmod.addActionListener(this); butSquare.addActionListener(this); butSquareRoot.addActionListener(this); butOneDividedBy.addActionListener(this); @@ -206,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); @@ -222,7 +238,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]); @@ -256,6 +275,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())); @@ -264,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 1ea20f7..378e9fc 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() { @@ -66,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() {