在JAVA Eclipse环境中,定义创建一个Rectabgle类,包括两个属性,weight和h
答案:2 悬赏:40 手机版
解决时间 2021-01-26 10:58
- 提问者网友:我是我
- 2021-01-26 00:03
在JAVA Eclipse环境中,定义创建一个Rectabgle类,包括两个属性,weight和height,两个方法:计算矩形的周长和面积。这道题怎么编写?能帮忙编一下吗?
最佳答案
- 五星知识达人网友:由着我着迷
- 2021-01-26 01:09
class Rectangle{
private double weight;
private double height;
public Rectangle(double weight,double height) {
this.weight=weight;
this.height=height;
}
public double perimeter(){
return (weight+height)*2;
}
public double area(){
return weight*height;
}
}
public class Test {
public static void main(String[] args) {
Rectangle rect=new Rectangle(3, 4);
System.out.println("周长:"+rect.perimeter());
System.out.println("面积:"+rect.area());
}
}
private double weight;
private double height;
public Rectangle(double weight,double height) {
this.weight=weight;
this.height=height;
}
public double perimeter(){
return (weight+height)*2;
}
public double area(){
return weight*height;
}
}
public class Test {
public static void main(String[] args) {
Rectangle rect=new Rectangle(3, 4);
System.out.println("周长:"+rect.perimeter());
System.out.println("面积:"+rect.area());
}
}
全部回答
- 1楼网友:迷人又混蛋
- 2021-01-26 01:49
public class Rectabgle{
private double width;
private double height;
public Rectabgle(double width, double height){
this.width = width;
this.height= width;
}
public double getArea(){
return width * height;
}
public double getGirth(){
return 2 * ( width + height );
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯