-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1.java
More file actions
47 lines (36 loc) · 1022 Bytes
/
Copy pathProblem_1.java
File metadata and controls
47 lines (36 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.screening;
import java.util.Scanner;
class Calculator{
public double calculate(double a, double b, String operation) {
switch(operation.toLowerCase()) {
case "add":
return a + b;
case "substract":
return a - b;
case "multiply":
return a * b;
case "divide":
if(b==0) {
throw new ArithmeticException("Cannot divided by Zero");
}
return a / b;
default:
throw new IllegalArgumentException("Invalid operation type...");
}
}
}
public class Problem_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a: ");
double a = sc.nextDouble();
System.out.println("Enter b: ");
double b = sc.nextDouble();
System.out.println("Enter operation (add/substract/multiply/divide): ");
String op = sc.next();
Calculator cal = new Calculator();
double result = cal.calculate(a, b, op);
System.out.println("Result: " + result);
sc.close();
}
}