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 trialbabasariffodeen
6,475 PointsC# Objects Methods First Task help! I do not know how to go about answering this
I figured the bool can be called anything so I decided 'canReach' which will either be true or false.
Please tell me where I am going wrong!
namespace Treehouse.CodeChallenges
{
class Frog
{
public readonly int TongueLength;
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
public bool EatFly(Frog distanceToFly)
{
bool canReach = (distanceToFly >= Frog.TongueLength);
return;
}
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! You've got a couple of problems here. First, the method is supposed to accept an integer as the distanceToFly
. Your method is accepting a Frog. Secondly, you have a return statement but it's not actually returning anything. And third, there's a logic flaw in your evaluation. If the fly is 18 inches away, but the frog's tongue is only 7 inches long then he/she isn't going to be able to eat that fly!
Here's how I did it:
public bool EatFly(int distanceToFly)
{
return distanceToFly <= TongueLength;
}
My method accepts an integer which is the distance to fly. If the distance to fly is less than or equal to the length of the frog's tongue, then a value of true is returned. Otherwise, false is returned. Hope this helps!
babasariffodeen
6,475 Pointsbabasariffodeen
6,475 PointsThank you.
Would it also be correct if I wrote:
?
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse Teacherbabasariffodeen Yes, that would also work just fine! I just chose to return it directly. But they both accomplish the same thing