0

I'm trying to create a program that passes arguments of size of three shapes, a circle, a triangle, and a rectangle. I got those but everytime I run the code it displays the arguments as 0 instead of giving me the shape dimension. Here is the code.

using System;
class TestShapeHierarchy 
{
   static void Main(string[] args)
   {
      TwoDimShape[] myShapes = new TwoDimShape[3];
      myShapes[0] = new Rectangle(10, 5);
      myShapes[1] = new Triangle(12, 6);
      myShapes[2] = new Circle(8);

      foreach (TwoDimShape tds in myShapes)
      {
         ProcessShape(tds);
         Console.WriteLine("");


         {

         }
      }
   }

   // you need to write a static method processShape
   static void ProcessShape(TwoDimShape shape)
   {
      Console.WriteLine(shape);
   } 
}



using System;

public class Circle : TwoDimShape
{
   private double radius;

   public Circle (double rad)
   {
      Radius = rad;
   }

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


   public override double Area()
   {
      return Radius * Radius * Math.PI;
   }

   public override string ToString()
   {
      return string.Format("It is a circle.\nradius = {0} area = {1}", Radius, Area());
   }
}



using System;

public class Triangle : TwoDimShape
{
   private double bas;
   private double height;

   public Triangle(double length, double width)
   {
      Base = width;
      Height = length;
   }

   public double Base
   {
      get
      {
         return bas;
      }
      set
      {
         value = bas;
      }
   }

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



   public override double Area()
   {
      return .5 * Base * Height;
   }

   public override string ToString()
   {
      return string.Format("It is a triangle.\nbase={0} height={1} area={2}", Base, Height, Area());
   }
}



using System;

public class Rectangle : TwoDimShape
{
   private double length;
   private double width;

   public Rectangle(double len, double wid)
   {
      Length = len;
      Width = wid;
   }

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

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

   public override double Area()
   {
      return Length * Width;
   }

   public override string ToString()
   {
      return string.Format("It is a rectangle.\nlength={0} width={1} area={2}", 
         Length, Width, Area());
   }

}



using System;

public abstract class TwoDimShape
{

   public abstract double Area();
}
2
Contributors
1
Reply
21
Views
3 Years
Discussion Span
Last Post by __avd
0

This thread is already been marked as solved without adding justification.

In opening post, there are issues with properties definition especially to set accessor where value parameter replaces the field.

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

I think one should have to use auto-implemented property to avoid such issues.

public class Circle : TwoDimShape
{
   public double Radius {get;set;}
   public Circle (double rad)
   {
      Radius = rad;
   }
   ...... 
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.