-1

I have the problem, that my equals method doesnt work as i want it to. I want to implement a deterministic turing machine, so I want to add the method findCommand(), which searchs through a arraylist of commands. So I decided to create a searchDummy to find all Transitions that are available for the Configuration I have.

Class States:

public class States {

private int stateId;
private boolean rejState;
private boolean accState;
private boolean stopState;
private List<Commands> commands = new ArrayList<Commands>();

equals in class States:

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof States) {
        States otherState = (States) other;
        return (stateId == otherState.stateId);
    } else {
        return false;
    }
}

hashCode:

@Override public int hashCode() {
    StringBuilder b = new StringBuilder(stateId);
    return b.toString().hashCode();
    }

this is the findCommand method in States:

    public Commands findCommand(States state, char inputTapeChar, 
        char[] tapeChars) {
    Commands searchDummy = new Commands(state, inputTapeChar, tapeChars, 
            null, null, null, null);
    int pos = commands.indexOf(searchDummy);
    return pos >= 0 ? commands.get(pos) : null;
}

commands is my arraylist, so I want to find the searchDummy with indexOf().

I have the class Commands, which holds the attribute Configuration configuration, the class Configuration, which holds the attributes of a Configuration and the attribute Transition transition and the class transition that holds the attributes for itself.

Class Commands:

public class Commands implements Comparable<Commands> {

private Configuration configuration;

Class Configuration:

public class Configuration {

private Transition transition;
private States state;
private char inputTapeChar;
private char[] tapeChars;

Class Transition:

public class Transition {

private States targetState;
private Direction inputTapeHeadMove;
private char[] newTapeChars;
private Direction[] tapeHeadMoves;

i have this equals method in Commands:

@Override public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Commands) {
Commands otherCmd = (Commands) other;
return (configuration.equals(otherCmd.configuration));
} else { 
return false; 
 }
}

and this hashcode

    @Override
    public int hashCode() {
    StringBuilder b = new StringBuilder(configuration.getState() + "," 
    + configuration.getInputTapeChar());
    for (char c : configuration.getTapeChars()) {
        b.append("," + c);
    }
    return b.toString().hashCode();
    }

then almost the same in Configuration:

    @Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof Configuration) {
        Configuration otherConfi = (Configuration) other;
        return (state.equals(otherConfi.state))
               && (inputTapeChar == otherConfi.inputTapeChar)
               && (Arrays.equals(tapeChars, otherConfi.tapeChars));
    } else {
        return false;
    }
}

hashcode:

@Override
public int hashCode() {
    StringBuilder b = new StringBuilder(state + "," + inputTapeChar);
    for (char c : tapeChars) {
        b.append("," + c);
    }
    return b.toString().hashCode();
}

equales in class State:

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof States) {
        States otherState = (States) other;
        return (stateId == otherState.stateId);
    } else {
        return false;
    }
}

so my question: when I debug this it goes through until it's finished with the checks but when it should return the value it stucks at Configuration.equals(...) and shows the error no source found!

what is the problem? Are the hashcodes wrong? Or are the equals wrong?

I never used equals before so I dont know when i need to use it or how i need to fix this. thanks for your help.

Wosch
  • 11
  • 1
  • I didn't even look at the `hashCode` implementations, as it doesn't look like it's used in your case. But yes, you'll definitely need to define the `equals` for your `States` class as well. Otherwise, `state.equals(otherConfi.state)` will perform reference equality by default. I don't know about that specific error you're getting though. – sstan May 26 '16 at 17:40
  • 1
    Possible duplicate of [Why do I need to override the equals and hashCode methods in Java?](http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) – Lee May 26 '16 at 17:45
  • @sstan i edited the attributes of States and the equals and hashcode method in it. – Wosch May 26 '16 at 17:50
  • @Lee are my hashCode and equals methods right? but i still have the problem, that the equals won't return anything to IndexOf() in findcommand(). I get the error no source found. – Wosch May 26 '16 at 18:01

1 Answers1

0

Your hashCode implementation looks suspect - all that String stuff is not standard.

For example for your Transition class should be something like this:

@Override
public int hashCode() {
    int result = 17;
    result = 31 * result + targetState.hashCode();
    result = 31 * result + inputTapeHeadMove.hashCode();
    result = 31 * result + newTapeChars.hashCode();
    result = 31 * tapeHeadMoves.hashCode();
    return result;
}

Most IDEs will offer autogen of hashCode and equals methods.

Lee
  • 738
  • 3
  • 13