java题目代码? 200
1个回答
展开全部
代码如下,Rectangle类在java.lang包下有同名类,如需引包请注意,望采纳。
补充:
Rectangle类
package com.baidu.question183173554138589044;
public class Rectangle {
private double width;
private double height;
public void setRectangle(int w, int h){
if(w>0 && h>0){
width = w;
height = h;
}else{
System.out.println("错误:长宽需要大于0");
}
}
public double getArea() {
return width*height;
}
}
运行主类
package com.baidu.question183173554138589044;
public class Main {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setRectangle(5,6);
System.out.println(rect.getArea());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询