Java编程 求矩形周长和面积

请写上类或变量分析或者流程图谢谢
2024-11-25 21:39:32
推荐回答(2个)
回答1:

import java.util.*;
public class Rectangle {

private float length; //定义长变量
private float width; // 宽变量
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;

} //求周长方法

public float getArea(){
return length*width;
} //求面积方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//调用输入方法

System.out.println ("请输入矩形的长:");
float a=in.nextFloat();
System.out.println ("请输入矩形的宽:");
float b=in.nextFloat();
System.out.println ("矩形周长为:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面积为:"+new Rectangle(a,b).getArea());

}

}
//Jcreator4.0编译通过,写的比较简单 只有简单的功能 刚刚写的求周长时忘乘2了...

回答2:

public class Rectangle
{
private float sideA;//边长A
private float sideB;//边长B

//不带参数的构造方法
public Rectangle()
{
//
}

//带参数的构造方法
public Rectangle(float sideA , float sideB)
{
this.sideA = sideA;
this.sideB = sideB;
}

//设置边长
public void setSide(int sideA , int sideB)
{
this.sideA = sideA;
this.sideB = sideB
}

//获取周长
public float getPerimeter()
{
return (sideA + sideB)*2;
}

//获取面积
public float getArea()
{
return sideA*sideB;
}
}