Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is what i have so far this is what im testing assertTrue(init.toString().eq

ID: 3589271 • Letter: T

Question

this is what i have so far

this is what im testing
assertTrue(init.toString().equals("The value is: 0"));

here is my code the equals works im trying to get toString to work, any advice is appreciated.

import framework.problem.State;
import java.util.stream.Stream;

public class ArithmeticState implements State {
  
public ArithmeticState(int contents) {
this.contents = contents;
}

@Override
public boolean equals(Object other) {
ArithmeticState otherArithmetic = (ArithmeticState) other;

if (this.contents == (otherArithmetic.contents))
return true;
else
return false;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
  
sb.append("The value is:");
sb.append(this.contents);
return sb.toString();

  
}
private final int contents;

private static final String NEW_LINE = " ";
}

Explanation / Answer

Hi,
The toString method is an oveeridden method and should be called from an object instance of ArithmeticState ,
so makes sure you init in assert True of that class type, also, you have an extra space in the assert statement, either delete that or add that space in string buider in class
option1- assertTrue(init.toString().equals("The value is:0")); //make sure init is object of ArithmeticState
option2-

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
  
sb.append("The value is: ");//add space here
sb.append(this.contents);
return sb.toString();

  
}

Thumbs up if this was helpful, otherwise let me know in comments