Code in C#: Implement the Shape hierarchy shown in Fig. 11.3. You may omit the T
ID: 664726 • Letter: C
Question
Code in C#: Implement the Shape hierarchy shown in Fig. 11.3. You may omit the Triangle and Tetrahedron classes. Each TwoDimensionalShape should contain read-only abstract property Area to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have read-only abstract properties Area and Volume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create an application that uses an array of Shape references to objects of each concrete class in the hierarchy. The application should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.
Explanation / Answer
using System;
namespace InheritanceApplication
{
public interface Shape
{
/* double radius;
double side;*/
}
public interface shape2d:Shape
{
double get2Darea( double val);
}
public interface shape3d:Shape
{
double get3Darea( double val);
double get3Dvolume( double val);
}
class Circle: shape2d
{
public double get2Darea( double val)
{
return (3.141*val*val);
}
}
class Square:shape2d
{
public double get2Darea( double val)
{
return (val*val);
}
}
class Sphere:shape3d
{
public double get3Darea( double val)
{
return (4*3.141*val*val);
}
public double get3Dvolume( double val)
{
return ((4/3)*3.141*val*val*val);
}
}
class Cube:shape3d
{
public double get3Darea( double val)
{
return (4*val);
}
public double get3Dvolume( double val)
{
return (val*val*val);
}
}
class ShapeTester
{
public static void Main()
{
Circle c=new Circle();
Square s=new Square();
Sphere sp=new Sphere();
Cube cu=new Cube();
Console.WriteLine("2D shape:");
Console.WriteLine("Area of circle:",c.get2Darea(2));
Console.WriteLine("Area of square:",s.get2Darea(2));
Console.WriteLine("3D shape:");
Console.WriteLine("Area of sphere:",sp.get3Darea(2));
Console.WriteLine("Volume of sphere:",sp.get3Dvolume(2));
Console.WriteLine("Area of cube:",cu.get3Darea(2));
Console.WriteLine("Volume of cube:",cu.get3Dvolume(2));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.