ในบทความนี้ เราจะทำความเข้าใจวิธี คำนวณพื้นที่ผิว (Surface Area) และปริมาตร (Volume) ของทรงสี่เหลี่ยมมุมฉาก (Cuboid)
ความแตกต่าง Cube และ Cuboid: Cube มีความยาว กว้าง สูง เท่ากันทั้งหมด ส่วน Cuboid ทั้ง 3 มิติไม่เท่ากัน
สูตร:
- ปริมาตร (Volume) = ความยาว × ความกว้าง × ความสูง (l × w × h)
- พื้นที่ผิว (Surface Area) = 2 × (lw + wh + hl)
ตัวอย่าง Input และ Output
Input:
Length = 6; Width = 7; Height = 8;
Output:
Volume Of the Cuboid is : 336.0 Surface area Of the Cuboid is : 292.0
อัลกอริธึม
ขั้นตอนที่ 1 - เริ่มต้น ขั้นตอนที่ 2 - ประกาศตัวแปร double: my_length, my_width, my_height, my_volume, my_surface_area ขั้นตอนที่ 3 - อ่านค่าจากผู้ใช้ หรือกำหนดค่าเริ่มต้น ขั้นตอนที่ 4 - คำนวณปริมาตร = l * w * h ขั้นตอนที่ 5 - คำนวณพื้นที่ผิว = 2 * (l*w + w*h + h*l) ขั้นตอนที่ 6 - แสดงผลลัพธ์ ขั้นตอนที่ 7 - หยุด
ตัวอย่างที่ 1: รับค่าจากผู้ใช้
import java.util.Scanner;
public class VolumeOfCuboid {
public static void main(String args[]) {
double my_length, my_width, my_height, my_volume, my_surface_area;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined");
System.out.println("Enter the length of Cuboid:");
my_length = my_scanner.nextDouble();
System.out.println("Enter the width of Cuboid:");
my_width = my_scanner.nextDouble();
System.out.println("Enter height of Cuboid:");
my_height = my_scanner.nextDouble();
my_volume = my_length * my_width * my_height;
System.out.println("The volume Of the Cuboid is :" + my_volume);
my_surface_area = 2 * (my_length * my_width + my_width * my_height + my_height * my_length);
System.out.println("The surface area Of the Cuboid is : " + my_surface_area);
}
}
Output
Required packages have been imported A reader object has been defined Enter the length of Cuboid: 6 Enter the width of Cuboid: 7 Enter height of Cuboid: 8 The volume Of the Cuboid is : 336.0 The surface area Of the Cuboid is : 292.0
ตัวอย่างที่ 2: กำหนดค่าไว้ล่วงหน้า
public class VolumeOfCuboid {
public static void main(String args[]) {
double my_length, my_width, my_height, my_volume, my_surface_area;
my_length = 6;
my_width = 7;
my_height = 8;
my_volume = my_length * my_width * my_height;
System.out.println("The volume Of the Cuboid is:" + my_volume);
my_surface_area = 2 * (my_length * my_width + my_width * my_height + my_height * my_length);
System.out.println("The surface area Of the Cuboid is:" + my_surface_area);
}
}
Output
The volume Of the Cuboid is:336.0 The surface area Of the Cuboid is:292.0