diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..b607183 --- /dev/null +++ b/.classpath @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/.github/workflows/devskim.yml b/.github/workflows/devskim.yml new file mode 100644 index 0000000..99f2ea4 --- /dev/null +++ b/.github/workflows/devskim.yml @@ -0,0 +1,34 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: DevSkim + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + schedule: + - cron: '32 13 * * 0' + +jobs: + lint: + name: DevSkim + runs-on: ubuntu-20.04 + permissions: + actions: read + contents: read + security-events: write + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Run DevSkim scanner + uses: microsoft/DevSkim-Action@v1 + + - name: Upload DevSkim scan results to GitHub Security tab + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: devskim-results.sarif 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..9b9f376 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + SimpleJavaCalculator + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + + + 1692244280313 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..9478cb1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b590801 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM jenkins/jenkins:2.401.3-jdk17 +USER root + +RUN apt-get update && apt-get install -y lsb-release +RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \ + https://download.docker.com/linux/debian/gpg +RUN echo "deb [arch=$(dpkg --print-architecture) \ + signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \ + https://download.docker.com/linux/debian \ + $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list +RUN apt-get update && apt-get install -y docker-ce-cli + +USER jenkins +RUN jenkins-plugin-cli --plugins "blueocean docker-workflow" \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..46c1bb1 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,24 @@ +pipeline { + agent any + stages{ + stage('Build docker image'){ + steps{ + script{ + sh 'docker build -t siowyenchong/simple-java-calculator .' + } + } + } + stage('Push image to Hub'){ + steps{ + script{ + withCredentials([string(credentialsId: 'dockerhub-pwd', variable: 'dockerhubpwd')]) { + sh 'docker login -u siowyenchong -p ${dockerhubpwd}' + } + sh 'docker push siowyenchong/simple-java-calculator' + } + } + } + } +} + + diff --git a/bin/module-info.class b/bin/module-info.class new file mode 100644 index 0000000..ef51cbd Binary files /dev/null and b/bin/module-info.class differ diff --git a/bin/resources/icon/icon-notice.txt b/bin/resources/icon/icon-notice.txt new file mode 100644 index 0000000..d2577c2 --- /dev/null +++ b/bin/resources/icon/icon-notice.txt @@ -0,0 +1 @@ +`icon.png`, all rights reserved by xdvrx1 diff --git a/bin/resources/icon/icon.png b/bin/resources/icon/icon.png new file mode 100644 index 0000000..527af6f Binary files /dev/null and b/bin/resources/icon/icon.png differ diff --git a/bin/simplejavacalculator/BufferedImageCustom.class b/bin/simplejavacalculator/BufferedImageCustom.class new file mode 100644 index 0000000..ae8edfb Binary files /dev/null and b/bin/simplejavacalculator/BufferedImageCustom.class differ diff --git a/bin/simplejavacalculator/Calculator$BiOperatorModes.class b/bin/simplejavacalculator/Calculator$BiOperatorModes.class new file mode 100644 index 0000000..4d39039 Binary files /dev/null and b/bin/simplejavacalculator/Calculator$BiOperatorModes.class differ diff --git a/bin/simplejavacalculator/Calculator$MonoOperatorModes.class b/bin/simplejavacalculator/Calculator$MonoOperatorModes.class new file mode 100644 index 0000000..6db8166 Binary files /dev/null and b/bin/simplejavacalculator/Calculator$MonoOperatorModes.class differ diff --git a/bin/simplejavacalculator/Calculator.class b/bin/simplejavacalculator/Calculator.class new file mode 100644 index 0000000..461dbcb Binary files /dev/null and b/bin/simplejavacalculator/Calculator.class differ diff --git a/bin/simplejavacalculator/SimpleJavaCalculator.class b/bin/simplejavacalculator/SimpleJavaCalculator.class new file mode 100644 index 0000000..cfcf44d Binary files /dev/null and b/bin/simplejavacalculator/SimpleJavaCalculator.class differ diff --git a/bin/simplejavacalculator/UI.class b/bin/simplejavacalculator/UI.class new file mode 100644 index 0000000..545fe04 Binary files /dev/null and b/bin/simplejavacalculator/UI.class differ diff --git a/bin/simplejavacalculatorTest/CalculatorTest.class b/bin/simplejavacalculatorTest/CalculatorTest.class new file mode 100644 index 0000000..6344801 Binary files /dev/null and b/bin/simplejavacalculatorTest/CalculatorTest.class differ diff --git a/build.xml b/build.xml index 69188f9..c947f0c 100644 --- a/build.xml +++ b/build.xml @@ -1,73 +1,68 @@ - - - - - - - - - - - Builds, tests, and runs the project SimpleJavaCalculator. - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/built-jar.properties b/build/built-jar.properties new file mode 100644 index 0000000..6282f6d --- /dev/null +++ b/build/built-jar.properties @@ -0,0 +1,4 @@ +#Tue, 05 Sep 2023 11:04:50 +0800 + + +C\:\\Users\\Clarr\\git\\Simple-Java-Calculator= diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ad21b5a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '3' +services: + jenkins: + container_name: jenkins + image: jenkins/jenkins:lts + ports: + - 8080:8080 + - 50000:50000 + volumes: + - jenkins_home:/var/jenkins_home + - /var/run/docker.sock:/var/run/docker.sock + environment: + - JAVA_OPTS=-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true + agent: + image: jenkins/ssh-agent:jdk11 + container_name: agent + expose: + - 22 + environment: + - JENKINS_AGENT_SSH_PUBKEY=ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDrI0OwiBlpK4FER5dMi2DwBs00FCdh1eGzvrNL1rIGhGyn+YUwXqCXEdTpCkA8lsjbbtUmDnRkPeCzTJpfOMcU+zBuQTUxS0txG1wotyGKWBEckd9giP4Z09JNKyftPdJEz0+YquN8KX1zS22l0hVZXHFQCSvbjaPu72D60752W9J0PdDSUfX7MdgJ/cDLD2IXguGcXVn/S3tLcdcoukFxsQ7zcaWkg3aF7rtFvuVQANb0B+h9KTxcQorJlDM4p4nkfiQEGxpijAh+p7Zi285kOt9OiJ3ButT4CnM5G6P2mhOxgLZX1InTGA07VTKGiGPp0Mrp2LzNKBDZdex4zNhDHmA3EiKGmKUHvHTcjA30md6VYqo7UB1S2BbCcmpDvcvtnplif8IyLfLq9qJb2vq71ItIho876MP3Kr7O4Ddh0zssuCMMtee2fnFpCtKRE4o2thlb/RM1vOKg0OvAo96Pl77HVuQm9RaTOe+PVk/63WLl5migG4THgbrXV+5HjCk= + +volumes: + jenkins_home: diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 93099f5..b6df91b 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -46,8 +46,8 @@ is divided into following sections: - - + + diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..1c12234 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,11 @@ +/** + * + */ +/** + * + */ +module SimpleJavaCalculator { + requires java.desktop; + requires junit; + requires org.junit.jupiter.api; +} \ No newline at end of file diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index f76e879..cf8b439 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -1,14 +1,3 @@ -/** - * @name Simple Java Calculator - * @package ph.calculator - * @file Main.java - * @author SORIA Pierre-Henry - * @email pierrehs@hotmail.com - * @link http://github.com/pH-7 - * @copyright Copyright Pierre-Henry SORIA, All Rights Reserved. - * @license Apache (http://www.apache.org/licenses/LICENSE-2.0) - */ - package simplejavacalculator; import static java.lang.Double.NaN; @@ -19,11 +8,11 @@ public class Calculator { public enum BiOperatorModes { - normal, add, minus, multiply, divide , xpowerofy + normal, add, minus, multiply, divide , xpowerofy, mod } public enum MonoOperatorModes { - square, squareRoot, oneDividedBy, cos, sin, tan, log, rate, abs, ln, + square,factorial, squareRoot, oneDividedBy, cos, sin, tan, log, rate, abs, ln, } private Double num1, num2; @@ -52,6 +41,9 @@ private Double calculateBiImpl() { if (mode == BiOperatorModes.xpowerofy) { return pow(num1,num2); } + if (mode == BiOperatorModes.mod) { + return num1 % num2; + } // never reach throw new Error(); @@ -85,6 +77,13 @@ public Double reset() { public Double calculateMono(MonoOperatorModes newMode, Double num) { + if (newMode == MonoOperatorModes.factorial) { + double facResult = 1; + for(int i=1;i<=num;i++){ + facResult=facResult*i; + } + return facResult; + } if (newMode == MonoOperatorModes.square) { return num * num; } @@ -127,4 +126,4 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { throw new Error(); } -} +} \ No newline at end of file diff --git a/src/simplejavacalculator/SimpleJavaCalculator.java b/src/simplejavacalculator/SimpleJavaCalculator.java index cd11787..59153f0 100644 --- a/src/simplejavacalculator/SimpleJavaCalculator.java +++ b/src/simplejavacalculator/SimpleJavaCalculator.java @@ -1,19 +1,3 @@ -/** - * @name Simple Java Calculator - * @package ph.calculator - * @file Main.java - * @author SORIA Pierre-Henry - * @email pierrehs@hotmail.com - * @link http://github.com/pH-7 - * @copyright Copyright Pierre-Henry SORIA, All Rights Reserved. - * @license Apache (http://www.apache.org/licenses/LICENSE-2.0) - * @create 2012-03-30 - * - * @modifiedby Achintha Gunasekara - * @modweb http://www.achinthagunasekara.com - * @modemail contact@achinthagunasekara.com - */ - package simplejavacalculator; public class SimpleJavaCalculator { diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 087bfb1..6bc3928 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -1,21 +1,3 @@ -/** - * @name Simple Java Calculator - * @package ph.calculator - * @file UI.java - * @author SORIA Pierre-Henry - * @email pierrehs@hotmail.com - * @link http://github.com/pH-7 - * @copyright Copyright Pierre-Henry SORIA, All Rights Reserved. - * @license Apache (http://www.apache.org/licenses/LICENSE-2.0) - * @create 2012-03-30 - * - * @modifiedby Achintha Gunasekara - * @modifiedby Kydon Chantzaridis - * @modweb http://www.achinthagunasekara.com - * @modemail contact@achinthagunasekara.com - * @modemail kchantza@csd.auth.gr - */ - package simplejavacalculator; import java.awt.FlowLayout; @@ -51,8 +33,9 @@ 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, butln; + butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy,butFactorial, + butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butln, butMod; + private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -96,7 +79,8 @@ public UI() throws IOException { butMultiply = new JButton("*"); butDivide = new JButton("/"); butEqual = new JButton("="); - butSquareRoot = new JButton("sqrt"); + butSquareRoot = new JButton("sqrt"); + butFactorial = new JButton("factorial!"); butSquare = new JButton("x*x"); butOneDividedBy = new JButton("1/x"); butCos = new JButton("Cos"); @@ -109,7 +93,7 @@ public UI() throws IOException { butabs = new JButton("abs(x)"); butCancel = new JButton("C"); butBinary = new JButton("Bin"); - + butMod = new JButton("%"); // Add the mod button calc = new Calculator(); } @@ -134,6 +118,7 @@ public void init() { butEqual.setFont(font); butSquareRoot.setFont(font); butSquare.setFont(font); + butFactorial.setFont(font); butOneDividedBy.setFont(font); butCos.setFont(font); butSin.setFont(font); @@ -145,7 +130,7 @@ public void init() { butabs.setFont(font); butCancel.setFont(font); butBinary.setFont(font); - + butMod.setFont(font); // Set font for the mod button panel.add(Box.createHorizontalStrut(100)); panelSub1.add(text); panel.add(panelSub1); @@ -163,7 +148,9 @@ public void init() { panelSub3.add(but[6]); panelSub3.add(Box.createHorizontalStrut(15)); panelSub3.add(butMultiply); - panelSub3.add(butDivide); + panelSub3.add(butDivide); + panelSub3.add(butMod); // Add the mod button here + panel.add(panelSub3); panelSub4.add(but[7]); @@ -181,6 +168,7 @@ public void init() { panel.add(panelSub5); panelSub6.add(butSquare); + panelSub6.add(butFactorial); panelSub6.add(butSquareRoot); panelSub6.add(butOneDividedBy); panelSub6.add(butxpowerofy); @@ -204,6 +192,7 @@ public void init() { butMinus.addActionListener(this); butMultiply.addActionListener(this); butDivide.addActionListener(this); + butFactorial.addActionListener(this); butSquare.addActionListener(this); butSquareRoot.addActionListener(this); butOneDividedBy.addActionListener(this); @@ -215,6 +204,8 @@ public void init() { butlog.addActionListener(this); butrate.addActionListener(this); butabs.addActionListener(this); + butMod.addActionListener(this); // Add action listener for the mod button + butBinary.addActionListener(this); butEqual.addActionListener(this); @@ -268,6 +259,10 @@ public void actionPerformed(ActionEvent e) { writer(calc.calculateBi(Calculator.BiOperatorModes.xpowerofy, reader())); } + if (source == butFactorial) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.factorial, reader())); + } + if (source == butSquare) { writer(calc.calculateMono(Calculator.MonoOperatorModes.square, reader())); } @@ -299,6 +294,11 @@ public void actionPerformed(ActionEvent e) { if (source == butabs) writer(calc.calculateMono(Calculator.MonoOperatorModes.abs, reader())); + if (source == butMod) { // Handle the mod button function + writer(calc.calculateBi(Calculator.BiOperatorModes.mod, reader())); + text.replaceSelection(butMod.getText()); + } + if (source == butEqual) writer(calc.calculateEqual(reader())); @@ -336,4 +336,4 @@ public void writer(final Double num) { text.setText(Double.toString(num)); } } -} +} \ No newline at end of file diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index 1ea20f7..4a857fc 100755 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -8,8 +8,15 @@ import simplejavacalculator.Calculator; -class CalculatorTest { +class CalculatorTest{ + @Test + void calculateBiModTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.mod, 7.0); + Assertions.assertEquals(3.0, calculator.calculateBi(Calculator.BiOperatorModes.mod, 4.0)); + } + @Test void calculateBiNormalTest() { Calculator calculator = new Calculator(); @@ -44,7 +51,7 @@ void calculateBiDivideTest() { calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); } - + @Test void calculateEqualTest() { Calculator calculator = new Calculator(); @@ -79,24 +86,46 @@ void CalculateMonoOneDividedByTest() { Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0)); } + //Math.cos(π/3)≈ 0.5 @Test - void CalculateMonoSinTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, 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); - } - - @Test - void CalculateMonoTanTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); - } - + void calculateMonoCosTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.cos, 60.0); + Assertions.assertEquals(Math.cos(Math.toRadians(60.0)), result); + } + + //Math.sin(π/6) ≈ 0.5 + @Test + void calculateMonoSinTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.sin, 30.0); + Assertions.assertEquals(Math.sin(Math.toRadians(30.0)), result); + } + + //Math.tan(π/4) = 1.0 + @Test + void calculateMonoTanTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.tan, 45.0); + Assertions.assertEquals(Math.tan(Math.toRadians(45.0)), result); + } + + //0.0 + @Test + void calculateMonoTanZeroTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.tan, 0.0); + Assertions.assertEquals(0.0, result); + } + + //undefined + @Test + void calculateMonoTanNaNTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.tan, 90.0); + Assertions.assertTrue(Double.isNaN(result)); + } + @Test void CalculateMonoLogTest() { Calculator calculator = new Calculator(); @@ -115,5 +144,12 @@ 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 calculateMonoFactorialTest() { + Calculator calculator = new Calculator(); + double result = calculator.calculateMono(Calculator.MonoOperatorModes.factorial, 5.0); + Assertions.assertEquals(120.0, result); + } + -} +} \ No newline at end of file