In C The following is the definition of an interface called
In C#
The following is the definition of an interface called GeometricObject:
Any class that implements the GeometricObject interface must include an implementation of its GetArea()and GetPerimeter() methods for that type of object. Complete the following class named Hexagon that implements GeometricObject. Complete the class by filling in the code that goes where the \"// Implement it\" comments are found. Assume that all sides of the hexagon are of equal size. And also, the formula for the area of a hexagon is given by the following
area 1.5 *N3 side sideSolution
using System.IO;
using System;
interface GeometricObject
{
// abstract method for getting the area of the object
double GetArea();
// abstract method for getting the perimeter of the object
double GetPerimeter();
}
public class Hexagon : GeometricObject
{
private double side; // just a variable, there are no properties
/* Constructor of the Hexagon class */
public Hexagon(double hexSide) {
// Implement it
side=hexSide;
}
/* Implements the abstract method getArea */
public double GetArea() {
// Implement it
return 1.5*Math.Sqrt(3)*side*side;
}
/* Implements the abstract method getPerimeter */
public double GetPerimeter() {
// Implement it
return 6*side;
}
/* Returns a string representation of the Hexagon object */
public override string ToString() {
// Implement it by returning a string that shows the length
// of the side, along with the area and the perimeter,
// and put each value on a separate line with an
// appropriate label in front of each value, and show
// only 2 places after the decimal
// usinf ToString(#.##) to return 2 decimal point
return String.Format(\"Side: {0} \ Area:{1} \ Perimeter:{2}\",side, GetArea().ToString(\"#.##\"), GetPerimeter().ToString (\"#.##\"));
}
}
// Main program to test the above
class Program
{
static void Main()
{
double val;// variable to be set by the user
val=double.Parse(Console.ReadLine());// reading from the console
Hexagon hex= new Hexagon(val);// calling the constructor to set the value;
Console.WriteLine(hex.ToString());// printing the overriden ToString()
}
}
--------------------output---------------------
sh-4.3$ mono main.exe
5
Side: 5
Area:64.95
Perimeter:30
------------------output ends----------------------
Note: Feel free to ask doubts/questions. Gode bless you!!

