I am trying to implement an interface so that the main program would work.
Any light? I am having a difficult time trying to understand interface :/
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.
public void printName (String a);//{
//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 {
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));
}
}