Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

points) The description below explains the Strategy Pattern: In Strategy pattern

ID: 3919616 • Letter: P

Question

points) The description below explains the Strategy Pattern: In Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern falls under the set of behavior patterns Its Intent is Define a family of algorithms, encapsulate each one, and make them interchange- able. Strategy lets the algorithm vary independently from the clients that use it Capture the abstraction in an interface, bury implementation details in derived classes You have the following operations . add: a b subtract: a-b divide: a / b, assume a and b are NEVER 0 multiply a b; . power: ab a raise to the power of b. Ex. 24 16 We will make the following assumptions 1 ALL operands are ints 2 ALL return values are ints 3 C 4 Classes do not need any Constructors lasses do not need any data-fields Implement a set of classes and an interface that adheres to the Strategy Pattern

Explanation / Answer

Operation.java Interface:

/**
* Interface for All Math operations
*
*/
public interface Operation {
public int opearate(int a, int b);
}

-------------------------------------------------------------------

AddOperation.java strategy for add:-

//perform addition of two numbers

public class AddOperation implements Operation{

@Override

public int opearate(int a, int b) {

return a + b;

}

}

------------------------------------------------------------------------------

SubtractOperation.java strategy for subtraction:

//Subtraction operation

public class SubtractOperation implements Operation {

@Override

public int opearate(int a, int b) {

return a - b;

}

}

----------------------------------------------------------------------------

MultiplyOperation.java Strategy for multiply :

//perform multiply operation

public class MultiplicationOperation implements Operation{

@Override

public int opearate(int a, int b) {

return a*b;

}

}

--------------------------------------------------------

DivideOperation.java for division:-

//Divide operation

public class DivideOperation implements Operation{

@Override

public int opearate(int a, int b) {

if(b != 0)

return a/b;

return 0;

}

}

------------------------------------------------------------------------

PowerOperation.java forpower strategy

// power operation

public class PowerOperation implements Operation{

@Override

public int opearate(int a, int b) {

return (int) Math.pow(a, b);

}

}

---------------------------------------------------------

MathContext.java

public class MathContext {

private Operation operation;

public MathContext(Operation operation) {

this.operation = operation;

}

public int performOperation(int a, int b) {

return operation.opearate(a, b);

}

}

-------------------------------------------

Test.java for testing:-

public class Test {

public static void main(String[] args) {

MathContext context = new MathContext(new AddOperation());

System.out.println("12 + 4 = " + context.performOperation(12, 4));

context = new MathContext(new SubtractOperation());

System.out.println("12 - 4 = " + context.performOperation(12, 4));

context = new MathContext(new MultiplicationOperation());

System.out.println("12 * 4 = " + context.performOperation(12, 4));

context = new MathContext(new DivideOperation());

System.out.println("12 / 4 = " + context.performOperation(12, 4));

context = new MathContext(new PowerOperation());

System.out.println("12 ^ 4 = " + context.performOperation(12, 4));

}

}