In: Computer Science
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns the area. 5. Method getPerimeter() that returns the perimeter. 6. Method getHeight() that returns the height. 7. Method getWidth() that returns the width. Now design and implement a test program to create two rectangle objects: one with default height and width, and the second is 5 units high and 6 units wide. Next, test the class methods on each object to print the information as shown below. Sample run: First object: Height: 1 unit Width: 1 unit Area: 1 unit Perimeter: 4 units Second object: Height: 5 unit Width: 6 unit Area: 30 units Perimeter: 22 units
If you have any doubts, please give me comment...
using System;
namespace Rectangle{
public class Rectangle{
private double height;
private double width;
public Rectangle(){
height = 1.0;
width = 1.0;
}
public Rectangle(double h, double w){
height = h;
width = w;
}
public double getArea(){
return height*width;
}
public double getPerimeter(){
return 2*(height+width);
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
}
public class RectangleDemo{
static void Main(){
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle(5, 6);
Console.WriteLine("First object:");
Console.WriteLine("Height {0} unit", rect1.getHeight());
Console.WriteLine("Weight {0} unit", rect1.getWidth());
Console.WriteLine("Area: {0} unit", rect1.getArea());
Console.WriteLine("Perimeter: {0} units", rect1.getPerimeter());
Console.WriteLine("\nSecond object:");
Console.WriteLine("Height {0} unit", rect2.getHeight());
Console.WriteLine("Weight {0} unit", rect2.getWidth());
Console.WriteLine("Area: {0} units", rect2.getArea());
Console.WriteLine("Perimeter: {0} units", rect2.getPerimeter());
}
}
}