Skip to main content

C#: Program #40 (Oop5)

using System;


namespace OOP5
{
    class Program
    {
        static void Main(string[] args)
        {
            Shape[] shapes = {new Square(4),new Circle(4),new RECTANGLE(4,3)};
            foreach (Shape s in shapes)
            {
                s.Getinformation();
                Console.WriteLine("{0} has area of {1:f2}",s.Name,s.Area());
                Circle objcircle = s as Circle;
                if (objcircle == null)
                {
                    Console.WriteLine("This is not a circle");

                }
                if(s is Circle)
                {
                    Console.WriteLine("This is a circle");
                }

                object sq=new Square(5);
                Square sq1 = (Square)sq;
                Console.WriteLine("{0} has area of {1:f2}", sq1.Name, sq1.Area());
               

            }



            Console.ReadKey();
        }
    }

 abstract class Shape
    {
        public string Name { get; set;}
        public virtual void Getinformation(){
        Console.WriteLine("{0} Name is",Name);
        }
        public abstract double Area();

    }
class Square:Shape
    {
          public double Length { get; set;}
       
        public Square(double l)
        {
            Name = "Square";
            Length = l;
         
        }
        public override void Getinformation()
        {
           base.Getinformation();
           Console.WriteLine("The Length is {0}",Length);
        }
        public override double Area()
        {
            return Length*Length;
        }
    }
class Circle:Shape
    {
        public double Radius { get; set;}
        public Circle(double radius)
        {
            Name = "Circle";
            Radius = radius;

        }
        public override void Getinformation()
        {
            base.Getinformation();
            Console.WriteLine("The Radius is {0}", Radius);
        }
        public override double Area()
        {
            return 2*Math.PI*Math.Pow(Radius,2);
        }
    }
class RECTANGLE:Shape
    {

        public double Length { get; set;}
        public double Width { get; set;}
        public RECTANGLE(double l,double w)
        {
            Name = "Rectangle";
            Length = l;
            Width = w;
        }
        public override void Getinformation()
        {
           base.Getinformation();
           Console.WriteLine("The Length is {0} and width is {1}",Length,Width);
        }
        public override double Area()
        {
            return Length*Width;
        }

    }









}

Comments

Popular posts from this blog

C#: Program #51(Using Linq Extension Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods {     class Program     {         static void Main(string[] args)         {             List<int> tosses = new List<int>();             Random rndm = new Random();             int i=0;             while (i < 100)             {                 tosses.Add(rndm.Next(1,3));                 i++;             }             Console.WriteLine("HEAD:{0}",tosses.Where(x => x == 1).Count());             Console.WriteLine("TAIL:{0}", tosses.Where(x => x == 2).Count());         ...

C#: Program #52(Using Linq Extension Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods2 {     class Program     {         static void Main(string[] args)         {             List<int> series = new List<int>();             series.AddRange(Enumerable.Range(1, 20));             var squares = series.Select(a=>a*a).ToList();             foreach (int i in squares)             {                 Console.WriteLine(i);             }             Console.WriteLine("The sum of the two");             List<int> lone = new List<int>{1,5,6,3,2};             List<int> ltwo = new List...