diff --git a/src/pkg2/Calculator.java b/src/pkg2/Calculator.java new file mode 100644 index 0000000..fbae497 --- /dev/null +++ b/src/pkg2/Calculator.java @@ -0,0 +1,80 @@ +package pkg2; + +import java.util.Scanner; + +public class Calculator { + int add(int a, int b) { + return a + b; + } + + int sub(int a, int b) { + return a - b; + } + + int mul(int a, int b) { + return a * b; + } + + int div(int a, int b) { + return a / b; + } + + int mod(int a, int b) { + return a % b; + } + + public void calculator() { + Scanner sc = new Scanner(System.in); + Calculator calc = new Calculator(); + int a, b; + int choice = 0; + while (choice >= 0) { + System.out.println("Enter your choice: "); + System.out.println("1. Addition"); + System.out.println("2. Subtraction"); + System.out.println("3. Multiplication"); + System.out.println("4. Division"); + System.out.println("5. Modulus"); + System.out.println("0. Exit"); + choice = sc.nextInt(); + switch (choice) { + case 1: + System.out.println("Enter two numbers: "); + a = sc.nextInt(); + b = sc.nextInt(); + System.out.println("Addition: " + calc.add(a, b)); + break; + case 2: + System.out.println("Enter two numbers: "); + a = sc.nextInt(); + b = sc.nextInt(); + System.out.println("Subtraction: " + calc.sub(a, b)); + break; + case 3: + System.out.println("Enter two numbers: "); + a = sc.nextInt(); + b = sc.nextInt(); + System.out.println("Multiplication: " + calc.mul(a, b)); + break; + case 4: + System.out.println("Enter two numbers: "); + a = sc.nextInt(); + b = sc.nextInt(); + System.out.println("Division: " + calc.div(a, b)); + break; + case 5: + System.out.println("Enter two numbers: "); + a = sc.nextInt(); + b = sc.nextInt(); + System.out.println("Modulus: " + calc.mod(a, b)); + break; + case 0: + System.out.println("Exiting..."); + choice = -1; + default: + System.out.println("Invalid choice!"); + } + } + sc.close(); + } +} diff --git a/src/pkg2/Main.java b/src/pkg2/Main.java new file mode 100644 index 0000000..50f4256 --- /dev/null +++ b/src/pkg2/Main.java @@ -0,0 +1,10 @@ +package pkg2; + +import pkg2.Calculator; + +public class Main{ + public static void main(String args[]){ + Calculator c = new Calculator(); + c.calculator(); + } +} \ No newline at end of file