views:

99

answers:

5

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));
    }


}
A: 

The values of the operands are passed as parameters to your method by the caller. The operator seems to be implicit in the specific implementation of ArithmeticOperator. It is up to the subclasses of ArithmeticOperator to define the body of operate, which is where the operator will "live".

Note that it doesn't need to be "stored" anywhere per se; it is just logic.

For example:

public class PlusOperator implements ArithmeticOperator {
    double operate(double a, double b) {
        return a+b;
    }
}

In your printName method, you will need to check against the actual type of the ArithmeticOperator, rather than a String (which you don't have, and don't need).

Like:

if (op instanceof PlusOperator) {
    System.out.println("+");
}
danben
A: 

Operator seems to be stateless being, just doing some computation and returning result. So the answer is not, do not store it in object state (or variable). What would you need it for?

Konrad Garus
+2  A: 

You use interfaces to define a contract. The variable is handled by the concrete implementation, e.g.

public class Add implements ArithmeticOperator(){ 
   @Override double operate (double a, double b){ return a+b;}
}

If you want something wit a bit more behavior as a base you can use an abstract class.

Steve B.
A: 

Decide whether your variable mutable or not. If yes - then declare it in class, if immutable - in interface. Interface variables final by default.

Artic
A: 

An interface is not an abstract class.

Interfaces are needed to define a common protocol between classes, where common protocol means just method signatures that are visible from other classes, so what they are capable to do. Interfaces can't "hold" values.

Abstract classes are more powerful, they are used to define a common part between classes, either procol or implementation. You can mix predefined behaviour with abstract methods and let subclasses implement them.

What does it mean?

It means that you use an interface when you want to give your classes same methods, but if you want to define common behaviour you should use an abstract class or a father class from which declare specific implementations.

In your example you can let the class type be your actual operation type:

interface ArithmeticOperation
{
  public double operate(double a, double b);
}

class AddOperation implements ArithmeticOperation
{
  public double operate(double a, double b)
  {
    return a + b;
  }
}

You can also use subclasses for specific things (mind that the following example is trivial and not so well suited):

class PowerOperation implements UnaryOperation
{
  public double operate(double a)
  {
    return a*a;
  }
}

class CubeOperation extends PowerOperation
{
  public double operate(double a)
  {
    return super.operate(a)*a;
  }
}

You should try to avoid chain of IFs just to print out things, this because your classes already know what type they are so you can delegate to them this work:

class AddOperation{ public String printVal() { return "ADD"; } }
class SubOperation{ public String printVal() { return "SUB"; } }
Jack