) The description below explains the Strategy Pattern: In Strategy pattern, a cl
ID: 3919932 • Letter: #
Question
) 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,assumeaandbareNEVER0. • multiply : a ? b; • power: ab ,araisetothepowerofb. Ex. 24 =16. We will make the following assumptions: 1 ALL operands are ints. 2 ALL return values are ints. 3 Classes do not need any data-fields 4 Classes do not need any Constructors Implement a set of classes and an interface that adheres to the Strategy Pattern. ) 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,assumeaandbareNEVER0. • multiply : a ? b; • power: ab ,araisetothepowerofb. Ex. 24 =16. We will make the following assumptions: 1 ALL operands are ints. 2 ALL return values are ints. 3 Classes do not need any data-fields 4 Classes do not need any Constructors Implement a set of classes and an interface that adheres to 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,assumeaandbareNEVER0. • multiply : a ? b; • power: ab ,araisetothepowerofb. Ex. 24 =16. We will make the following assumptions: 1 ALL operands are ints. 2 ALL return values are ints. 3 Classes do not need any data-fields 4 Classes do not need any Constructors Implement a set of classes and an interface that adheres to the Strategy Pattern.Explanation / Answer
ScreenShot
---------------------------------------------------------------------
Program
//An algorithm implementing interface
public interface strategy {
public void Operation(int a,int b);
}
//Addition class
class add implements strategy{
//implement the interface method
@Override
public void Operation(int a, int b) {
System.out.println("a+b="+a+"+"+b+"="+(a+b));
}
}
//Subtraction class
class subtract implements strategy{
//implement the interface method
@Override
public void Operation(int a, int b) {
System.out.println("a-b="+a+"-"+b+"="+(a-b));
}
}
//Division class
class divide implements strategy{
//implement the interface method
@Override
public void Operation(int a, int b) {
System.out.println("a/b="+a+"/"+b+"="+(a/b));
}
}
//Multiplication class
class multiply implements strategy{
//implement the interface method
@Override
public void Operation(int a, int b) {
System.out.println("a*b="+a+"*"+b+"="+(a*b));
}
}
//Power implementation class
class power implements strategy{
//implement the interface method
@Override
public void Operation(int a, int b) {
System.out.println("a^b="+a+"^"+b+"="+Math.pow(a,b));
}
}
//Context class for algorithm details
class context{
//member variables
int a,b;
//Interface object
strategy s;
//Object set
public void setStrategy(strategy st) {
this.s= st;
}
//Call according to algorithm
public void setOp(){
s.Operation(a,b);
}
//accessor
public int getA() {
return a;
}
public int getB() {
return b;
}
//Mutator
public void setA(int val1) {
this.a = val1;
}
public void setB(int val1) {
this.b = val1;
}
}
//Test class
public class StrategyPlan {
public static void main(String[] args) {
//algorithm test for add
strategy st=new add();
context c=new context();
c.setStrategy(st);
c.setA(3);
c.setB(5);
c.setOp();
//algorithm test for subtract
strategy st1=new subtract();
c.setStrategy(st1);
c.setA(8);
c.setB(5);
c.setOp();
//algorithm test multiplication
strategy st2=new multiply();
c.setStrategy(st2);
c.setA(8);
c.setB(5);
c.setOp();
//algorithm test division
strategy st3=new divide();
c.setStrategy(st3);
c.setA(8);
c.setB(5);
c.setOp();
//algorithm test power
strategy st4=new power();
c.setStrategy(st4);
c.setA(2);
c.setB(3);
c.setOp();
}
}
----------------------------------------------------------
Output
a+b=3+5=8
a-b=8-5=3
a*b=8*5=40
a/b=8/5=1
a^b=2^3=8.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.