Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Methods Method Overloading

>= and ==

Shouldn't this operator be == rather than >= return TongueLenth >= distanceToFly && ReactionTime < flyReactionTime;

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 TongueLenth >= distanceToFly && ReactionTime < flyReactionTime; 
        }


    }
}
Eric M
Eric M
11,516 Points

Hi Brian, worthwhile question, why do you think this operator should be changed from greater than or equal to to exactly equal to?

3 Answers

Eric M
Eric M
11,516 Points

Hi Brian,

The frog can only eat the fly (without moving) if its tongue can reach the fly. How do we know if the tongue can reach the fly? If the tongue can extend to 10cm, then any fly within a 10cm radius of the frog can be eaten by the frog. How do we represent this in code?

If we say TongueLength == distanceToFly then this would be true if the fly were exactly 10cm away, but not 3cm away, or 2cm or 9cm. However if the frog can extend its tongue 10cm, it should be able to eat a fly within that distance, even if the fly is closer than exactly 10cm away.

By using TongueLength >= distanceToFly we are saying that so long as the tongue is greater than or equal to the distance between the frog and the fly, then it can eat it. 10cm is greater than 2cm, so the that would return true in the case of the fly only being 2cm away, or anything up to the tongue length. If the fly is too far away (if distanceToFly is greater than TongueLength) then the frog can't eat the fly because it's too far away.

We could also express this as distanceToFly <= TongueLength, which you may find a more intuitive representation of the same conditional logic.

Cheers,

Eric

Why do we need the tongue to be more than to eat the fly?

Cheers mate.