using System;
interface Shape{
double Area{get;}
string Desc{get;}
};
interface TwoDimensionalShape : Shape {}
interface ThreeDimensionalShape : Shape {
double Volume{get;}
}
class Circle: TwoDimensionalShape{
private double Radius;
public Circle(double r){Radius = r;}
public double Area{ get{ return ((Radius * Radius) * Math.PI); }}
public string Desc{get{return "A circle";}}
}
class Square: TwoDimensionalShape{
private double Side;
public Square(double s){Side = s;}
public double Area{ get{ return (Side * Side); }}
public string Desc{get{return "A square";}}
}
class Sphere: ThreeDimensionalShape{
private double Radius;
public Sphere(double r){Radius = r;}
public double Area{ get{ return (4 * Math.Pow(Radius,2) * Math.PI); }}
public double Volume{ get{ return (4/3.0 * Math.Pow(Radius,3) * Math.PI); }}
public string Desc{get{return "A sphere";}}
}
class Cube: ThreeDimensionalShape{
private double Side;
public Cube(double s){Side = s;}
public double Area{ get{ return (6*(Side * Side)); }}
public double Volume{ get{ return Math.Pow(Side,3);}}
public string Desc{get{return "A cube";}}
}
class TestShapes{
public static void Main(string[]args){
Shape[]shapes={new Circle(3.5), new Cube(6.2), new Sphere(4.7), new Square(7.8)};
ThreeDimensionalShape s3;
foreach(Shape s in shapes){
Console.WriteLine("Shape is: {0}", s.Desc);
if(s is TwoDimensionalShape){
Console.WriteLine("Area is: {0:0.00}", (s as TwoDimensionalShape).Area);
continue;
}
if(s is ThreeDimensionalShape){
s3 = s as ThreeDimensionalShape;
Console.WriteLine("Area is: {0:0.00}", s3.Area);
Console.WriteLine("Volume is: {0:0.00}", s3.Volume);
}
}
}
}
Download
Comments
Post a Comment