diff --git a/java-math-2/README.md b/java-math-2/README.md new file mode 100644 index 0000000000..ca809e8623 --- /dev/null +++ b/java-math-2/README.md @@ -0,0 +1,8 @@ +## Java Math + +This module contains articles about math in Java. + +### Relevant articles: + +- [Basic Calculator in Java](https://www.baeldung.com/basic-calculator-in-java) +- More articles: [[<-- prev]](/../java-math) diff --git a/java-math-2/pom.xml b/java-math-2/pom.xml new file mode 100644 index 0000000000..c9d083101b --- /dev/null +++ b/java-math-2/pom.xml @@ -0,0 +1,105 @@ + + 4.0.0 + java-math-2 + 0.0.1-SNAPSHOT + java-math-2 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.ejml + ejml-all + ${ejml.version} + + + org.nd4j + nd4j-native + ${nd4j.version} + + + org.la4j + la4j + ${la4j.version} + + + colt + colt + ${colt.version} + + + com.google.guava + guava + ${guava.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + org.assertj + assertj-core + ${org.assertj.core.version} + test + + + com.github.dpaukov + combinatoricslib3 + ${combinatoricslib3.version} + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + + + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + + + + + 3.6.1 + 3.9.0 + 1.11 + 27.0.1-jre + 3.3.0 + 0.38 + 1.0.0-beta4 + 1.2.0 + 0.6.0 + 1.19 + + + \ No newline at end of file diff --git a/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorIfElse.java b/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorIfElse.java new file mode 100644 index 0000000000..cad7bf0f13 --- /dev/null +++ b/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorIfElse.java @@ -0,0 +1,55 @@ +package com.baeldung.maths.calculator.basic; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BasicCalculatorIfElse { + + public static void main(String[] args) { + + System.out.println("---------------------------------- \n" + + "Welcome to Basic Calculator \n" + + "----------------------------------"); + System.out.println("Following operations are supported : \n" + + "1. Addition (+) \n" + + "2. Subtraction (-) \n" + + "3. Multiplication (* OR x) \n" + + "4. Division (/) \n"); + + Scanner scanner = new Scanner(System.in); + try { + System.out.println("Enter an operator: (+ OR - OR * OR /) "); + char operation = scanner.next().charAt(0); + + if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) { + System.err.println("Invalid Operator. Please use only + or - or * or /"); + } + + System.out.println("Enter First Number: "); + double num1 = scanner.nextDouble(); + + System.out.println("Enter Second Number: "); + double num2 = scanner.nextDouble(); + + if (operation == '/' && num2 == 0.0) { + System.err.println("Second Number cannot be zero for Division operation."); + } + + if (operation == '+') { + System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); + } else if (operation == '-') { + System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); + } else if (operation == '*' || operation == 'x') { + System.out.println(num1 + " x " + num2 + " = " + (num1 * num2)); + } else if (operation == '/') { + System.out.println(num1 + " / " + num2 + " = " + (num1 / num2)); + } else { + System.err.println("Invalid Operator Specified."); + } + } catch (InputMismatchException exc) { + System.err.println(exc.getMessage()); + } finally { + scanner.close(); + } + } +} \ No newline at end of file diff --git a/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorSwitchCase.java b/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorSwitchCase.java new file mode 100644 index 0000000000..f87437a967 --- /dev/null +++ b/java-math-2/src/main/java/com/baeldung/maths/calculator/basic/BasicCalculatorSwitchCase.java @@ -0,0 +1,64 @@ +package com.baeldung.maths.calculator.basic; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BasicCalculatorSwitchCase { + public static void main(String[] args) { + + System.out.println("---------------------------------- \n" + + "Welcome to Basic Calculator \n" + + "----------------------------------"); + System.out.println("Following operations are supported : \n" + + "1. Addition (+) \n" + + "2. Subtraction (-) \n" + + "3. Multiplication (* OR x) \n" + + "4. Division (/) \n"); + + Scanner scanner = new Scanner(System.in); + try { + System.out.println("Enter an operator: (+ OR - OR * OR /) "); + char operation = scanner.next().charAt(0); + + if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) { + System.err.println("Invalid Operator. Please use only + or - or * or /"); + } + + System.out.println("Enter First Number: "); + double num1 = scanner.nextDouble(); + + System.out.println("Enter Second Number: "); + double num2 = scanner.nextDouble(); + + if (operation == '/' && num2 == 0.0) { + System.err.println("Second Number cannot be zero for Division operation."); + } + + switch (operation) { + case '+': + System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); + break; + case '-': + System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); + break; + case '*': + System.out.println(num1 + " x " + num2 + " = " + (num1 * num2)); + break; + case 'x': + System.out.println(num1 + " x " + num2 + " = " + (num1 * num2)); + break; + case '/': + System.out.println(num1 + " / " + num2 + " = " + (num1 / num2)); + break; + default: + System.err.println("Invalid Operator Specified."); + break; + } + } catch (InputMismatchException exc) { + System.err.println(exc.getMessage()); + } finally { + scanner.close(); + } + } +} + diff --git a/java-math-2/src/main/resources/logback.xml b/java-math-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/java-math-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8bcb6ce1d6..10a1ce2aeb 100644 --- a/pom.xml +++ b/pom.xml @@ -508,6 +508,7 @@ java-lite java-math + java-math-2 java-numbers java-numbers-2 java-rmi @@ -1274,6 +1275,7 @@ java-ee-8-security-api java-lite java-math + java-math-2 java-numbers java-numbers-2 java-rmi