๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜•Java

[Java ์ดํ•ดํ•˜๊ธฐ] 19. Java๋กœ ๋„ํ˜• ๋ฉด์ ๊ตฌํ•˜๊ธฐ

by Luvbit 2024. 5. 27.


 

 

๐Ÿ”ด๐ŸŸง๐ŸŸก๐ŸŸฉ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ €๋ฒˆ์— ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ํ† ๋Œ€๋กœ Java๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋„ํ˜• ๋ฉด์  ๊ตฌํ•˜๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฉด์  ๊ณ„์‚ฐ๊ธฐ์— ๊ฐ์ฒด๋ผ๋Š” ๊ฐœ๋…์„ ์ ์šฉํ•˜์—ฌ, ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋„ํ˜•์˜ ๋ฉด์ ์„ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

1. ์šฐ์„  ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”. ํŒจํ‚ค์ง€๋ช…์€ ์›ํ•˜์‹œ๋Š” ๊ฑธ๋กœ ํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค

์ €๋Š” shapeCalculator๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

2. ๊ธฐ์กด์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋ฅผ ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆ„๊ฒ ์Šต๋‹ˆ๋‹ค.

Main ํด๋ž˜์Šค์™€ ShapeAreaCalculator ํด๋ž˜์Šค ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

 

 

 

 

3. ์ด์ œ ShapeAreaCalculatorํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ค๋ฒ„๋กœ๋”ฉ ์‹ค์Šต์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ•„๋“œ๋‚˜ ์ƒ์„ฑ์ž ์—†์ด ๋ฉ”์„œ๋“œ๋งŒ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด์˜ ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

 

 

๐Ÿ’ก 

1. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅ ์‹œ ์›์˜ ๋ฉด์ ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
2. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‘ ๊ฐœ๋ฅผ ์ž…๋ ฅ ์‹œ ์ง์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
3. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ธ ๊ฐœ๋ฅผ ์ž…๋ ฅ ์‹œ ์‚ฌ๋‹ค๋ฆฌ๊ผด์˜ ๋ฉด์ ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

1-1) ๋ฐ˜์ง€๋ฆ„์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
2-1) ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
3-1) ๋‘ ๋ฐ‘๋ณ€๊ณผ ๋†’์ด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

 

 

 

 

๋ฉ”์„œ๋“œ๋ช…์„ calculateArea๋กœ ํ•˜์—ฌ 3๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉํ•˜์—ฌ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

๋ณด์‹œ๋‹ค์‹œํ”ผ ๋ฉ”์„œ๋“œ๋ช…์€ ๋™์ผํ•˜์ง€๋งŒ, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

 

package shapeCalculator;

public class ShapeAreaCalculator {

    // ์›์˜ ๋ฉด์ 
    // ๋ฐ˜์ง€๋ฆ„
    public double calculateArea(double radius) {
        return Math.PI * radius * radius;
    }


    // ์ง์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ 
    // ๋„ˆ๋น„, ๋†’์ด
    public double calculateArea(double width, double height) {
        return width * height;
    }


    // ์‚ฌ๋‹ค๋ฆฌ๊ผด์˜ ๋ฉด์ 
    // ๋‘ ๋ฐ‘๋ณ€, ๋†’์ด
    public double calculateArea(double base1, double base2, double height) {
        return 0.5 * (base1 + base2) * height;
    }
    
}

 

 

 

 

 

 

 

 

4. ์ด์ œ Main ํด๋ž˜์Šค์—์„œ ShapeAreaCalculator ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด, ๋„ํ˜•์˜ ๋ฉด์ ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

package shapeCalculator;

public class Main {
	public static void main(String[] args) {
    
		ShapeAreaCalculator cal = new ShapeAreaCalculator();
        
		// ์›
		double circleArea = cal.calculateArea(5.0); 
        
		// ๋ฐ˜์ง€๋ฆ„์ด 5์ธ ์›
		System.out.println("Circle Area: " + circleArea);
        
        
		// ์ง์‚ฌ๊ฐํ˜•
		double rectangleArea = cal.calculateArea(4.0, 6.0);
        
		// ๋„ˆ๋น„๊ฐ€ 4, ๋†’์ด๊ฐ€ 6์ธ ์ง์‚ฌ๊ฐํ˜•
		System.out.println("Rectangle Area: " + rectangleArea);
        
        
		// ์‚ฌ๋‹ค๋ฆฌ๊ผด
		double trapezoidArea = cal.calculateArea(3.0, 5.0, 4.0);
        
		// ๋ฐ‘๋ณ€1์ด 3, ๋ฐ‘๋ณ€2๊ฐ€ 5, ๋†’์ด๊ฐ€ 4
		System.out.println("Trapezoid Area: " + trapezoidArea);
        
	    }
}

 

 

 

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์‹ค์Šต์„ ํ†ตํ•ด ์˜ค๋ฒ„๋กœ๋”ฉ์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

์˜ค๋ฒ„๋กœ๋”ฉ์€ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ผ๊ด€๋˜๊ฒŒ ์œ ์ง€ํ•˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์—ฌ
์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์„ ๋†’์ด๋Š” ์ค‘์š”ํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๋” ์ง๊ด€์ ์ด๊ณ  ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

 

 

์˜ค๋Š˜ ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ์ง์ ‘ ์‹ค์Šตํ•ด ๋ณด์‹œ๋ฉด ๋งŽ์€ ๋„์›€์ด ๋˜์‹ค ๊ฒ๋‹ˆ๋‹ค.
๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ™œ์šฉํ•ด ๋ณด์„ธ์š”!

์งˆ๋ฌธ์€ ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ์ฃผ์„ธ์š”.
๐Ÿฉต๐Ÿฐ๐Ÿฉต