public class Rectangle { private double width; private double height;

public Rectangle() {
    this.width = 1;
    this.height = 1;
}

public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
}

public double getArea() {
    return width * height;
}

public double getPerimeter() {
    return 2 * (width + height);
}

}

public class EmployeeDemo { public static void main(String[] args) { Rectangle rectangle1 = new Rectangle(); Rectangle rectangle2 = new Rectangle(5, 10);

    System.out.println('Rectangle 1:');
    System.out.println('Width: ' + rectangle1.getWidth());
    System.out.println('Height: ' + rectangle1.getHeight());
    System.out.println('Area: ' + rectangle1.getArea());
    System.out.println('Perimeter: ' + rectangle1.getPerimeter());

    System.out.println('Rectangle 2:');
    System.out.println('Width: ' + rectangle2.getWidth());
    System.out.println('Height: ' + rectangle2.getHeight());
    System.out.println('Area: ' + rectangle2.getArea());
    System.out.println('Perimeter: ' + rectangle2.getPerimeter());
}

}

Java 矩形类实现:计算面积和周长

原文地址: http://www.cveoy.top/t/topic/bein 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录