7. Write a program to print the area of a rectangle by creating a class named 'Area' taking the values of its length and breadth as parameters of its constructor and having a function named 'returnArea' which returns the area of the rectangle. Length and breadth of the rectangle are entered through keyboard c#
using System;
class Area
{
double length;
double breadth;
public Area(double l, double d)
{
length = l;
breadth = d;
}
public double ReturnArea()
{
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(length, breadth);
Console.WriteLine($"Area of the rectangle: {area.ReturnArea()}");
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message} Try again!");
}
}
}
Comments
Leave a comment