求矩形周長與面積

ThinkPet發表於2018-03-15
package cube;
//矩形周長與面積
class Rectangle  
{
	double width;
	double height;
	Rectangle()
	{
		width=2;
		height=3;
	}	
	double length()//這是無參建構函式
	{
		double l=2*(width+height);
		return l;
	}
	double getArea(double w,double h)//這是有參建構函式
	{
		w=this.width;
		h=this.height;
		double a=w*h;
		return a;
	}
}
public class Cube
{
	public static void main(String[] args)
	{
		Rectangle r1=new Rectangle();
		double l1=r1.length();
		double a1=r1.getArea(r1.width,r1.height);
		System.out.println("矩形周長為:"+l1);
		System.out.println("矩形面積為:"+a1);
	}
}

相關文章