當前位置:編程學習大全網 - 編程軟體 - 編寫java程序,輸入壹個長方體的長、寬、高,求長方體的表面積和體積,並將結果顯示

編寫java程序,輸入壹個長方體的長、寬、高,求長方體的表面積和體積,並將結果顯示

1、長方體表面積公式?:S?=?2(ab?+?bc?+?ac);

2、長方體體積公式?:V?=?abc?=?Sh;(這裏的S表示底面積)。

實現如下:

public?class?Cuboid?{

// 定義長方體的長、寬、高

private?double?length,?width,?height;

public?Cuboid(double?length,?double?width,?double?height)?{

super();

this.length?=?length;

this.width?=?width;

this.height?=?height;

}

// 獲取當前長方體的表面積

public?double?getSurface()?{

return?getSurface(length,?width,?height);

}

// 獲取當前長方體的體積

public?double?getVolume()?{

return?getVolume(length,?width,?height);

}

// 計算長方體表面積的通用方法

public?static?double?getSurface(double?length,?double?width,?double?height)?{

return?2?*?(length?*?width?+?width?*?height?+?length?*?height);

}

// 計算長方體體積的通用方法

public?static?double?getVolume(double?length,?double?width,?double?height)?{

return?length?*?width?*?height;

}

public?static?void?main(String[]?args)?{

// 1、創建長方體對象,計算當前長方體的表面積和體積

Cuboid?cuboid?=?new?Cuboid(1,?1.6,?4.8);

System.out.println(cuboid.getSurface());

System.out.println(cuboid.getVolume());

// 2、使用通用方法,計算任意長方體的表面積和體積

System.out.println(Cuboid.getSurface(1,?1.6,?4.8));

System.out.println(Cuboid.getVolume(1,?1.6,?4.8));

}

}

  • 上一篇:如何使用滾筒雕刻機的程序?
  • 下一篇:如何提高軟件研發的效率
  • copyright 2024編程學習大全網