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 Encapsulation with Properties Computed Properties

Fahmi Eshaq
Fahmi Eshaq
3,002 Points

Write a computed property named Area that returns the area - Help

I keep getting this error: Bummer! Did you compute the area as the square of SideLength?

Where did I go wrong?

Square.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; }

        public double Area
        {
            get
            {
                return Math.Sqrt(SideLength);   
            }
        }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }  
    }
}
Polygon.cs
namespace Treehouse.CodeChallenges
{
    public class Polygon
    {
        public int NumSides { get; private set; }

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }
}
Carel Du Plessis
Carel Du Plessis
Courses Plus Student 16,356 Points
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }

        public double Area 
        {
            get{
                return SideLength * SideLength;
            }
        }
    }
}

4 Answers

andren
andren
28,508 Points

Your code is fine, the problem is your math, square root and square are (despite the name) not the same thing.

To square something means multiplying it by itself, the square root of something is a number which when squared will result in said number.

So 4 squared would be 16 (4*4 = 16), and the square root of 16 would be 4 (16 = 4*4).

So to solve this task you can either return "SideLength * SideLength", or if you prefer using the Math class you can use "Math.Pow(SideLength, 2)" Pow is a "power to" method, and raising a number to the power of 2 is the same as multiplying it by itself.

Clinton Johnson
Clinton Johnson
28,713 Points
using System;
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }

        public double Area
        {
            get
            {
                return Math.Pow(SideLength,2);
            }
        }
    }
}
Clinton Johnson
Clinton Johnson
28,713 Points

andren Thank you so much for your explanation. I have taken the liberty to post the code based off of both your explanation and what i've learned. Hope this helps the next person.

Also for everyone seeing this please review andren overview for explaning how to solve this challenge before looking at the code i posted, because both will help you understand and pass the challenge.

Thank you, Happy coding!

Alwin Landler
Alwin Landler
1,070 Points

Or if you don't know / don't want to search for the corresponding Math method for this case, you could also keep it as simple as: public double Area { get { return SideLength * SideLength; } }

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

that definitely helped me Clinton, thank you very much