编写Rectangle(矩形)类.该类具有double类型的私有实例变量length(长)和width(宽),它们默认值都是0.0,提供带有2个参数的构造函数,以便在创建该类的对象时对其进行初始化;提供将实例变量设置为默认值的无参构造函数以便创建时可以不带初始值。该类有length和width的设置/读取方法,设置方法应验证length和width都是正数,否则设置发法将这些变量设置为它们的默认值。该类还有计算矩形周长(perimeter)和面积(area)的方法以及toString()方法,toString()方法返回包含矩形的长、宽、周长、面积的字符串。
import java.util.Scanner;
public classRectangle{
private static double length;
private static double width;
static void set_width(double width2) {
// TODO Auto-generated method stub
if(width2<0)
width=0;
else width = width2;
}
static void set_length(double length2) {
// TODO Auto-generated method stub
if(length2<0)
length=0;
else length = length2;
}
static double get_length(){
return length;
}
static double get_width(){
return width;
}
static double perimeter(){
return (length+width)*2;
}
static doublearea(){
return length*width;
}
public static String tostring(){
//return null;
Strings = "";
s = s + " length="+get_length();
s = s + " width="+get_width();
s = s + " perimeter="+perimeter();
s = s + " area="+area();
return s;
}
public static void Rectangle(double d, double e) {
// TODO Auto-generated method stub
set_length(d);
set_width(e);
}
public static void main(String[]args){
Scannerinput= newScanner(System.in);
System.out.print("input double type length and width: ");
double length = input.nextDouble();
double width = input.nextDouble();
Rectangle(length,width);
System.out.print(tostring());
}
}