在java中创建一个rectangle类,使其有width,height两个属性,并且包含?两个方
在java中创建一个rectangle类,使其有width,height两个属性,并且包含?两个方法area,perimeter分别能计算自身的面积及周长,然后创建该类的...
在java中创建一个rectangle类,使其有width,height两个属性,并且包含?两个方法area,perimeter分别能计算自身的面积及周长,然后创建该类的两个构造方法,其中一个是默认构造方法,一个是带宽与高两个参数的构造方法。然后创建Test类,在main方法中中创建该矩形类的两个实例a、b,一个是创建后,再设定宽与高,一个是在创建时直接设定宽与高。然后利用rectangle的面积方法、周长方法分别打印出两个矩形的面积与周长。
展开
3个回答
展开全部
public class Rectangle{
private double width;
private double height;
public double area(){
return width*height;
}
public double perimeter(){
return (width+height)*2;
}
public Rectangle(){}
public Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public void setWidth(double width){
this.width=width;
}
public double getWidth(){
return width;
}
public double setHeight(double height){
this.height=height;
}
public double getHeight(){
return height;
}
}
public class Test{
public static void main(String... args){
Rectangle a = new Rectangle();
a.setWidth(width);//此处width填入你所需要的宽度
a.setHeight(height);//此处height填入早激帆你所需要的高度
Rectangle b = new 陆雹Rectangle(width,height);//此处width与height分别填入铅纤你所需要的宽度与高度
System.out.println(a.area());//a的面积
System.out.println(a.perimeter());//a的周长
System.out.println(b.area());//b的面积
System.out.println(b.perimeter());//b的周长
}
}
展开全部
类Rectangle:
public 或谈class Rectangle {
private double width;
private double height;
public Rectangle(){
}
public Rectangle(double width , double height){
this.width = width;
this.height = height;
}
public double area(){
return width * height;
}
public double perimeter(){
return 2 * (width + height);
}
// getter and setter
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
类Test:
public static void 汪键main(String[] args) {
Rectangle recOne = new Rectangle();
recOne.setWidth(200);
recOne.setHeight(100);
System.out.println("第一种方法计算周长和面积");
System.out.println("周长:" + recOne.perimeter());
System.out.println("面积:" + recOne.area());
Rectangle recTwo = new Rectangle(200 , 100);
System.out.println("第二种方法计算周长和面积");
System.out.println("周长:" + recTwo.perimeter());
System.out.println("面积:"困团巧 + recTwo.area());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-03-31
展开全部
Text 类:
public class Text {
public static void main(String[] args) {
Rectangle a=new Rectangle();
a.setWidth(2);
a.setHeight(3);
Rectangle b=new Rectangle(3,4);
System.out.println("举燃尘段燃面积:"+a.area()+" 周长"正禅+a.perimeter());
System.out.println("面积:"+b.area()+" 周长"+b.perimeter());
}
}
Rectangle类:
private int width;
private int height;
public Rectangle(){
}
public Rectangle(int width,int height){
this.width=width;
this.height=height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int area(){
return this.width*this.height;
}
public int perimeter(){
return (this.width+this.height)*2;
}
public class Text {
public static void main(String[] args) {
Rectangle a=new Rectangle();
a.setWidth(2);
a.setHeight(3);
Rectangle b=new Rectangle(3,4);
System.out.println("举燃尘段燃面积:"+a.area()+" 周长"正禅+a.perimeter());
System.out.println("面积:"+b.area()+" 周长"+b.perimeter());
}
}
Rectangle类:
private int width;
private int height;
public Rectangle(){
}
public Rectangle(int width,int height){
this.width=width;
this.height=height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int area(){
return this.width*this.height;
}
public int perimeter(){
return (this.width+this.height)*2;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询