lundi 28 septembre 2015

Java method that returns logical operators

Question first, then backstory, as to not make the question seem completly idiotic if it is infact not possible..

QUESTION: Is there any way to write a method that returns || or && or < or > depending on which implemented subclass is calling the method

I´m currently learning Java and taking a few courses at uni to that extent.

So in an assignment in a course concerning objective-based design patterns and discrete structures we are to write a program that simulates a logical proposition and examines it for the quality of tautologi. I.e if the expression is true for all possible inputs.

i have made a simple binarytree implementation and the program works, but the design part of the course calls for us to use such design patterns as "Composite" and "Template method" all very fashionable words, sure you´ve all heard of e´m.

i have a Template method pattern in my toString insothat the

abstract Class BinaryConnector extends Expression{
protected Expression a,b
toString(){return a + getConnective() + b;  }
abstract String getConnective();
}

wich in for example

Class Or extends BinaryConnective{
//omissions
public String getConnective(){return "||";}

i would like to do the same for the actual comparison aswell.

i suppose there are many ways of juryrigging such a solution, such as a switch-statement in my abstract class. But just elegant solutions please.. im hoping for something like" just parse the getConnective()" or similar:)

or scold me for making you read so much..

Aucun commentaire:

Enregistrer un commentaire