can you tell me how this line works.... my OperatorFactory.get("add") is not doing anything. I'm not getting anything printed
ArithmeticOperator add = OperatorFactory.get ("add");
when I have the following:
interface ArithmeticOperator {
// Returns the result of applying the operator to operands a and b.
double operate (double a, double b);
// Return a String that is the name of this operator.
String printName ();
}
public class OperatorFactory implements ArithmeticOperator {
public OperatorFactory(){
}
public static ArithmeticOperator get(String name){
if(name.equals("add"))
return new PlusOperator();
else if(name.equals("sub"))
return new SubOperator();
else if(name.equals("mult"))
return new MultOperator();
else if(name.equals("div"))
return new DivOperator();
else
return null;
}
public double operate(double a, double b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String printName() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
public class PlusOperator extends OperatorFactory {
public double operate(double a, double b) {
return a + b;
}
public String printName() {
return "Add";
}
}
public class PlusOperator extends OperatorFactory {
public double operate(double a, double b) {
return a + b;
}
public String printName() {
return "Add";
}
}