Jump to content

Help with C#

SickSix66
Go to solution Solved by PlutoNZL,

Why not? The way you have implemented Circle and Rectangle allow them to be compared.

Console.WriteLine(r1.IsLarger(c1));

prints True.

{    abstract class Shape    {        public string Type;        abstract public double CalcArea();        public void Description()        {            Console.WriteLine("Type={0}, Area={1}", Type, CalcArea());        }    }}{    class Rectangle : Shape, IComparable    {        public double Length;        public double Width;               public override double CalcArea()        {            return Length * Width;        }        public Rectangle(double l, double w)        {            Length = l;            Width = w;            Type = "Rectangle";        }        public bool IsLarger(IComparable obj)        {            if (this.CalcArea() > ((Shape)obj).CalcArea())            {                return true;            }            else            {                return false;            }                    }    }}{    interface IComparable    {        bool IsLarger(IComparable obj);    }}{    class Program    {        static void Main(string[] args)        {            Rectangle r1 = new Rectangle(12.0, 4.0);            r1.Description();            Circle c1 = new Circle(3.5);            c1.Description();        }    }}{    class Circle : Shape, IComparable    {        public double Radius;        public override double CalcArea()        {            return 3.14 * Radius * Radius;        }        public Circle(double r)        {            Radius = r;            Type = "Circle";        }        public bool IsLarger(IComparable obj)        {            if (this.CalcArea() > ((Shape)obj).CalcArea())            {                return true;            }            else            {                return false;            }        }    }}

Need to get the output True or False, compare square and circle. I can't seem to get the output however, calculating the area is a success.

The output is this:

Type=Rectangle, Area=48

Type=Circle, Area=38.465
 
But I can't get the true/false comparison between the rectangle and circle.
Link to comment
Share on other sites

Link to post
Share on other sites

Why not? The way you have implemented Circle and Rectangle allow them to be compared.

Console.WriteLine(r1.IsLarger(c1));

prints True.

Link to comment
Share on other sites

Link to post
Share on other sites

Nevermind guys thanks for all the help, I figured out what's wrong and I was missing this code on the Program class.

Console.WriteLine(r1.IsLarger((c2)));
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×