Skip to content

new funcs, error messages and tests #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test1"/>
<classpathentry kind="src" path="tests"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/ant.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Simple-Java-Calculator(2)</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions Simple-Java-Calculator(2).eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inheritJdk="true">
<contentEntry url="file://$MODULE_DIR$">
<testFolder url="file://$MODULE_DIR$/tests"/>
<testFolder url="file://$MODULE_DIR$/test1"/>
</contentEntry>
</component>
29 changes: 29 additions & 0 deletions Simple-Java-Calculator(2).iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test1" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
46 changes: 39 additions & 7 deletions src/simplejavacalculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
package simplejavacalculator;

import static java.lang.Double.NaN;
import static java.lang.Math.log;
import static java.lang.Math.log10;
import static java.lang.Math.pow;
import static java.lang.Math.*;

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, squareRoot, oneDividedBy, cos, sin, tan, log, rate, abs, ln, fact, exp
}

private Double num1, num2;
Expand Down Expand Up @@ -52,6 +50,13 @@ private Double calculateBiImpl() {
if (mode == BiOperatorModes.xpowerofy) {
return pow(num1,num2);
}
if (mode==BiOperatorModes.mod){
if(floor(num1) == num1 && floor(num2)==num2) {
return num1%num2;
} else {
return NaN;
}
}

// never reach
throw new Error();
Expand All @@ -75,6 +80,20 @@ public Double calculateEqual(Double num) {
return calculateBi(BiOperatorModes.normal, num);
}

public Double calculateFact(Double num){
int i;
double fact=1;
if (num==1){
return 1.0;
}
else{
for (i=1; i<=floor(num); i++){
fact=fact*i;
}
return fact;
}
}

public Double reset() {
num2 = 0.0;
num1 = 0.0;
Expand All @@ -83,7 +102,7 @@ public Double reset() {
return NaN;
}


public Double calculateMono(MonoOperatorModes newMode, Double num) {
if (newMode == MonoOperatorModes.square) {
return num * num;
Expand Down Expand Up @@ -117,12 +136,25 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
return log(num);
}
if (newMode == MonoOperatorModes.rate) {
return num / 100;
return num / 100;
}
if (newMode == MonoOperatorModes.abs){
return Math.abs(num);
}

if (newMode == MonoOperatorModes.fact) {
if (num==floor(num) && num>0) {
return calculateFact(num);
}
else {
return NaN;
}
}

if (newMode == MonoOperatorModes.exp){
return Math.exp(num);
}

// never reach
throw new Error();
}
Expand Down
Loading