In: Computer Science
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize the rectangle. Calculate and display the area of the rectangle by first initialize an object named myRectangle and then calculate its area using the CalArea() method.
Program
using System;
class Rectangle{ //Rectangle class
//member variables
double length;
double width;
public Rectangle(double len,double wid){ //constructor
length=len;
width=wid;
}
public double CalArea(){ //method CalArea()
return length*width;
}
}
class RectangleArea {
static void Main() {
Rectangle myRectangle=new Rectangle(15.8,7.9); //object of
Rectangle type
Console.WriteLine("The area of rectangle is :
"+myRectangle.CalArea()); //invoking CalArea() method
}
}
Screenshot of program
Output