Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrea De Nuzzo
Courses Plus Student 639 PointsDo we really need to make applyGuess method return the boolean isHit? Would it work if it was a void method?
The method is this one
public boolean applyGuess(char letter) {
boolean isHit = answer.indexOf(letter) != -1;
if (isHit) {
hits += letter;
} else {
misses += letter;
} return isHit;
}
I was just wondering, if we just need to add a letter to the Strings hits and misses, why should we use a method that return a boolean? Wouldn't it work exactly the same if we had a void method that doesn't return the boolean isHit? Thanks!!
1 Answer
Steven Parker
231,269 PointsYou're right that the assignment is totally unrelated to returning a value. That aspect would work just the same if the function had no return (void).
But for demonstration purposes, returning a value allows you to know whether a hit or miss was scored when you call the method in the REPL.
It could also be potentially useful in a program so you could simultaneously register the result and use the determination in some conditional expression.
Andrea De Nuzzo
Courses Plus Student 639 PointsAndrea De Nuzzo
Courses Plus Student 639 PointsThanks for your reply, Steven Parker :)