Hi I was wondering if someone could help me explain why I have to override the hashCode() method in this class and provide any other tips on making my code more readable. Thanks!
public class Synapse
{
private InputCell inputCell;
private float permanenceValue;
/**
* Amount the permanenceValue is increased during learning.
*/
public static final float PERMANCE_INCREASE = 0.015f;
/**
* Amount the permanenceValue is decreased during learning.
*/
public static final float PERMANCE_DECREASE = 0.005f;
/**
* Minimal permanenceValue needed for a Synapse to be connected.
*/
public static final float MINIMAL_CONNECTED_PERMANCE = 0.2f;
/**
* Initial permanceValues for distal Synapses.
*/
public static final float INITIAL_PERMANENCE = 0.3f;
// ----------------------------------------------------------
/**
* Create a new Synapse object.
*
* @param inputSource
*/
public Synapse(InputCell inputSource)
{
this.inputCell = inputSource;
this.permanenceValue = INITIAL_PERMANENCE;
}
// ----------------------------------------------------------
/**
* Gets the inputCell for a Synapse object.
*
* @return The object providing binary input states to this Synapse.
*/
public InputCell getInputCell()
{
return this.inputCell;
}
// ----------------------------------------------------------
/**
* Sets a new inputSource for a Synapse object.
*
* @param inputCell
* The new inputSource to this Synapse.
*/
public void setInputCell(InputCell inputCell)
{
this.inputCell = inputCell;
}
// ----------------------------------------------------------
/**
* Get the private permanenceValue field of a Synapse.
*
* @return The permanence value of a Synapse object.
*/
public float getPermanenceValue()
{
return this.permanenceValue;
}
// ----------------------------------------------------------
/**
* Sets the permanenceValue for a Synapse object.
*
* @param permanenceValue
* The new permanenceValue for a Synapse object.
*/
public void setPermanenceValue(float permanenceValue)
{
this.permanenceValue = permanenceValue;
}
// ----------------------------------------------------------
/**
* Place a description of your method here.
*/
public void increasePermance()
{
this.permanenceValue =
Math.min(1.0f, this.permanenceValue + PERMANCE_INCREASE);
}
// ----------------------------------------------------------
/**
* Place a description of your method here.
*/
public void decreasePermance()
{
this.permanenceValue =
Math.max(0.0f, this.permanenceValue - PERMANCE_DECREASE);
}
// ----------------------------------------------------------
/**
* Return whether this Synapse's permanenceValue is greater than the minimal
* connected permanenceValue.
*
* @return true if this Synapse is currently connected; otherwise false.
*/
public boolean getConnectedState()
{
return this.permanenceValue >= MINIMAL_CONNECTED_PERMANCE;
}
// ----------------------------------------------------------
/**
* Return whether this Synapse's permanenceVale is high enough to be active
* due to the current input. No need for setter method to change input.
*
* @return true if this Synapse is active; otherwise false.
*/
public boolean getActiveState()
{
return this.inputCell.getActiveState() && this.getConnectedState();
}
// ----------------------------------------------------------
/**
* Return whether this Synapse is active due to the previous input. No need
* for setter method to change previous input.
*
* @return true if this Synapse was active due to the previous input at t-1.
*/
public boolean getPreviousActiveState()
{
return this.inputCell.getPreviousActiveState()
&& this.getConnectedState();
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n============================");
stringBuilder.append("\nSynapse Information");
stringBuilder.append("\ninputSourceXPosition: ");
stringBuilder.append(this.inputCell.getX());
stringBuilder.append("\ninputSourceYPosition: ");
stringBuilder.append(this.inputCell.getY());
stringBuilder.append("\npermanenceValue: ");
stringBuilder.append(this.getPermanenceValue());
stringBuilder.append("\nconnectedState: ");
stringBuilder.append(this.getConnectedState());
stringBuilder.append("\nactiveState: ");
stringBuilder.append(this.getActiveState());
stringBuilder.append("\npreviousActiveState: ");
stringBuilder.append(this.getPreviousActiveState());
stringBuilder.append("\n============================");
String synapseInformation = stringBuilder.toString();
return synapseInformation;
}
@Override
public boolean equals(Object object)
{
if (this == object)
{
return true;
}
if (this == null || object.getClass() != this.getClass())
{
return false;
}
Synapse newSynapse = (Synapse)object;
return this.inputCell != null
&& (this.inputCell.equals(newSynapse.getInputCell()) && this.permanenceValue == newSynapse
.getPermanenceValue());
}
// why does this method need to overriden.
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result =
prime
* result
+ ((this.inputCell.equals(null)) ? 0 : this.inputCell
.hashCode());
result = (int)(prime * result + this.permanenceValue);
return result;
}
}