본문 바로가기
Java/Java Examples

[Java Examples] 상자 용량, 면적 구하는 예제 및 설명

by Henry Cho 2024. 2. 7.
728x90

상자 용량, 면적 구하는 예제 및 설명

포스트 난이도: HOO_Intern


# Example Code

public class Rectangle {

    private double length;
    private double width;
    
    // Constructor
    public Rectangle(double len, double w) {
        length = len;
        width = w;
    }
    
    // Setter for length
    public void setLength(double len) {
        length = len;
    }
    
    // Setter for width
    public void setWidth(double w) {
        width = w;
    }
    
    // Getter for length
    public double getLength() {
        return length;
    }
    
    // Getter for width
    public double getWidth() {
        return width;
    }
    
    // Method to calculate area
    public double getArea() {
        return width * length;
    }

    // Main method to run the program
    public static void main(String[] args) {
        // Create a Rectangle object
        Rectangle myRectangle = new Rectangle(5.0, 10.0);

        // Set length and width
        myRectangle.setLength(5.0);
        myRectangle.setWidth(10.0);

        // Print the length, width, and area of the rectangle
        System.out.println("Rectangle length: " + myRectangle.getLength());
        System.out.println("Rectangle width: " + myRectangle.getWidth());
        System.out.println("Rectangle area: " + myRectangle.getArea());
    }
}

Figure 1. Result of example code 1


//HOOAI
class box {
    double width;
    double height;
    double depth;
}

class boxExample {
    public static void main(String args[]) {
        box mybox = new box();
        double vol;
        
        mybox.width = 100;
        mybox.height = 20;
        mybox.depth = 50;
               
        vol = mybox.width * mybox.height * mybox.depth;
        System.out.println("Volume is " + vol);          
    }
}

Figure 2. Result of example code 2


# Explanations

이번 포스트는 2021년 2월에 포스팅이 되었던 "상자 용량 구하기" 포스트의 업데이트 버전이다. 앞서서 업데이트가 이루어진 직사각형 및 상자 너비 구하는 포스트와 이어지는 예제 코드라고 생각하면 된다. 앞선 포스트를 보지 못했다면 아래 링크나 검색창에서 검색해서 확인이 가능하다. 첫 번째 예제코드는 아래 링크에 나와있는 포스트에서 다뤘던 2개의 예제코드를 하나로 합쳐 놓은 것이기에 이전 포스트를 리뷰했다면 이번 첫 번째 예제코드는 쉽게 이해할 수 있다.

 

다만 두번째 예제코드는 Volume을 구하는 식이 추가된 것을 알 수 있다. 예제코드 2에서는 부피를 구하는 식을 추가하여 상자 또는 직사각형의 부피를 계산으로 어떻게 작성하고 산출해 내는지를 살펴볼 수 있다. 사실 부피를 산출해 낸다는 게 큰 차이점처럼 보이지만 사실상 계산식을 달리해서 결괏값을 산출해 낸다는 관점에서는 앞서 배운 너비 계산과 동일하기 때문에 어렵지 않게 리뷰가 가능하다. 여기서도 마찬가지로 public main 함수가 포함된 클래스가 boxExample이기 때문에 파일명 또한 boxExample이 된다는 점만 유의하면 된다. 자바는 클래스를 기반으로 한 언어라고 불릴 만큼 클래스를 중점으로 작성이 되다 보니 이 점을 한번 더 이해하고 익숙해보고자 이번 예제코드를 준비해 보았다.


https://whoishoo.tistory.com/88

 

[Java Examples] 상자, 직사각형의 크기를 구하는 예제 및 설명

상자, 직사각형의 크기를 구하는 예제 및 설명 포스트 난이도: HOO_Intern # Example Code //Example1 public class Rectangle { private double length; private double width; public void setLength(double l) { length = l; } public double getL

whoishoo.tistory.com

 


# github

https://github.com/WhoisHOO/HOOAI/blob/main/Java_examples/box3

https://github.com/WhoisHOO/HOOAI/blob/main/Java_examples/box4


 

728x90

댓글