一道简单的java编程题,各位大神拜托了!

Write a class named Rectangle to represent rectangles.it has member attributes width:int and height:int.please provide the setter and getter of those members, and design a method named area to calculate its area.
编写一个类名为矩形代表rectangles.it的成员属性width:int和高度:int.please提供setter和getter那些成员,设计名为面积的方法来计算其面积。

新手,所以请大神们用简单点的方式解答,多谢!

等楼下的给你解答吧

居然没人回答你,看下面


public class Rectangles{
    private int width;    // 宽度
    private int height;    // 长度
    public void setWidth(int width){
        this.width = width;
    }
    public int getWidth(){
        return width;
    }
    public void setHeight(int height){
        this.height = height;
    }
    public int getHeight(){
        return height;
    }
    public int getSquare(){
        return width * height;
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-06
package t90;

public class Rectangle {
static int hight;
static int width;

public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}

public int getArea (){
if(hight <= 0 || width <= 0){
System.out.println("你设置的长宽不符合条件");
return 0;
}else{
return hight*width;
}

}

}

测试类

package t90;

public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.setHight(-1);
r.setWidth(10);
System.out.println(r.getArea());

}
}
第2个回答  2013-09-06
public class Rectangle {

int width;
int 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 String area(){
return "area is "+ width*height;
}
public static void main(String[] args) {
Rectangle r=new Rectangle();
r.setWidth(5);
r.setHeight(4);
System.out.println(r.area());
}
}本回答被网友采纳
相似回答