Jump to content

Object-Oriented Programming/Polymorphism/C Sharp

From Wikiversity

main.cs

[edit | edit source]
// This program demonstrates use of the Polygon, Triangle, Rectangle, and Square classes.

using System;

class Program 
{
    public static void Main (string[] args)
    {
        ProcessPolygons();
        ProcessShapes();
    }

    private static void ProcessPolygons()
    {
        var triangle = new Polygon(new double[] {3, 4, 5});
        DisplaySides("triangle", triangle.Sides);
        Console.WriteLine("triangle perimeter: " + triangle.Perimeter.ToString());

        var rectangle = new Polygon(new double[] {4, 2, 4, 2});
        DisplaySides("rectangle", rectangle.Sides);
        Console.WriteLine("rectangle perimeter: " + rectangle.Perimeter.ToString());

        var square = new Polygon(new double[] {2, 2, 2, 2});
        DisplaySides("square", square.Sides);
        Console.WriteLine("square perimeter: " + square.Perimeter.ToString());
    }

    private static void ProcessShapes()
    {
        var triangle = new Triangle(3, 4, 5);
        DisplaySides("triangle", triangle.Sides);
        Console.WriteLine("triangle perimeter: " + triangle.Perimeter.ToString());
        Console.WriteLine("triangle area: " + triangle.Area.ToString());

        var rectangle = new Rectangle(4, 2);
        DisplaySides("rectangle", rectangle.Sides);
        Console.WriteLine("rectangle perimeter: " + rectangle.Perimeter.ToString());
        Console.WriteLine("rectangle area: " + rectangle.Area.ToString());

        var square = new Square(2);
        DisplaySides("square", square.Sides);
        Console.WriteLine("square perimeter: " + square.Perimeter.ToString());
        Console.WriteLine("square area: " + square.Area.ToString());
    }

    private static void DisplaySides(string label, double[] sides)
    {
        Console.Write(label + " sides: ");
        for (var i = 0; i < sides.Length; i++) {
            Console.Write(sides[i].ToString() + " ");
        }
        Console.WriteLine();
    }
}

polygon.cs

[edit | edit source]
// Supports polygon calculations for perimeter and area.

using System;

public class Polygon
{
    private double[] _sides;

    // Creates a polygon object.
    public Polygon(double[] sides)
    {
        this._sides = sides;
    }

    // Gets the polygon sides.
    public double[] Sides
    {
        get
        {
            return this._sides;
        }

        set
        {
            this._sides = value;
        }
    }    

    // Gets the polygon perimeter.
    public double Perimeter
    {
        get
        {
            double perimeter = 0.0;

            for (var i = 0; i < this.Sides.Length; i++) {
                perimeter += this.Sides[i];
            }

            return perimeter;
        }
    }
}

public class Triangle : Polygon {
    public Triangle(double side1, double side2, double side3) : base(new double[] {side1, side2, side3})
    {
        // Calling base constructor
    }

    // Gets the area of the triangle.
    // References:
    //  https://www.mathsisfun.com/geometry/herons-formula.html
    public double Area {
        get
        {
            var s = this.Perimeter / 2;
            var a = this.Sides[0];
            var b = this.Sides[1];
            var c = this.Sides[2];
            var area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
            return area;
        }
    }
}

public class Rectangle : Polygon {
    public Rectangle(double length, double width) : base(new double[] {length, width, length, width})
    {
        // Calling base constructor
    }

    // Gets the area of the rectangle.
    public double Area {
        get
        {
            var length = this.Sides[0];
            var width = this.Sides[1];
            if (length == width) {
                // Allow for sides in any order.
                width = this.Sides[2];
            }

            var area = length * width;
            return area;        }
    }
}

public class Square : Polygon {
    public Square(double side) : base(new double[] {side, side, side, side})
    {
        // Calling base constructor
    }

    // Gets the area of the square.
    public double Area {
        get
        {
            var side = this.Sides[0];
            var area = side * side;
            return area;
        }
    }
}

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.

See Also

[edit | edit source]