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 #35 (Oop2)

using System; namespace OOP_CONSTRUCTORS { class Animals     {         private string name;         private string sound;         public const string shelter = "Aibo's Hangout";         public Animals()             : this("No Name", "No Sound") { }         public Animals(string name)             : this(name, "No Sound") { }         public Animals(string n, string s)         {             setName(n);             SoundCap = s;             Console.WriteLine(SoundCap);             Counter = 1;             Console.WriteLine(Count);         }         public void introduced()         {             Console.WriteLine("Voice of {0} sounds like {1}", name, sound);         }         public void setName(string n)         {             int i = 1;                foreach(char c in n)                {                    if (!char.IsLetter(c))                    {                        i = 0;                    }

C#: Program #24 (DoWhile Loop)

using System; namespace DoWhile {     class Program     {         static void Main(string[] args)         {             Random no = new Random();             int numbergenerated = no.Next(1,20);             int numberguessed;             int turns=0;             do             {                 if (turns != 0)                 {                     Console.WriteLine("Guessed Wrong Number!");                 }                 Console.WriteLine("Guess a number between 1 and 20");                 numberguessed = Convert.ToInt32(Console.ReadLine());                 turns++;             } while (numberguessed != numbergenerated);             Console.WriteLine("Correct you got it in {0} turns",turns);             Console.ReadKey();         }     } }