-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDynamicWay.java
More file actions
48 lines (44 loc) · 1.42 KB
/
DynamicWay.java
File metadata and controls
48 lines (44 loc) · 1.42 KB
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
48
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;
public class DynamicWay {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
int a = 100;
int b = 200;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the ClassName");
String className = scanner.next();
Object obj =Class.forName(className).newInstance(); // It load the Class
//Calculator calc = new Calculator();
System.out.println("add");
System.out.println("subtract");
System.out.println("multiply");
System.out.println("divide");
System.out.println("Type Operation Name");
String methodName = scanner.next();
Class cls = obj.getClass();
Method method = cls.
getDeclaredMethod(methodName, int.class,int.class);
/*Method method = obj.getClass()
.getDeclaredMethod
(methodName, int.class,int.class) ;*/
Integer result =(Integer)method.invoke(obj, a,b);
System.out.println("Result is "+result);
/*switch(operation){
case "add":
result= calc.add(a, b);
break;
case "subtract":
result=calc.subtract(a, b);
break;
case "multiply":
result=calc.multiply(a, b);
break;
case "divide":
result=calc.divide(a, b);
break;
}
System.out.println("Result is "+result);
*/
}
}