
Implement Generic to perform arithmetic evaluation using command line argument by using Java

Hi Friend,
Try this:
import java.util.*;
interface MathClass<T extends Number> {
T add(T a, T b);
}
class GenericExample implements MathClass<Integer> {
public Integer add(Integer a, Integer b) {
return a +b;
}
public static void main(String[]args){
GenericExample g=new GenericExample();
Scanner input=new Scanner(System.in);
System.out.print("Enter number1: ");
int num1=input.nextInt();
System.out.print("Enter number2: ");
int num2=input.nextInt();
System.out.println("Result is: "+(g.add(num1,num2)));
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.