Hi I have the following interfaces that I need to create. Can you just answer one question. Am I supposed to create a variable to hold the value of the operator and the values of the two operands? If so, should these be created inside the interfaces or inside the class with my main method?
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 ();
}
interface OperatorIterator {
// Apply the operator op repeatedly to the startValue, for numIterations
// times, e.g., for numIterations=4 you would return
// op (startValue, op (startValue, op (startValue, startValue)))
double iterate (ArithmeticOperator op, double startValue, int numIterations);
}
public class Exam1 implements ArithmeticOperator, OperatorIterator{
public double operate(double a, double b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String printName() {
String operator ="";
if(operator.equals("+"))
return "Add";
else if(operator.equals("-"))
return "Sub";
else if(operator.equals("/"))
return "Div";
else if(operator.equals("*"))
return "Mult";
else
return "Unknown Operator";
}
public double iterate(ArithmeticOperator op, double startValue, int numIterations) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static void main (String[] argv)
{
// Test 1:
System.out.println ("TEST 1:");
ArithmeticOperator add = OperatorFactory.get ("add");
System.out.println (" 1 " + add.printName() + " 2 = " + add.operate (1,2));
ArithmeticOperator sub = OperatorFactory.get ("sub");
System.out.println (" 3 " + sub.printName() + " 2 = " + sub.operate (3,2));
ArithmeticOperator mult = OperatorFactory.get ("mult");
System.out.println (" 3 " + mult.printName() + " 2 = " + mult.operate (3,2));
ArithmeticOperator div = OperatorFactory.get ("div");
System.out.println (" 3 " + div.printName() + " 2 = " + div.operate (3,2));
// Test 2:
System.out.println ("TEST 2:");
ArithmeticOperator add2 = OperatorFactory.get ("add");
ArithmeticOperator sub2 = OperatorFactory.get ("sub");
System.out.println (" Does add2==add? " + add2.equals(add));
System.out.println (" Does add2==sub2? " + add2.equals(sub2));
// Test 3:
System.out.println ("TEST 3:");
OperatorIterator opIter = new OpIterator ();
System.out.println (" 3 * 8 = " + opIter.iterate(add, 3, 8));
System.out.println (" 3 ^ 4 = " + opIter.iterate(mult, 3, 4));
}
}