In: Computer Science
Consider the following code:
public class Example { public static void doOp(Op op) { boolean result = op.operation(true, false); System.out.println(result); } public static void main(String[] args) { doOp(new AndOperation()); doOp(new OrOperation()); } }
main's output:
false true
Define any interfaces and/or classes necessary to make this output happen. Multiple answers are possible. You may not modify any of the code in Example.
interface Op{//interface Op with method declaration
public boolean operation(boolean v1,boolean v2);
}
class AndOperation implements Op{//class definition that implements
interface
public boolean operation(boolean v1,boolean v2){//and
operation
return v1&&v2;
}
}
class OrOperation implements Op{//class definition that implements
interface
public boolean operation(boolean v1,boolean v2){//oroperation
return v1||v2;
}
}
public class Example {//starter code provided
public static void doOp(Op op) {
boolean result = op.operation(true, false);
System.out.println(result);
}
public static void main(String[] args) {
doOp(new AndOperation());
doOp(new OrOperation());
}
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Please upvote my answer .
Thank you.