Write a program to print the area of a rectangle by creating a class named 'Area' having two functions. First function named as 'setDim' takes the length and breadth of the rectangle as parameters and the second function named as 'getArea' returns the area of the rectangle. Length and breadth of the rectangle are entered through keyboard.(For C sharp)
using System;
class Area
{
double length;
double breadth;
public void SetDim(double l, double d)
{
this.length = l;
this.breadth = d;
}
public double GetArea()
{
return length * breadth;
}
}
class Program
{
static void Main(String[] args)
{
try
{
Console.Write("Length: ");
double length = Double.Parse(Console.ReadLine());
Console.Write("Breadth: ");
double breadth = Double.Parse(Console.ReadLine());
Area area = new Area();
area.SetDim(length, breadth);
Console.WriteLine($"Area of the rectangle: {area.GetArea()}");
}
catch(Exception ex)
{
Console.WriteLine("Try again!");
}
}
}
Comments
Leave a comment