namespace ShapeLibrary_W_Interface
{
public interface IShape
{
//An interface must not have fields
double Perimeter();
double Area();
double Volume();
}
}
=================================================================
namespace ShapeLibrary_W_Interface
{
public class Circle : IShape
{
///An interface is like a contract or a pure abstract class
///All the methods / properties must be overriden, or we prefer
///to use the term implement.
///That is the Circle (shape class) MUST IMPLEMENT all the methods
///and properties defined in an interface
///However, because this is the only behavior, you must not
///add the keyword override, it is implied.
///
/// To implement interface
/// R-Click on iterface name IShape --> Choose "Implement Interface" --> Implement Interface Explicitly
///
internal protected double _radius;
public Circle(double radius)
{
this._radius = radius;
}
public virtual double Radius
{ get { return this._radius; } }
public virtual double Perimeter()
{
return 2 * Math.PI * _radius;
//throw new NotImplementedException();
}
public virtual double Area()
{
return Math.PI * _radius * _radius;
//throw new NotImplementedException();
}
public virtual double Volume()
{
return 0;
//throw new NotImplementedException();
}
public override string ToString()
{
return String.Format("Type: {0}, radius: {1:f}, perimeter: {2:f} area: {3:f} \n\n", GetType().Name,
_radius, Perimeter(), Area());
//return base.ToString();
}
}
}
=================================================================
namespace ShapeLibrary_W_Interface
{
public class Sphere : Circle
{
public Sphere(double radius) : base(radius) { }
public override double Area()
{
return 4 * Math.PI * _radius * _radius;
}
public override double Volume()
{
return 4D / 3D * Math.PI * Math.Pow(_radius, 3);
}
public override string ToString()
{
return String.Format("Type: {0} Radius: {1:f} Area : {2:f} Volume: {3:f} \n\n",
GetType().Name, Radius, Area(), Volume());
}
}
}
=================================================================
namespace ShapeLibrary_W_Interface
{
public class Rect : IShape
{
internal protected double _length;
internal protected double _width;
//Constructor
public Rect(double length, double width)
{
this._length = length;
this._width = width;
}
//Property
public virtual double Length
{ get { return this._length; } }
public virtual double Width
{ get { return this._width; } }
//Methods
public virtual double Perimeter()
{
return 2 * (_length + _width);
}
public virtual double Area()
{
return _length * _width;
}
public virtual double Volume()
{
return 0;
}
public override string ToString()
{
return String.Format("Type: {0} Length: {1:f} Width: {2:f} Area : {3:f} Peri: {4:f} \n\n",
GetType().Name, Length, Width, Area(), Perimeter());
}
}
}
=================================================================
namespace ShapeLibrary_W_Interface
{
public class RectBox : Rect
{
internal protected double _height;
public RectBox(double length, double width, double height):base (length, width)
{
this._height = height;
}
public virtual double Height
{ get { return this._height; } }
public override double Perimeter()
{
// P = 4L+4W+4H
return (4 * Length) + (4 * Width) + (4 * Height);
}
public override double Area()
{
return 2 * (Length * Height) + 2 * (Length * Width) + 2 * (Width * Height);
}
public override double Volume()
{
return Length * Width * Height;
}
public override string ToString()
{
return String.Format("Type: {0} Length: {1:f} Width: {2:f} Height: {3:f} Area : {4:f} Volume: {5:f} \n\n",
GetType().Name, Length, Width, Height, Area(), Volume());
}
}
}
================================================================
namespace ShapeLibrary_W_Interface
{
public class RectPyramid : RectBox
{
public RectPyramid(double length, double width, double height): base(length, width, height)
{
}
public override double Area()
{
return Length * Width + (Length * Math.Sqrt (Math.Pow((Width/2), 2) +
Math.Pow (Height, 2)) + Width * Math.Sqrt (Math.Pow((Length/2), 2) + Math.Pow(Height, 2)));
}
public override double Volume()
{
return 1f / 3f * (Length * Width) * Height;
}
public override string ToString()
{
return String.Format("Type: {0} Length: {1:f} Width: {2:f} Height: {3:f} Area : {4:f} Volume: {5:f} \n\n",
GetType().Name, Length, Width, Height, Area(), Volume());
}
}
}
***************************************************************************
using System.Windows.Forms;
using ShapeLibrary_W_Interface; //Write(add) after adding reference
namespace IntroductionToInterfaces
{
/*What is an interface
* ===================
* Note:- interface is used extensively in .Net
* An interface is like a "pure abstract class"
* ------------------------------------------------------------------
* Reminder: An abstract class is any class that contains at least one abstract method
* or abstract property.
* In addition such class must be defined with the keyword 'abstract'
*
* What is an abstract method or property?
* ======================================
* is a method that does not have a body (or implementation), in addition
* you must defined abstract keyword
* --------------------------------------------------------------------
* What is "Pure Abstract Class"?
* ... is a class where all the methods and properties are abstract
* *
* You can think of an interface as a pure abstract class
* all the methods and properties have no implementations
* so an interface contains only the signatures of methods and properties.
*
* Like an abstract class, an interface provides a way to achieve runtime polymorphism.
* You can define a method that takes a parameter of type interface.
* Such methods can be called by passing it any object of a class that inherits
* from that interface
*
* We usually say that a class inherits from another class
* but we say that a class implements an interface.
*
* An abstract method defined inside of an abstract class must be defined with
* the keyword abstract.
* But a method defined inside of an interface is by default abstract.
* Therefore, you do not use the keyword abstract for method inside it.
* In addition a method or property defined in an interface is by default public
* So you do not add the keyword "public" to it.
*/
public partial class Form1 : Form
{
List<IShape> shapeList = new List<IShape>();
public Form1()
{
InitializeComponent();
}
Random rand = new Random();
private void btnCreateNSaveCircle_Click(object sender, EventArgs e)
{
double radius = rand.Next(1, 10) + rand.NextDouble();
Circle c = new Circle(radius);
shapeList.Add(c);
DisplayAllShapes(c);
}
//public void DisplayAllShapes()
//{
// richTextBox1.Clear();
// foreach (IShape s in shapeList)
// {
// //display perimeter,area, and volume
// richTextBox1.AppendText(
// String.Format("perimeter: {0:f} area: {1:f} volume: {2:f} type: {3}\n",
// s.Perimeter(), s.Area(), s.Volume(), s.GetType().Name));
// }
//}
private void DisplayAllShapes(IShape shape)
{
richTextBox1.AppendText(shape.ToString() + "\n" );
}
private void btnCreateandSaveSphere_Click(object sender, EventArgs e)
{
double radius = rand.Next(1, 10) + rand.NextDouble();
Sphere sp = new Sphere(radius);
shapeList.Add(sp);
DisplayAllShapes(sp);
}
private void btnCreateRectangle_Click(object sender, EventArgs e)
{
double length = rand.Next(1, 10) + rand.NextDouble();
double width = rand.Next(1, 10) + rand.NextDouble();
Rect rect = new Rect(length, width);
shapeList.Add(rect);
DisplayAllShapes(rect);
}
private void btnCreateRectbox_Click(object sender, EventArgs e)
{
double l = rand.Next(1, 10) + rand.NextDouble();
double w = rand.Next(1, 10) + rand.NextDouble();
double h = rand.Next(1, 10) + rand.NextDouble();
RectBox rb = new RectBox(l, w, h);
shapeList.Add(rb);
DisplayAllShapes(rb);
}
private void btnCreateRectPyramid_Click(object sender, EventArgs e)
{
double l = rand.Next(1, 10) + rand.NextDouble();
double w = rand.Next(1, 10) + rand.NextDouble();
double h = rand.Next(1, 10) + rand.NextDouble();
RectPyramid rp = new RectPyramid(l, w, h);
shapeList.Add(rp);
DisplayAllShapes(rp);
}
}
}
//Lab
///Add a rectangle that inherits from IShape
///Add a RectBox that inherits from Rectangle
///Add a RectPyramid that inherits from RectBox
///Form1: Add buttons to create each shape
///Read more on interfaces

No comments:
Post a Comment