题目:
按以下要求编写程序
(1) 创建一个Rectangle 类,添加width 和height 两个成员变量。
(2) 在Rectangle 中添加两种方法分别计算矩形的周长和面积。
(3) 编程利用Rectangle 输出一个矩形的周长和面积。
(1) 创建一个Rectangle 类,添加width 和height 两个成员变量。
(2) 在Rectangle 中添加两种方法分别计算矩形的周长和面积。
(3) 编程利用Rectangle 输出一个矩形的周长和面积。
package com.task01;
//矩形类
public class Rectangle {
private int width;
private int height;
public static void main(String[] args) {
//声明类时传入宽高
Rectangle rec = new Rectangle(200,100);
System.out.println("该矩形的周长是:"+rec.getPerimeter());
System.out.println("该矩形的面积是"+rec.getArea());
}
public int getPerimeter(){
return this.width *2 + this.height *2;
}
//获得周长
public int getArea(){
return this.width * this.height;
}
//面积
public Rectangle(int width,int height) {
super();
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
//矩形类
public class Rectangle {
private int width;
private int height;
public static void main(String[] args) {
//声明类时传入宽高
Rectangle rec = new Rectangle(200,100);
System.out.println("该矩形的周长是:"+rec.getPerimeter());
System.out.println("该矩形的面积是"+rec.getArea());
}
public int getPerimeter(){
return this.width *2 + this.height *2;
}
//获得周长
public int getArea(){
return this.width * this.height;
}
//面积
public Rectangle(int width,int height) {
super();
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}