创建Rectangle 的类用java
创建一个Rectangle类。该类拥有属性lenghth和width,每个属性的默认值为1;该类拥有的方法perimeiter和area,分别用于计算矩形的周长和面积;该...
创建一个Rectangle类。该类拥有属性lenghth和width,每个属性的默认值为1;该类拥有的方法perimeiter和area,分别用于计算矩形的周长和面积;该类还有设置和读取属性length和width的方法,设置方法应能检查length和width的属性是否为小于20.0的浮点数。
展开
展开全部
class Rectangle {
double width=1;
double height=1;
public Rectangle(double width,double height)
{
this.width=width;
this.height=height;
}
public void setwidth(double w)
{width=w;
}
public void setheight(double h)
{height=h;
}
public double getwidth()
{return width;
}
public double getheight()
{return height;
}
public boolean f()
{if(width<20&&height<20) return true;
else return false;
}
public void area()
{
System.out.println("这个矩形的面积是:"+width*height);
}
public void perimeter()
{
System.out.println("这个矩形的周长是:"+2*(width+height));
}
}
public class RectangleDemo
{
public static void main(String[] args) {
Rectangle r1=new Rectangle(4,6);
Rectangle r2=new Rectangle(4,7);
r1.area();
r1.perimeter();
r2.area();
r2.perimeter();
}
}
double width=1;
double height=1;
public Rectangle(double width,double height)
{
this.width=width;
this.height=height;
}
public void setwidth(double w)
{width=w;
}
public void setheight(double h)
{height=h;
}
public double getwidth()
{return width;
}
public double getheight()
{return height;
}
public boolean f()
{if(width<20&&height<20) return true;
else return false;
}
public void area()
{
System.out.println("这个矩形的面积是:"+width*height);
}
public void perimeter()
{
System.out.println("这个矩形的周长是:"+2*(width+height));
}
}
public class RectangleDemo
{
public static void main(String[] args) {
Rectangle r1=new Rectangle(4,6);
Rectangle r2=new Rectangle(4,7);
r1.area();
r1.perimeter();
r2.area();
r2.perimeter();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询