Java实验⼀:编写Rectangle(矩形)类......
编写Rectangle(矩形)类.该类具有double类型的私有实例变量length(长)和width(宽),它们默认值都是0.0,提供带有2个参数的构造函数,以便在创建该类的对象时对其进⾏初始化;提供将实例变量设置为默认值的⽆参构造函数以便创建时可以不带初始值。该类有length和width的设置/读取⽅法,设置⽅法应验
证length和width都是正数,否则设置发法将这些变量设置为它们的默认值。该类还有计算矩形周长(perimeter)和⾯积(area)的⽅法以及toString()⽅
法,toString()⽅法返回包含矩形的长、宽、周长、⾯积的字符串。
import java.util.Scanner;
public class Rectangle{
private static double length;
private static double width;
static void t_width(double width2) {adventurous
// TODO Auto-generated method stub
if(width2<0)
width=0;
el width = width2;
}
static void t_length(double length2) {
// TODO Auto-generated method stub
if(length2<0)
length=0;
el length = length2;
}
static double get_length(){
return length;
在职硕士报名
}
static double get_width(){
return width;
}
static double perimeter(){pickle
return (length+width)*2;
}
static double area(){
return length*width;
}
hrs
public static String tostring(){
//return null;
braceletString s = "";
s = s + " length="+get_length();
s = s + " width="+get_width();
雅虎在线翻译equations = s + " perimeter="+perimeter();
s = s + " area="+area();
return s;
}
public static void Rectangle(double d, double e) {
/
/ TODO Auto-generated method stub
t_length(d);
t_width(e);
宫东风
}
public static void main(String[]args){
Scanner input= new Scanner(System.in);
System.out.print("input double type length and width: ");
double length = Double();
double width = Double();
brazzaville
Rectangle(length,width);
System.out.print(tostring());五月英文
}
}