设计一个矩形类Rectangle,包含私有的数据成员宽度(Width)和高度(Height);公有的方法double getArea()返回矩形的面积,公有的方法double getPerimeter()返回矩形的周长。
例如:
测试 | Result |
---|---|
Rectangle rect=new Rectangle(8,5); System.out.printf("The Area is %.2f,Perimeter is %.2f\n", rect.getArea(),rect.getPerimeter()); | The Area is 40.00,Perimeter is 26.00 |
class Rectangle{private double Width;private double Height;public Rectangle(double width, double height) {Width = width;Height = height;}public double getArea() {return Width * Height;}public double getPerimeter(){return (Width + Height) * 2;}
}
public class Test{public static void main(String[] args) {Rectangle rect=new Rectangle(8,5);System.out.printf("The Area is %.2f,Perimeter is %.2f\n", rect.getArea(),rect.getPerimeter());}
}