How to get the max value of a function in a list of objects (C#) ? -Beginner-

first post here so I hope this is how I should ask questions here, So, I have a function used in different classes (override), and I need to display the MaxValue of this function across different data. (don't know if I said it correcty).

Tried this first but it kept giving me the same answer even though it wasn't the max value:

class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;
            
            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();
                
                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid1.Area();

                    if (area1 >= area2)
                    {
                        maximumArea = trapezoid1;
                    }
                    else
                    {
                        maximumArea = trapezoid2;
                    }
                }
            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();
        }
    }

the second time I tried this but couldn't make it work:

    class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;

            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();

                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid2.Area();

                    maximumArea = Math.Max (area1 , area2);
                }
            }

            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();

        }
    }

the function is Area, if you need to see it:

class Trapezoid
    {
        protected double a,b,h;

        public Trapezoid(double a, double b, double h)
        {
            this.a = a;
            this.b = b;
            this.h = h;
        }

        public virtual double Area()
        {            
            return ((a + b) * h) / 2;
        }
        public override string ToString()
        {
            return "The area of this Trapezoid is " + Area() + ".";
        }
    }

did the same thing in rectangle and square classes by overriding (inherited classes).

I hope you can help me, thank you. :)



from Recent Questions - Stack Overflow https://ift.tt/2NQ4bRA
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)