1.根据提示,填空...public class Rectangle{
private double length;
private double width;
public Rectangle()
{ System.out.println(“无参数的构造函数”); } public Rectangle(double len, double wid)
{ System.out.println(“有参数的构造函数”);
setValue(len, wid);
} public void setValue(double len, double wid)
{ if(len>=0)
length=len;
else
{ System.out.println(“不合法的长度值,系统将使用默认值0.0”);
length=0.0;
} if(wid>=0)
width=wid;
else
{ System.out.println(“不合法的宽度值,系统将使用默认值0.0”);
width=0.0;
}
} public String toString()
{ return “长方形: 宽=”+length+”,高=”+width; } public static void main(String args[])
{
//创建一个Rectangle类的对象r1,使用无参构造函数
(1)_____________________________________
//创建一个Rectangle类的对象r2,使用有参数的构造函数,长度是4.5,宽度是3
(2)_____________________________________
//设置r1的长度和宽度,长度是3,宽度是-4
(3)_____________________________________
//显示r1的属性值
(4)_____________________________________
//显示r2的属性值
(5)_____________________________________
}
}
private double length;
private double width;
public Rectangle()
{ System.out.println(“无参数的构造函数”); } public Rectangle(double len, double wid)
{ System.out.println(“有参数的构造函数”);
setValue(len, wid);
} public void setValue(double len, double wid)
{ if(len>=0)
length=len;
else
{ System.out.println(“不合法的长度值,系统将使用默认值0.0”);
length=0.0;
} if(wid>=0)
width=wid;
else
{ System.out.println(“不合法的宽度值,系统将使用默认值0.0”);
width=0.0;
}
} public String toString()
{ return “长方形: 宽=”+length+”,高=”+width; } public static void main(String args[])
{
//创建一个Rectangle类的对象r1,使用无参构造函数
(1)_____________________________________
//创建一个Rectangle类的对象r2,使用有参数的构造函数,长度是4.5,宽度是3
(2)_____________________________________
//设置r1的长度和宽度,长度是3,宽度是-4
(3)_____________________________________
//显示r1的属性值
(4)_____________________________________
//显示r2的属性值
(5)_____________________________________
}
}
Rectangle r1 = new Rectangle();
//创建一个Rectangle类的对象r2,使用有参数的构造函数,长度是4.5,宽度是3
Rectangle r2 = new Rectangle(4.5,3);
//设置r1的长度和宽度,长度是3,宽度是-4
r1.setValue(3, -4);
//显示r1的属性值
System.out.println(r1.toString());
//显示r2的属性值
System.out.println(r2.toString());
public class Circle{
private float radius; public Circle ()
{ System.out.println(“无参数的构造函数”); } public Circle (float rad)
{ System.out.println(“有参数的构造函数”);
setRadius(rad);
} public void setRadius(float rad)
{ if(rad>=0)
radius=rad;
else
{
System.out.println(“不合法的半径值,系统将使用默认值0.0”);
radius=0.0;
}
} public String toString()
{ return “圆形: 半径=”+radius; } public static void main(String args[])
{
//创建一个Circle类的对象c1,使用无参构造函数
//创建一个Circle类的对象c2,使用有参数的构造函数,半径是3.7
//设置c1的半径值为-4
//显示c1的属性值
//显示c2的属性值
}
}
private double length;
private double width;
public Rectangle(){
System.out.println("无参数的构造函数");
} public Rectangle(double len, double wid){
System.out.println("有参数的构造函数");
setValue(len, wid);
} public void setValue(double len, double wid){
if(len>=0)
length=len;
else{
System.out.println("不合法的长度值,系统将使用默认值0.0");
length=0.0;
} if(wid>=0)
width=wid;
else{
System.out.println("不合法的宽度值,系统将使用默认值0.0");
width=0.0;
}
} public String toString(){
return "长方形: 宽="+length+",高="+width;
} public static void main(String args[]){
//创建一个Rectangle类的对象r1,使用无参构造函数
Rectangle r1 = new Rectangle();
//创建一个Rectangle类的对象r2,使用有参数的构造函数,长度是4.5,宽度是3
Rectangle r2 = new Rectangle(4.5,3);
//设置r1的长度和宽度,长度是3,宽度是-4
r1.setValue(3, -4);
//显示r1的属性值
System.out.println(r1.toString());
//显示r2的属性值
System.out.println(r2.toString());
}
}
private double radius; public Circle ()
{ System.out.println("无参数的构造函数"); } public Circle (double rad)
{ System.out.println("有参数的构造函数");
setRadius(rad);
} public void setRadius(double rad)
{ if(rad>=0)
radius=rad;
else
{
System.out.println("不合法的半径值,系统将使用默认值0.0");
radius=0.0;
}
} public String toString()
{ return "圆形: 半径="+radius; } public static void main(String args[])
{
//创建一个Circle类的对象c1,使用无参构造函数
Circle c1=new Circle();
//Rectangle r1 = new Rectangle();
//创建一个Circle类的对象c2,使用有参数的构造函数,半径是3.7
Circle c2=new Circle(3.7);
// Rectangle r2 = new Rectangle(4.5,3);
//设置c1的半径值为-4
c1.setRadius(-4);
//显示c1的属性值
System.out.println(c1.toString());
//显示c2的属性值
System.out.println(c2.toString());
}
}
那个损失精度是因为小数默认是double 你返回的是float
这里我在小数后面加了f将它强制转换成float的public class Circle{
private float radius; public Circle (){
System.out.println("无参数的构造函数");
} public Circle (float rad){
System.out.println("有参数的构造函数");
setRadius(rad);
} public void setRadius(float rad){
if(rad>=0)
radius=rad;
else{
System.out.println("不合法的半径值,系统将使用默认值0.0");
radius=0.0f;
}
} public String toString(){
return "圆形: 半径="+radius;
} public static void main(String args[])
{
//创建一个Circle类的对象c1,使用无参构造函数
Circle c1 = new Circle();
//创建一个Circle类的对象c2,使用有参数的构造函数,半径是3.7
Circle c2 = new Circle(3.7f);
//设置c1的半径值为-4
c1.setRadius(-4);
//显示c1的属性值
System.out.println(c1.toString());
//显示c2的属性值
System.out.println(c2.toString());
}
}