java,定义一个长方形类
java,定义一个长方形类
定义一个长方形类,该类具有长和宽两种属性,并具有相应的构造方法、属性访问方法和计算周长和面积的方法,要求输出长是5宽是4的长方形的面积和周长.
public class Test {
\x05public static void main(String[] args) {
\x05\x05Rectangle r = new Rectangle(5,4);
\x05\x05System.out.println("Circle for rectangle is:" + r.circle());
\x05\x05System.out.println("Area for rectangle is:" + r.area());
\x05}
}
class Rectangle{
\x05private double length;
\x05private double width;
\x05
\x05public Rectangle(double length,double width){
\x05\x05this.length = length;
\x05\x05this.width = width;
\x05}
\x05public double getLength() {
\x05\x05return length;
\x05}
\x05public double getWidth() {
\x05\x05return width;
\x05}
\x05public void setLength(double length) {
\x05\x05this.length = length;
\x05}
\x05public void setWidth(double width) {
\x05\x05this.width = width;
\x05}
\x05
\x05public double circle(){
\x05\x05return 2 * (width + length);
\x05}
\x05
\x05public double area(){
\x05\x05return width * length;
\x05}
}
------------------------
Circle for rectangle is:18.0
Area for rectangle is:20.0