/**
* Solutions objects represent a valid solution to this puzzler
*/
public class Solutions implements Comparable<Object> {
private float value; //The calculation result
private int operation0; //The operator between first two operands
private int operation1; //The operator between 2nd and 3rd operands
private int operation2; //The operator between 3rd and 4th operands
private int operand0; //First operand...
private int operand1; // ...
private int operand2;
private int operand3;
private String parenthesis; //String representing the position of the parentheses
//char[] operators = {'+', '-', '*', '/', '^'};
private static char[] operators = "+-*/^".toCharArray();
public Solutions (float val, int op0, int op1, int op2, int oper0, int oper1, int oper2, int oper3, String paren) {
value = val;
operation0 = op0;
operation1 = op1;
operation2 = op2;
operand0 = oper0;
operand1 = oper1;
operand2 = oper2;
operand3 = oper3;
parenthesis = paren;
}
public boolean equals(Object obj) {
/**
* Two Solutions are equal if they have the same value (calculation result)
*/
if (obj instanceof Solutions) {
Solutions solution = (Solutions)obj;
return solution.value == this.value;
}
else return false;
}
public int hashCode() {
/**
* This is needed to make equals work, otherwise Solutions with the same
* value will have different hashCodes and will therefore not be equal
*/
return (int)value;
}
public int compareTo(Object b) {
/**
* Solutions are compared by comparing the value (calculation result)
*/
int a = 0;
if (this.value > ((Solutions)b).value) a = 1;
else if (this.value < ((Solutions)b).value) a = -1;
else a = 0;
return a;
}
public String toString() {
/**
* Print the appropriate representation of the equation based on the parenthesis
*/
String resp = "";
if (parenthesis == "ABCD") {
resp = "(((" + operand0 + operators[operation0] + operand1 + ")" + operators[operation1] +
operand2 + ")" + operators[operation2] + operand3 + ") = " + value;
}
if (parenthesis == "A(BCD)") {
resp = "" + operand0 + operators[operation0] + "(" + operand1 + operators[operation1] +
operand2 + operators[operation2] + operand3 + ") = " + value;
}
if (parenthesis == "(AB)(CD)") {
resp = "(" + operand0 + operators[operation0] + operand1 + ")" + operators[operation1] +
"(" + operand2 + operators[operation2] + operand3 + ") = " + value;
}
if (parenthesis == "A(BC)D") {
resp = "" + operand0 + operators[operation0] + "(" + operand1 + operators[operation1] +
operand2 + ")" + operators[operation2] + operand3 + " = " + value;
}
if (parenthesis == "A(B(CD))") {
resp = "" + operand0 + operators[operation0] + "(" + operand1 + operators[operation1] + "(" +
operand2 + operators[operation2] + operand3 + ")) = " + value;
}
if (parenthesis == "A((BC)D") {
resp = operand0 + operators[operation0] + "((" + operand1 + operators[operation1] + "(" +
operand2 + ")" + operators[operation2] + operand3 + ") = " + value;
}
return resp;
}
// public static void main(String[] args) {
// Solutions a = new Solutions(10.0f, 1,2,3,4,5,6,7,"");
// Solutions b = new Solutions(11.0f, 10,2,3,4,5,6,7);
// Solutions c = new Solutions(11.0f, 10,2,3,4,5,6,7);
//
// Set solutions = new HashSet();
// solutions.add(a);
// solutions.add(b);
// solutions.add(c);
// System.out.println(solutions.size());
// System.out.println(a);
// }
}