Hello!
I'm having a bit of confusion here regarding toStrings and Constructors. Namely, why would you ever need them?
I have a project for my course, and in it, I use a method that checks for a creature's "energyLevel" to see if that creature is still "alive". This method returns "true" or "false", and I think that I should probably use a toString() to convert something in one java file that gets called in a related java file. Here is the relevant code from the first file:
public boolean isAlive() {
if (energyLevel > 0) {
return true;
}
else {
return false;
} //end isAlive()
// *after here I'm lost*
public String toString() {
return isAlive;
}
That last "public String toString()" is not working at all. I imagine that I'm not doing the syntax correctly or, more likely, I'm making it up. I've done similar methods that return a variable, which I've successfully called from another java program, but I don't understand how to call more abstract things like "return true". Should I convert using toString()? If so, how? I've attached everything I have so far, just in case I didn't post enough code.
At the root, I'm not sure why or how I would you use toStrings to convert a boolean true/false to a string if I can just create a variable that gets changed inside your if/else clause.
On a completely similar note, how do I implement a "constructor", and under what conditions would they be useful?
I love the course I'm taking, and want to learn as much as I can, but these basic things are kind of weighing me down here. Any assistance would be greatly appreciated.