设计一个名为Rectangle的类表示矩形。这个类包括:
- 两个名为width和height的double类型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
- 一个用于创建默认矩形的无参构造方法。
- 一个创建指定width和height值的矩形的构造方法。
- 一个名为getArea()的方法,返回该矩形的面积。
- 一个名为getPerimeter()的方法,返回周长。
编写一个测试程序,分别输入两个矩形的高和宽,创建两个Rectangle对象。按照顺序显示每个矩形的宽、高、面积和周长。
输入格式:在一行内输入4个数据,依次为两个矩形的高和宽
输出格式:每行输出一个矩形的宽、高、面积和周长,中间用空格隔开
输入样例:在这里给出一组输入。例如:4 40 3.5 35.9
输出样例:在这里给出相应的输出。例如:
4.0 40.0 160.0 88.0
3.5 35.9 125.64999999999999 78.8
using System;
namespace _6._15
{
class Program
{
static void Main(string[] args)
{
string arr = Console.ReadLine();
string[] arr1 = arr.Split(' ');
double[] arr2 = new double[4];
int i = 0;
foreach (var item in arr1)
{
arr2[i] = Convert.ToDouble(item);
i++;
}
Rectangle a = new Rectangle(arr2[0], arr2[1]);
Rectangle b = new Rectangle(arr2[2], arr2[3]);
Console.WriteLine("{0:f1} {1:f1} {2:f1} {3:f1}", arr2[0], arr2[1], a.getArea(), a.getPerimeter());
Console.WriteLine("{0:0.0} {1:0.0} {2:0.0} {3:0.0}", arr2[2], arr2[3], b.getArea(), b.getPerimeter());
}
}
class Rectangle
{
private double width = 1;
private double height = 1;
public Rectangle()
{ }
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return width * 2 + height * 2;
}
}
}