i am trying to figure this exercise. i just started, but i dont understand what do i need to put where ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapesDemo
{
     abstract  class GeometricFigure
     {
            int height;
            int width;
            double area;

          /*  public GeometricFigure(int Height, int Width)
            {
                Height = height;
                Width = width;
            }*/

            public int Heigth
            {
                get { return height; }
                set { height = value; }
            }

            public int Width
            {
                get { return width; }
                set { width = value; }
            }

            public double area
            {
                get { return area; }
            }

        }

         static void Main(string[] args)
        {


        }
}
           
       /*     class Rectangle : GeometricFigure
            {
                public     () : base()
                {


                }
                public override ()
                {
            
            
                }
            }*/

Recommended Answers

All 7 Replies

in this abstract class you should do the data members and methods that are generic(common) in all the subclasses.

let say you have a rectangle[length x width] and a Trapezium [ 1/2 (top+base) x height ]:

in the abstract class you should do data members like : double width/base, double height .. since these are both common in both shapes and can do an abstract method called "Calculate area", but do not add code to this method in this class since it is always a different calculation for each shape to calculate its area.

then in the rectangle class you just inherit from the base class and just add the equation to calculate the area of a rectangle in the override method.

and in the Trapezium class you should inherit also from the base class and add the data member : double top and add the equation to calculate the area of a trapezium in the override method.

i am more a visual learner can you give some examples i am new to c# so i can learn better if i will see it

Hi, christine0293 i really need some help this is what i got so far :

i am an international student so the english is another obstacle for me

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapesDemo
{
    abstract class GeometricFigure
    {
        
        
        
       
        
        public int height;
        public int width;
        public readonly double area;

        public int heigth
        {get { return height; }
            set { height = value; } }

        public int Width
        {get { return width; }
            set { width = value; }}

        



          static void Main(string[] args)
          {


          } 
    }
            abstract class ComputeArea 
            {
             
            }

            class Rectangle : GeometricFigure
            {
                public Rectangle(double area)
                {
                    area = width * height;

                }


                class Square : Rectangle
                {
                    public  Square(int height, int width)
                    {
                        if (height.Equals(width))
                            Console.WriteLine("{0} for {1} has the same messers " +
                                height, width);

                    }
                }

                class Triangle : GeometricFigure
                {
                    public Triangle(double area)
                    {
                        area = width * (height / 2);

                    }


                }
            }
}

hi sory for the late reply ..

you should do something like the example below !!

i have done something like this :

abstract Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shapes
{
    class Point
    {
        private int coorX;
        private int coorY;

        public Point()
        { }

        public Point(int _CoorX, int _CoorY)
        {
            CoorX = _CoorX;
            CoorY = _CoorY;
        }

        public int CoorX
        {
            get { return coorX; }
            set { coorX = value; }
        }

        public int CoorY
        {
            get { return coorY; }
            set { coorY = value; }
        }

        public virtual double GetArea()
        {
            return 0.0;
        }

        public override string ToString()
        {
            return CoorX + "\t\t" + CoorY;
        }
    }
}

circle Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shapes
{
    class Circle:Point
    {
        private double radius;

        public Circle(double _Radius, int _CoorX, int _CoorY)
            : base(_CoorX, _CoorY)
        {
            Radius = _Radius;
        }

        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }

        public override double GetArea()
        {
            double area = Math.PI * Math.Pow(Radius, 2); 
            return area;
        }

        public override string ToString()
        {
           return base.ToString() + "\t\t" + Radius;
        }

    }
}

rectangle Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shapes
{
    class Rectangle:Point
    {
        private double length;
        private double width;

        public Rectangle(double _Lenght, double _Width, int _CoorX, int _CoorY)
            : base(_CoorX, _CoorY)
        {
            Length = _Lenght;
            Width = _Width;
        }

        public double Length
        {
            get { return length; }
            set { length = value; }
        }

        public double Width
        {
            get { return width; }
            set { width = value; }
        }

        public override double GetArea()
        {
            double area = Length * Width;
            return area;
        }

        public override string ToString()
        {
           return base.ToString() + "\t\t" + Length + "\t\t" + Width;
        }
       
    }
}

triangle Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shapes
{
    class Triangle:Point
    {
        private double length;
        private double height;

        public Triangle(double _Lenght, double _Height, int _CoorX, int _CoorY)
            : base(_CoorX, _CoorY)
        {
            Length = _Lenght;
            Height = _Height;
        }

        public double Length
        {
            get { return length; }
            set { length = value; }
        }

        public double Height
        {
            get { return height; }
            set { height = value; }
        }

        public override double GetArea()
        {
            double area = (Length * Height)/2;
            return area;
        }

        public override string ToString()
        {
           return base.ToString() + "\t\t" + Length + "\t\t" + Height;
        }

    }
}

did i solve your problem?

if it is solved, pls mark the thread as solve..

i kinda did it like this, is this answer the requirement ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapesDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            Square s = new Square(4,5);
            Triangle t = new Triangle(4,9);
            Console.WriteLine("Computed area is {0}" +"\n\n"+"Computed Triangle is: {1}"+"\n", s.ComputeArea(r.Area),t.ComputeArea(r.Area));
        }
    }
    abstract class GeometricFigure
    {
        public double Width { get; set; }
        public double Height { get; set; }        
        public readonly double Area;
 
        public abstract double ComputeArea(double _Area);
    }
 
    class Rectangle : GeometricFigure
    {
        public override double ComputeArea(double _Area)
        {
            return _Area = this.Width * this.Height;            
        }
    }
 
    class Square : Rectangle
    {
        public Square(int x, int y)
        {this.Width = x;
            this.Height = y;
            if (x != y)
            this.Height = this.Width;
        }
 
        public Square(int xy)
        {this.Width = xy;
            this.Height = this.Width;}

        public override double ComputeArea(double _Area)
        { return _Area = this.Width * this.Height; }

    }
    class Triangle : GeometricFigure
    { public Triangle( int x, int y)
        {this.Width = x;
        this.Height = y;}

        public override double ComputeArea(double _Area)
        { return _Area = (this.Width * this.Height) / 2;}
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.