java面向对象程序设计 1.按以下要求编写程序(1) 创建一个Rectangle类,添加width和height两个成员变量(2).
class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return (width + height) * 2;
}
}
public class App {
public static void main(String[] args) {
Rectangle rect = new Rectangle(50, 20);
System.out.println("矩形,面积:" + rect.getArea() + ",周长:" + rect.getPerimeter());
}
}
float width, height;
public Rectangle(float width, float height) {
this.width = width;
this.height = height;
}
public float getLength______{
return (this.width + this.height) * B;
}
public float getArea______{
return this.width * this.height;
}
public static void main(String [] args) {
Rectangle rect = new Rectangle(A0, B0);
System.out.println("周长是:" + rect.getLength______);
System.out.println("面积是:" + rect.getArea______);
}
}