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 trial

C# C# Objects Methods Method Overloading

Pulla Rao Charugalla
PLUS
Pulla Rao Charugalla
Courses Plus Student 2,755 Points

I've been stuck for days with a problem i have no idea how to solve

Please fix the problem in the attached file as soon as possible. I could not proceed

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;

        public Frog(int tongueLength, int reactionTime)
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
        public bool EatFly(int distanceToFly, int flyReactionTime)
        {
           // return true;
           if ((TongueLength >= distanceToFly) && (ReactionTime >= flyReactionTime))
                return true;
           else return false;          
        }
    }
}

2 Answers

Gyorgy Andorka
Gyorgy Andorka
13,811 Points

I've tried your code, and it gives a hint actually: "Bummer! If the frog's reaction time is greater than the fly's reaction time, the frog can't get the fly." If the frog has a faster reaction time, it means it is less than the fly's.

By the way, in such situations you can simply return the boolean expression itself (since it evaluates to true or false anyway), no need to write out the if-else blocks:

return ((TongueLength >= distanceToFly) && (ReactionTime <= flyReactionTime));
Steven Parker
Steven Parker
229,786 Points

Another reason to not wrap the returns in if...else is that the compiler might otherwise think you failed to return a value in all code paths.

Gyorgy Andorka
Gyorgy Andorka
13,811 Points

"The compiler might otherwise think you failed to return a value in all code paths" - could you elaborate, Steven? I don't really understand what you mean by this.

Steven Parker
Steven Parker
229,786 Points

The compiler might not understand that the conditional code covers all possible cases. So it might give you a compile error. Try it out and see.

Gyorgy Andorka
Gyorgy Andorka
13,811 Points

But if there is an else branch (with a return statement) then all possible cases must have been covered (by definition), mustn't they?

Steven Parker
Steven Parker
229,786 Points

Logically, yes. But would the compiler still give an error? Try it out and see.

James Churchill
STAFF
James Churchill
Treehouse Teacher

Pulla,

Were you able to solve the code challenge after Gyorgy's suggestion? If not, how can we further help?

Thanks ~James