求用Java编一个小程序

来源:百度知道 编辑:UC知道 时间:2024/05/04 11:17:17
设计一个Retangle类,提供通过length和width字段计算面积和周长的2个方法。并用main方法来测试程序。

class Retangle{

static public double countArea(double d1,double d2){
return d1*d2;
}
static public double countgirth(double d1,double d2){
return 2*(d1+d2);
}
使用static方法,不需要创建实例

class Retangle{
private double length;
private double width;
//构造函数
Retangle(double length,double width){
this.length = length;
this.width = width;
}
//方法
public double getArea(){
return length*width;
}
public double getGirth(){
return 2*(length+width);
}
public static void main(String[] args){
Rectangle rec = new Rectangle(3,4);
System.out.println("面积为:"+rec.getArea());
System.out.println("周长为:"+rec.getArea());
}
}

public void luqiang(){
public void Retangle(double length, double width){
double mianji,zhouchang;
mianji=length*width;
System.out.println("面积:"+mianji);
zhouchang=(length+width)*2;