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

2024-11-09 10:26:16
推荐回答(3个)
回答(1):

等楼下的给你解答吧

居然没人回答你,看下面


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;
    }
}

回答(2):

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());

}
}

回答(3):

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());
}
}