In: Computer Science
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RectangleAreaPerimeter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs
e)
{
double length, width;
length = Convert.ToDouble(txtLength.Text);
width = Convert.ToDouble(txtWidth.Text);
Rectangle rect = new Rectangle(length,width );
//calling getArea() function and store its value into txtArea
TextBox.
txtArea.Text = rect.getArea().ToString();
//calling getPerimeter() function and store its value into
txtPerimeter TextBox.
txtPerimeter.Text = rect.getPerimeter().ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
//read only txtArea and txtPerimeter
txtArea.Enabled = false;
txtPerimeter.Enabled = false;
}
}
}
Rectangle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RectangleAreaPerimeter
{
class Rectangle
{
double length, width;
public Rectangle()
{
length = 1;
width = 1;
}
public Rectangle(double len, double wid)
{
setLength(len);
setWidth(wid);
}
public void setLength(double len)
{
if (len >= 0.0 && len <= 20.0)
{
length = len;
}
else
{
//as the length is not in the range so assign 0
length = 0;
}
}
public void setWidth(double wid)
{
if (wid >= 0.0 && wid <= 20.0)
{
width = wid;
}
else
{
//as the width is not in the range so assign 0
width = 1;
}
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return (getLength() * getWidth());
}
public double getPerimeter()
{
return (2*getLength()+2*getWidth() );
}
}
}
Output: