โปรแกรมคำนวณพื้นที่รูปทรงเรขาคณิต
โจทย์
You are tasked with creating a simple geometric calculation program. In this program, you need to define different types of shapes (e.g., Circle, Rectangle, and Triangle) and calculate their areas. Each shape should be able to describe itself by providing its dimensions and calculated area.
Class Descriptions:
- Create an abstract class called
Shapeclass that contains:- Methods
- An abstract method
calculateArea()that returns the area of the shape. - A concrete method void
describe()that prints out the type of shape and its area by invoking the calculateArea() method.
- An abstract method
- Methods
- Create a derived class of
ShapecalledCirclethat contains:- Attribute
- A private double attribute named
radius, representing the radius of the circle.
- A private double attribute named
- Encapsulation
- The attribute should be private.
- Provide public getter and setter for the radius attribute.
- Method
calculateArea()that calculates the area of the circle.
- Attribute
- Create a derived class of Shape called
Rectanglethat contains:- Attribute
- A private double attribute named
width, representing the width of the rectangle. - A private double attribute named
height, representing the height of the rectangle.
- A private double attribute named
- Encapsulation
- The attribute should be private.
- Provide public getters and setters for the width and height attributes.
- Method
calculateArea()that calculates the area of the rectangle.
- Attribute
- Create a derived class of Shape called
Trianglethat contains:- Attribute
- A private double attribute named
base, representing the base of the triangle. - A private double attribute named
height, representing the height of the triangle.
- A private double attribute named
- Encapsulation
- The attribute should be private.
- Provide public getters and setters for the base and height attributes.
- Method
calculateArea()that calculates the area of the triangle.
- Attribute
โค้ดเริ่มต้น
public class ShapeDriver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input for Circle
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
// Get input for Rectangle
double width = scanner.nextDouble();
double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
// Get input for Triangle
double base = scanner.nextDouble();
height = scanner.nextDouble(); // Reusing the variable for convenience
Triangle triangle = new Triangle(base, height);
// Create an array of Shape objects
Shape[] shapes = new Shape[]{circle, rectangle, triangle};
// Iterate over the array and describe each shape
for (Shape shape : shapes) {
shape.describe();
}
}
}
class Shapes{
// Write your code here
}
class Circle{
// Write your code here
}
class Rectangle{
// Write your code here
}
class Triangle{
// Write your code here
}โค้ด
import java.util.Scanner;
public class ShapeDriver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input for Circle
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
// Get input for Rectangle
double width = scanner.nextDouble();
double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
// Get input for Triangle
double base = scanner.nextDouble();
height = scanner.nextDouble(); // Reusing the variable for convenience
Triangle triangle = new Triangle(base, height);
// Create an array of Shape objects
Shape[] shapes = new Shape[]{circle, rectangle, triangle};
// Iterate over the array and describe each shape
for (Shape shape : shapes) {
shape.describe();
}
}
}
abstract class Shape {
abstract public double calculateArea();
public void describe() {
System.out.println("Shape: " + this.getClass().getSimpleName() + ", Area: " + calculateArea());
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle (double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return this.width;
}
public double getHeight() {
return this.height;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
public double calculateArea() {
return width * height;
}
}
class Triangle extends Shape {
private double base;
private double height;
public Triangle (double base, double height) {
this.base = base;
this.height = height;
}
public double getBase() {
return this.base;
}
public double getHeight() {
return this.height;
}
public void setBase(double base) {
this.base = base;
}
public void setHeight(double height) {
this.height = height;
}
public double calculateArea() {
return (1/2.0) * base * height;
}
}คำอธิบาย
โครงสร้างของระบบ
คลาส Shape (Abstract Class)
คลาสนี้เป็น abstract class ที่กำหนดพฤติกรรมพื้นฐานของรูปทรงทุกประเภท
abstract class Shape {
// เมธอดที่ต้องถูก implement โดยคลาสลูก
abstract public double calculateArea();
// เมธอดที่ใช้ร่วมกัน
public void describe() {
System.out.println("Shape: " + this.getClass().getSimpleName() +
", Area: " + calculateArea());
}
}calculateArea()เป็น abstract method ที่บังคับให้ทุกรูปทรงต้องมีวิธีคำนวณพื้นที่describe()เป็นเมธอดที่ใช้แสดงชื่อรูปทรงและพื้นที่getClass().getSimpleName()ใช้ดึงชื่อคลาสของวัตถุนั้นๆ
คลาส Circle (วงกลม)
class Circle extends Shape {
private double radius; // รัศมี
// Constructor
public Circle(double radius) {
this.radius = radius;
}
// Getter และ Setter
public double getRadius() { return this.radius; }
public void setRadius(double radius) { this.radius = radius; }
// คำนวณพื้นที่วงกลม
public double calculateArea() {
return Math.PI * radius * radius; // π × r²
}
}การคำนวณ:
- สูตร:
- ใช้
Math.PIสำหรับค่า - ตัวอย่าง: ถ้ารัศมี = 5
- พื้นที่ = 3.14159… × 5² = 78.54 หน่วย²
คลาส Rectangle (สี่เหลี่ยมผืนผ้า)
class Rectangle extends Shape {
private double width; // ความกว้าง
private double height; // ความสูง
// Constructor
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Getters และ Setters
public double getWidth() { return this.width; }
public double getHeight() { return this.height; }
public void setWidth(double width) { this.width = width; }
public void setHeight(double height) { this.height = height; }
// คำนวณพื้นที่สี่เหลี่ยม
public double calculateArea() {
return width * height; // กว้าง × ยาว
}
}การคำนวณ:
- สูตร: กว้าง × ยาว
- ตัวอย่าง: ถ้ากว้าง = 4, ยาว = 5
- พื้นที่ = 4 × 5 = 20 หน่วย²
คลาส Triangle (สามเหลี่ยม)
class Triangle extends Shape {
private double base; // ฐาน
private double height; // ความสูง
// Constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Getters และ Setters
public double getBase() { return this.base; }
public double getHeight() { return this.height; }
public void setBase(double base) { this.base = base; }
public void setHeight(double height) { this.height = height; }
// คำนวณพื้นที่สามเหลี่ยม
public double calculateArea() {
return (1/2.0) * base * height; // (1/2) × ฐาน × สูง
}
}การคำนวณ:
- สูตร: (1/2) × ฐาน × สูง
- ตัวอย่าง: ถ้าฐาน = 6, สูง = 4
- พื้นที่ = (1/2) × 6 × 4 = 12 หน่วย²
ตัวอย่างการทำงาน
การใช้งานระบบผ่าน ShapeDriver:
1. การรับข้อมูล
Scanner scanner = new Scanner(System.in);
// รับข้อมูลวงกลม
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
// รับข้อมูลสี่เหลี่ยม
double width = scanner.nextDouble();
double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
// รับข้อมูลสามเหลี่ยม
double base = scanner.nextDouble();
height = scanner.nextDouble();
Triangle triangle = new Triangle(base, height);2. การแสดงผล
Shape[] shapes = new Shape[]{circle, rectangle, triangle};
for (Shape shape : shapes) {
shape.describe();
}ปรับปรุงล่าสุด