Hello! I was writing a class Operation which takes in 2 Operands (which have an eval method they override from an Operable interface) and a char operator and results in the corresponding output. However, there is an added demand that if the '+' operator is used, we must only ensure they are strings, or else we throw an exception -- or similarly if the * operator is used, we must ensure they are integers or throw an exception. I attempted to write my overriden eval function in Operation with the same generic return type as the one we declare at Operation. However, even upon casting, it says my wrapper Integer class cannot be converted to the return type T. How may I fix this?
Code: ```java
class Operation<T> implements Operable<T> {
private char s;
private Operand<T> o1;
private Operand<T> o2;
private Operation(char s, Operand<T> o1, Operand<T> o2) {
this.s = s;
this.o1 = o1;
this.o2 = o2;
}
public static <T> Operation<T> of(char s, Operand<T> o1, Operand<T> o2) {
return new Operation(s, o1, o2);
}
@Override
public T eval() {
if (this.s == '*') {
if (o1.eval() instanceof Integer && o2.eval() instanceof Integer) {
int a = (Integer) o1.eval();
int b = (Integer)o2.eval();
return a*b;
}
throw new InvalidOperandException(s);
}
else if (this.s == '+') {
if (o1.eval() instanceof String && o2.eval() instanceof String) {
return (String)o1.eval() + (String)o2.eval();
}
throw new InvalidOperandException(s);
}
else if (this.s == '^') {
if (o1.eval() instanceof Boolean && o2.eval() instanceof Boolean) {
return o1.eval() ^ o2.eval();
}
throw new InvalidOperandException(s);
}
else {
return null;
}
}
}