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

What is wrong with this code ?

I can't figure what is wrong with this code.... The quizz told me to add another EatFly class with 2 integer parameters and to return true if the frog length is longer then the fly distance and if the reaction time of the frog is faster then the fly reaction time. When i try to verify if i did it right it says : " Bummer! Did you create a new Eatfly method with the distanceToFly and flyReactionTime parameters"

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  Eatfly(distanceToFly) && (ReactionTime <= flyReactionTime);



        }



    }
}

4 Answers

Kristian Gausel
Kristian Gausel
14,661 Points

You didn't capitalize the methodcall to EatFly:

return  Eatfly(distanceToFly) && (ReactionTime <= flyReactionTime);

Should be

return  EatFly(distanceToFly) && (ReactionTime <= flyReactionTime);

Thank you for the answer , but i still have the same problem.....it keeps telling me " Bummer! Did you create a new Eatfly method with the distanceToFly and flyReactionTime parameters"

Kristian Gausel
Kristian Gausel
14,661 Points
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  EatFly(distanceToFly) && (ReactionTime <= flyReactionTime);



        }



    }
}

Try that then, I removed a space here between the parameters:

public bool EatFly(int distanceToFly, int flyReactionTime)
Adam Leven
Adam Leven
2,225 Points

The second set of parenthesis aren't needed.

That worked. Thank you very much sir.

Kristian Gausel
Kristian Gausel
14,661 Points

Please mark as best answer if you are satisfied =) and no problem

I ran into an issue with the conditions in the second method:

 public bool EatFly(int distanceToFly, int flyReactionTime)
        {
            return TongueLength >= distanceToFly && ReactionTime >= flyReactionTime;
        }

Strangely, the code was correct after changing the logic, requiring the frog's reaction time to be less than or equal to the fly's!

ReactionTIme <= flyReactionTime;
Nicholas Wallen
Nicholas Wallen
12,278 Points

This is correct. The question and the answer do not match.

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