/* * CSP Project * Authors: * Jay Bernardo * Matt Rancourt * * File: AbstractVariable.java * * Contains the AbstractVariable class. This class is used to represent variables in the CSP * problem domain. In our impementation we have both the TransitionAssignmentVariable and regular * Variable * * See: TransitionAssignmentVariable.java * Variable.java * */ abstract class AbstractVariable{ String name; int value; boolean positive; /** * Basic constructor. Creates a variable with a given name * and value * @param name - name of the variable * @param value - value assigned to the variable */ public AbstractVariable(String name, int value, boolean positive) throws InvalidVariableValueException{ this.name = name; this.positive = positive; this.setValue(value); } /** * Sets the value of the variable * @param value - value to set the variable to */ public void setValue(int value) throws InvalidVariableValueException{ if(value < -1 || value > 1) throw new InvalidVariableValueException(); this.value = value; } /** * Returns the value of the variable * @return value of variable as an int */ public int getValue(){ if(positive == false){ if(value == 0) return 1; else if(value == 1) return 0; else return value; } return value; } /** * Returns the name of the variable * @return name of the variable as a String */ public String getName(){ return name; } /** * Sets whether this variable is a positive or not * @param p - whether the variable is a positive or not */ public void setPositive(boolean p){ positive = p; } /** * Returns whether this variable is positive or * negative * @return true if positive, false otherwise */ public boolean getPositive(){ return positive; } /** * Prints the variable in a "pretty" format */ public void print(){ if(positive == false){ System.out.print("!"); } System.out.print(name); //System.out.print(name + "[" + value + "]"); } }