From fec3f27ecbfaed4159f5d253211423e59e84dcee Mon Sep 17 00:00:00 2001 From: BudBak Date: Sun, 17 Nov 2019 21:43:26 +0530 Subject: [PATCH] BAEL-3506 --- .../baeldung/maths/BasicCalculatorIfElse.java | 52 ++++++++++++++++ .../maths/BasicCalculatorSwitchCase.java | 62 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorIfElse.java create mode 100644 java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorSwitchCase.java diff --git a/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorIfElse.java b/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorIfElse.java new file mode 100644 index 0000000000..37cac75539 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorIfElse.java @@ -0,0 +1,52 @@ +package com.baeldung.maths; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BasicCalculatorIfElse { + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + 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"); + 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()); + } + } +} \ No newline at end of file diff --git a/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorSwitchCase.java b/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorSwitchCase.java new file mode 100644 index 0000000000..1f30f1b8c2 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/maths/BasicCalculatorSwitchCase.java @@ -0,0 +1,62 @@ +package com.baeldung.maths; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BasicCalculatorSwitchCase { + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + 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"); + 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()); + } + scanner.close(); + } +} +