Skip to content
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

Updated class ScientificCalculator #21

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
63 changes: 62 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
http://maven.apache.org/POM/4.0.0 ">
<modelVersion>4.0.0</modelVersion>

<groupId>com.zipcodewilmington</groupId>
Expand All @@ -11,16 +12,65 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<kotlin.version>1.9.22</kotlin.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down Expand Up @@ -48,6 +98,17 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<artifactId>kotlin-test</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.zipcodewilmington.scientificcalculator;

public class CoreCalculations {

public double addition(double displayNum, double num){
return displayNum + num;
}


public double subtraction(double displayNum, double num) {
return displayNum - num;
}

public double multiplication(double displayNum, double num) {
return displayNum*num;
}

public double division(double displayNum, double error) {
if (error != 0) {
displayNum /= error;

} else {
System.out.println("Err: Cannot divide a number by 0.\n");
displayNum = Double.NaN;
}
return displayNum;
}

public double square(double displayNum) {

return displayNum*displayNum;
}
public double squareRoot(double displayNum) {

if (displayNum >= 0) {
displayNum = Math.sqrt(displayNum);
} else {
System.out.println("Err: user cannot take the square root of a negative number.");
displayNum = Double.NaN;
}
return displayNum;
}
public double exponent(double displayNum, double exponent) {
return Math.pow(displayNum,exponent);
}
public double inverse(double displayNum) {
if (displayNum != 0) {
displayNum = 1 / displayNum;
} else {
System.out.println("Err: user cannot take the inverse of 0.");
displayNum = Double.NaN;
}
return displayNum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.zipcodewilmington.scientificcalculator;

public class LogarithmicFunctions {
// calculate the logarithmic function of a given value then display it
public double calculateLogarithmic( double value ) {
double k = Math.log(value);
return Math.round(k* 100.0) /100.0;
}

// calculate the inverse logarithm of a given value then display it
public double calculateInverseLogarithm( double value ) {
double k = Math.exp(value);
return Math.round(k* 100.0) /100.0;
}

// calculate the natural logarithm of a given value then display it
public double calculateNaturalLog( double value, double base ) {
double k = Math.log((value) /(base));
return Math.round(k* 100.0) /100.0;
}

// calculate the inverse natural logarithm of a given value then display it
// public double calculateNaturalInverseLog( double value ) {
// return Math.exp(value);
// }

public double factorial(double value){
//5 ->1*2*3*4*5
int res = 1;
for (int i = 2; i <= value; i++) {
res = res * i;
}
return res;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.zipcodewilmington.scientificcalculator;

import java.util.Scanner;
public class ScientificCalculator {

public static double displayNum = 0;
public static void main(String[] args) {

String calculation;
Scanner scanner = new Scanner(System.in);
CoreCalculations cal = new CoreCalculations();
Trigonometry trig = new Trigonometry();
LogarithmicFunctions log = new LogarithmicFunctions();
SwitchDisplay sd = new SwitchDisplay();
System.out.println("----------------------------");
System.out.println("Welcome to my calculator!\n");
System.out.println("----------------------------");

while (true) {
System.out.printf("List of Operations:\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Square\n 6. Square Root\n 7. Exponent\n 8. Inverse\n 9. Switch\n " +
"10. sin\n 11. cos\n 12. tan\n 13. inverseSin\n 14. inverseCos\n 15. inverseTan\n " +
"16. log\n 17. log-1\n 18. ln\n 19. factorial\n 20. switchBinary\n 21. switchDecimal\n 22. switchOctal \n 23. HexaDecimal\n 24.clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum);
calculation = scanner.next();

if (calculation.equalsIgnoreCase("exit")) {
break;
}

switch (calculation) {
case "1":
System.out.println("Enter Number: ");
displayNum = cal.addition(displayNum,scanner.nextDouble());
break;

case "2":
System.out.println("Enter Number: ");
displayNum = cal.subtraction(displayNum, scanner.nextDouble());
break;

case "3":
System.out.println("Enter Number: ");
displayNum = cal.multiplication(displayNum,scanner.nextDouble() );
break;

case "4":
System.out.println("Enter Number: ");
displayNum = cal.division(displayNum, scanner.nextDouble());
break;

case "5":
System.out.println("Enter Number: ");
displayNum = cal.square(displayNum);
break;

case "6":
System.out.println("Enter Number: ");
displayNum = cal.squareRoot(displayNum);
break;

case "7":
System.out.println("Enter Exponent: ");
double exponent = scanner.nextDouble();
displayNum = cal.exponent(displayNum, exponent);
break;

case "8":
System.out.println("Enter Number: ");
displayNum = cal.inverse(displayNum);
break;

case "9":
System.out.println("Enter Number: ");
displayNum = -displayNum;
break;

case "10" :
System.out.println("Enter Number: ");
displayNum = trig.calculateSine(displayNum);
break;
case "11" :
System.out.println("Enter Number: ");
displayNum = trig.calculateCosine(displayNum);
break;
case "12":
System.out.println("Enter Number: ");
displayNum = trig.calculateTangent(displayNum);
break;
case "13":
System.out.println("Enter Number: ");
displayNum = trig.calculateInverseSine(displayNum);
break;
case "14":
System.out.println("Enter Number: ");
displayNum = trig.calculateInverseCosine(displayNum);
break;
case "15":
System.out.println("Enter Number: ");
displayNum = trig.calculateInverseTangent(displayNum);
break;
case "16":
System.out.println("Enter Number: ");
displayNum = log.calculateLogarithmic(displayNum);
break;
case "17":
System.out.println("Enter Number: ");
displayNum = log.calculateInverseLogarithm(displayNum);
break;
case "18":
System.out.println("Enter value: ");
double base = scanner.nextDouble();
System.out.println("Enter base: ");
displayNum = log.calculateNaturalLog(displayNum, base);
break;
case "19":
displayNum = log.factorial(displayNum);
break;
case "20":
displayNum = Double.parseDouble(sd.switchBinary(displayNum));
break;
case "21":
displayNum = Double.parseDouble(sd.switchDecimal(displayNum));
break;
case "22":
displayNum = Double.parseDouble(sd.switchOctal(displayNum));
break;
case "23":
displayNum = Double.parseDouble(sd.switchHexadecimal(displayNum));
break;
case "24":
displayNum = 0;
break;
default :
System.out.println("Invalid Selection.\n\n");

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.zipcodewilmington.scientificcalculator;

public class SwitchDisplay {
public String switchBinary(double input) {
long bits = Double.doubleToLongBits(input);
return Long.toBinaryString(bits);

}

// switch double input to octal
public String switchOctal(double input) {
long bits = Double.doubleToLongBits(input);
return Long.toOctalString(bits);
}

// switch double input to decimal
public String switchDecimal(double input) {
return Double.toString(input);
}

// switch double input to hexadecimal
public String switchHexadecimal(double input) {
long bits = Double.doubleToLongBits(input);
return Long.toHexString(bits);
}
}


Loading