Example #1
1
        /* 01. Define abstract class Shape with only one abstract method CalculateSurface() and
         * fields width and height. Define two new classes Triangle and Rectangle that 
         * implement the virtual method and return the surface of the figure (height*width
         * for rectangle and height*width/2 for triangle). Define class Circle and suitable
         * constructor so that at initialization height must be kept equal to width and 
         * implement the CalculateSurface() method. Write a program that tests the behavior
         * of  the CalculateSurface() method for different shapes (Circle, Rectangle, Triangle)
         * stored in an array.
        */

        static void Main()
        {
            //initialize an array of shapes and calculate their surface
            Shape[] shapes = new Shape[5] { new Circle(4), new Rectangle(3, 4), new Triangle(3, 4), new Circle(5), new Rectangle(4, 5) };

            foreach (var shape in shapes)
            {
                Console.WriteLine("Type: {0,10} Surface: {1:0.00}", shape.GetType().Name, shape.CalculateSurface());
            }
        }
 static void Main()
 {
     Shape[] shapes = new Shape[]
     {
         new Rectangle(5.0m, 4.5m),
         new Triangle(3.1m, 5.5m),
         new Square(3.5m)
     };
     Console.WriteLine("Rectangle surface : {0:F2}", shapes[0].CalculateSurface());
     Console.WriteLine("Triangle surface : {0:F2}", shapes[1].CalculateSurface());
     Console.WriteLine("Square surface : {0:F2}", shapes[2].CalculateSurface());
 }
Example #3
0
        static void Main(string[] args)
        {
            //Testing the shapes :
            Shape[] shapes = new Shape[3];
            shapes[0] = new Triangle(5, 5);
            shapes[1] = new Rectangle(10, 10);
            shapes[2] = new Circle(15);

            //Printing on the console:
            foreach (var shape in shapes)
            {
                Console.WriteLine("The surface of the {0} is {1:F2}.",shape.GetType().Name, shape.CalculateSurface());
            }
        }
Example #4
0
		static void Main()
		{
			var shapes = new Shape[]
						 {
							 new Circle(3.4),
							 new Triangle(2.67, 3.45),
							 new Rectangle(4.5, 5.89) 
						 };

			foreach (var shape in shapes)
			{
				Console.WriteLine("Surface of {0}: {1:F2}", shape.GetType().Name, shape.CalculateSurface());
			}
		}
        static void Main(string[] args)
        {
            Shape[] shapes = new Shape[]
            {
                new Triangle(5, 7),
                new Rectangle(6, 8),
                new Square(9)
            };

            foreach (Shape element in shapes)
            {
                Console.WriteLine("{0}: Surface is {1}", element.GetType().Name, element.CalculateSurface());
            }
        }
Example #6
0
        static void Main()
        {
            Shape[] shapes = new Shape[]
            {
                new Triangle(5,10),
                new Rectangle(5,10),
                new Square(4)
            };

            foreach (var item in shapes)
            {
                Console.WriteLine("{0}'s surface is {1}", item.GetType(), item.CalculateSurface());
            }
        }
Example #7
0
        static void Main()
        {
            Shape[] shapes = new Shape[]
            {
                new Square (2),
                new Rectangle(2, 3),
                new Triangle (2, 3)
            };

            foreach (Shape item in shapes)
            {
                Console.WriteLine("The {0} surface is {1:F2}", item.GetType().Name.ToLower(), item.CalculateSurface());
            }
        }
Example #8
0
        public static void Shape()
        {
            Shape[] myShapes = new Shape[] {

                new Circle(3),
                new Rectangle(2,3),
                new Triangle(2,3)

            };

            foreach (Shape item in myShapes)
            {
                Console.WriteLine("Shape {0} has surface: {1}", item.GetType(), item.CalculateSurface());
            }
        }
        public static void Main()
        {
            Shape[] shapes = new Shape[]
            {
                new Square (3),
                new Circle (2),
                new Rectangle (3, 4),
                new Rectangle (3.5, 4.5),
                new Circle (3.5),
                new Square (5.5),
                new Square  (3),
                new Triangle (4 , 5)
            };

            foreach (var shape in shapes)
            {
                Console.WriteLine("Figure = {0} surface = {1:F2}",shape.GetType().Name.PadRight(10, ' '),shape.CalcSurface());
            }
        }
        static void Main()
        {
            var testShapes = new Shape[]
            {
                new Triangle(7.5 , 12),
                new Triangle(5 , 7.5),

                new Square(5),
                new Square(3.75),

                new Rectangle(1.5, 10),
                new Rectangle(3, 6.45),
            };

            foreach (var shapes in testShapes)
            {
                Console.WriteLine(string.Format("{0} has a surface of {1}"
                    , shapes.GetType().Name, shapes.CalculateSurface()));
            }

            Console.WriteLine();
        }