[Java] Solution to puzzles

Joy🌱·2023λ…„ 1μ›” 2일
0

🧩 Coding Challenges

λͺ©λ‘ 보기
9/20
post-thumbnail

🧩 클래슀 λ‹€μ΄μ–΄κ·Έλž¨μ— 따라 ν”„λ‘œκ·Έλž¨ μž‘μ„±ν•˜κΈ° (좔상 클래슀)

πŸ’β€ λ‹€μŒκ³Ό 같은 쑰건을 λ§Œμ‘±ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„± ν•˜μ„Έμš”


🚩 Example Output

Rectangle 면적 : 1473.15
Rectangle λ‘˜λ ˆ : 154.4
Circle 면적 : 754.7676350249478
Circle λ‘˜λ ˆ : 97.38937226128358

β—Ό Shape Class (Abstract)

public abstract class Shape {
	
	/* 좔상 λ©”μ†Œλ“œ */
	public abstract double area();
	
	public abstract double perimeter();
}

β—Ό Rectangle Class

public class Rectangle extends Shape {

	/* ν•„λ“œ */
	private double width;
	private double height;
	
	/* μƒμ„±μž */
	public Rectangle() {}
	
	public Rectangle(double width, double height) {
		this.width = width;
		this.height = height;
	}
	
	/* getter & setter */
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	
	/* λ©”μ†Œλ“œ */
	@Override
	public double area() {
		return this.width * this.height;
	}
	
	@Override
	public double perimeter() {
		return (this.width + this.height) * 2;
	}
}

β—Ό Circle Class

public class Circle extends Shape {

	/* ν•„λ“œ */
	public static final double PI = Math.PI;
	private double radius; >>> λ°˜μ§€λ¦„

	/* μƒμ„±μž */
	public Circle() {}
	
	public Circle(double radius) {
		this.radius = radius;
	}

	/* getter & setter */
	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public static double getPi() {
		return PI;
	}
	
	/* λ©”μ†Œλ“œ */
	public double area() {
		return this.radius * this.radius * this.PI;
	}
	
	public double perimeter() {
		return (this.radius * 2) * this.PI;
	}
}

β—Ό ShapeManager Class

public class ShapeManager {

	public void calcShape() {
		
        // λ„ν˜•λ“€μ΄ μƒμ†λ°›λŠ” Shape 클래슀의 λ°°μ—΄ 객체 생성 및 μ΄ˆκΈ°ν™”
		Shape[] shape = new Shape[2];			
		String[] rnc = {"Rectangle", "Circle"};		>>> λ„ν˜•μ΄λ¦„ λ°°μ—΄
		
		shape[0] = new Rectangle(34.5, 42.7);
		shape[1] = new Circle(15.5);
		
		for(int i = 0; i < shape.length; i++) {
			shape[i].area();		>>> 각 λ„ν˜•μ˜ λ©΄μ κ΅¬ν•˜λŠ” λ©”μ†Œλ“œ
			shape[i].perimeter();	>>> 각 λ„ν˜•μ˜ λ‘˜λ ˆκ΅¬ν•˜λŠ” λ©”μ†Œλ“œ
			
			System.out.println(rnc[i] + " 면적 : " + shape[i].area());
			System.out.println(rnc[i] + " λ‘˜λ ˆ : " + shape[i].perimeter());
		}	
	}
}

πŸ’¬ Overall Comment

* 였늘 μˆ˜μ—…μ—μ„œ 배운 λ‚΄μš©μ„ ν† λŒ€λ‘œ ν’€μ–΄λƒˆλ˜ λ¬Έν•­μ΄μ—ˆλ‹€. 예제λ₯Ό recipe둜 μ‚Όμ•„ μ—΄μ‹¬νžˆ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆλŠ”λ°, 
  μΆ”μƒν΄λž˜μŠ€μ™€ 상속을 μ½”λ“œλ‘œ ν’€μ–΄λ‚΄λŠ”κ±΄ 처음이라 μ΄ˆλ°˜μ—” λ”λŽ μ§€λ§Œ 클래슀 λ‹€μ΄μ–΄κ·Έλž¨μ„ 따라 μ΅œλŒ€ν•œ 
  ν•  수 μžˆλŠ”κ±΄ 해보렀고 ν•˜λ‹ˆ 끝내 μ™„μ„±λ˜μ—ˆλ‹€.
* μ›λž˜μ˜ μ½”λ“œμ—” prinln식이 λ„ν˜•λ§ˆλ‹€ μž‘μ„±λ˜μ–΄μžˆμ–΄ 같은 κ΅¬μ„±μ˜ println식이 λΆˆν•„μš”ν•˜κ²Œ λ°˜λ³΅λ˜μ–΄μžˆμ—ˆλŠ”λ°, 
  μ½”λ“œλ₯Ό 더 κ°„κ²°ν•˜κ²Œ λ§Œλ“€ 수 μžˆμ„κ±°λΌλŠ” μ„ μƒλ‹˜μ˜ 쑰언을 λ“£κ³ λ‚˜μ„œ κ³ λ―Όν•˜λ‹€κ°€ for문으둜 λ°˜λ³΅ν•˜λŠ” 
  λ°©λ²•μœΌλ‘œ λ¬Έμž₯을 두 μ€„λ‘œ μ€„μ΄λ‹ˆ 훨씬 효율적인 μ½”λ“œκ°€ μ™„μ„±λ˜μ—ˆλ‹€. ν’€κ³  λ‚œ λ’€, ꡉμž₯히 λΏŒλ“―ν•œ λ¬Έν•­μ΄μ—ˆλ‹€.

β—Ό Application Class

public class Application {

	public static void main(String[] args) {
		
        // ShapeManager 클래슀의 calcShape λ©”μ†Œλ“œ 호좜
		ShapeManager manager = new ShapeManager();

		manager.calcShape();
	}
}

🧩 클래슀 λ‹€μ΄μ–΄κ·Έλž¨μ— 따라 ν”„λ‘œκ·Έλž¨ μž‘μ„±ν•˜κΈ° (μΈν„°νŽ˜μ΄μŠ€)

πŸ’β€ λ‹€μŒκ³Ό 같은 쑰건을 λ§Œμ‘±ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„± ν•˜μ„Έμš”.
(Shape 클래슀λ₯Ό μΈν„°νŽ˜μ΄μŠ€λ‘œ λ³€κ²½ν•˜λŠ” 것 외에 μœ„μ™€ λ™μΌν•˜κ³ , Triangle 클래슀 μΆ”κ°€)

🚩 Example Output

Circle -  면적 : 38.48451000647496, λ‘˜λ ˆ : 21.991148575128552
Rectangle -  면적 : 60.5, λ‘˜λ ˆ : 33.0
Triangle -  면적 : 45.0, λ‘˜λ ˆ : 33.6509716980849, λΉ—λ³€ : 14.150971698084906
Circle -  면적 : 113.09733552923255, λ‘˜λ ˆ : 37.69911184307752
Triangle -  면적 : 15.75, λ‘˜λ ˆ : 19.821658488546618, λΉ—λ³€ : 8.32165848854662

β—Ό IShape Interface

public interface IShape {

	public double area();
	
	public double perimeter();
}

β—Ό Rectangle Class

public class Rectangle implements IShape {

	/* ν•„λ“œ */
	private double width;
	private double height;
	
	/* μƒμ„±μž */
	public Rectangle() {}
	
	public Rectangle(double width, double height) {
		this.width = width;
		this.height = height;
	}
	
	/* getter & setter */
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	
	/* λ©”μ†Œλ“œ */
	@Override
	public double area() {
		
		return this.width * this.height;
	}
	@Override
	public double perimeter() {
		
		return (this.width + this.height) * 2;
	}
}

β—Ό Circle Class

public class Circle implements IShape {

	/* ν•„λ“œ */
	public static final double PI = Math.PI;
	private double radius; >>> λ°˜μ§€λ¦„

	/* μƒμ„±μž */
	public Circle() {}
	
	public Circle(double radius) {
		this.radius = radius;
	}

	/* getter & setter */
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public static double getPi() {
		return PI;
	}
	
	/* λ©”μ†Œλ“œ */
	@Override
	public double area() {
		
		return this.radius * this.radius * this.PI;
	}

	@Override
	public double perimeter() {
		
		return (this.radius * 2) * this.PI;
	}
}

β—Ό Triangle Class

public class Triangle implements IShape {

	/* ν•„λ“œ */
	private double base;	// λ°‘λ³€
	private double height;	// 높이
	
	/* μƒμ„±μž */
	public Triangle() {}
	
	public Triangle(double base, double height) {
		this.base = base;
		this.height = height;
	}
	
	/* getter & setter */
	public double getBase() {
		return base;
	}

	public void setBase(double base) {
		this.base = base;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}
	
	/* λ©”μ†Œλ“œ */
	public double area() {
		return base * height * 0.5;
	}
	
	public double perimeter() {
		return base + height + Math.sqrt(base * base + height * height);
	}
	
	public double hypotenuse() { >>> λΉ—λ³€ 길이 계산
		return Math.sqrt(base * base + height * height);
	}
}

β—Ό ShapeManager Class

public class ShapeManager {
	
	public void calcShapeArray() {
		
		IShape[] iShape = new IShape[5];
		String[] rnc = {"Circle", "Rectangle", "Triangle", "Circle", "Triangle"};
		
		iShape[0] = new Circle(3.5);
		iShape[1] = new Rectangle(5.5, 11);
		iShape[2] = new Triangle(12, 7.5);
		iShape[3] = new Circle(6);
		iShape[4] = new Triangle(7, 4.5);
		
		for(int i = 0; i < iShape.length; i++) {
			iShape[i].area();
			iShape[i].perimeter();
			
			if(rnc[i].equals("Triangle")) {
				String triPrint = ", λΉ—λ³€ : " + ((Triangle)iShape[i]).hypotenuse();
				System.out.println(rnc[i] + " -  면적 : " + iShape[i].area() + ", λ‘˜λ ˆ : " + iShape[i].perimeter() + triPrint);
			} else {
				System.out.println(rnc[i] + " -  면적 : " + iShape[i].area() + ", λ‘˜λ ˆ : " + iShape[i].perimeter());
			}
		}
	}	
}

πŸ’¬ Overall Comment

* 처음으둜 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•˜μ—¬ μ—¬λŸ¬ 클래슀λ₯Ό κ΅¬ν˜„ν•΄λ³΄μ•˜λŠ”λ°, μœ„μ˜ μΆ”μƒν΄λž˜μŠ€μ™€ λΉ„μŠ·ν•œλ“― ν–ˆμœΌλ‚˜ 
  차이점은 λΆ„λͺ…ν–ˆλ‹€. 이 μ½”λ“œμ—μ„  extends λŒ€μ‹  implements λ₯Ό μ‚¬μš©ν•œ 것이 λŒ€ν‘œμ μ΄λ‹€. 
* λ˜ν•œ 이번 λ¬Έν•­μ—μ„œλŠ” Triangle ν΄λž˜μŠ€κ°€ μΆ”κ°€λ˜μ—ˆλŠ”λ° μœ„ λ¬Έν•­κ³ΌλŠ” 달리 빗변을 μΆ”κ°€μ μœΌλ‘œ κ΅¬ν•΄μ•Όν–ˆκ³  
  λ¬΄μž‘μœ„μ˜ λ„ν˜•κ³Ό μˆ˜κ°€ λ“€μ–΄κ°€μ•Όν•΄μ„œ 각 λ°°μ—΄μ˜ μΈλ±μŠ€μ— λ”°λ‘œ μ΄ˆκΈ°ν™”λ₯Ό ν•΄μ£Όμ—ˆλ‹€. for문을 μ‚¬μš©ν•˜μ—¬ 좜λ ₯ν•œ 
  방법은 μœ„ λ¬Έν•­κ³Ό λ™μΌν–ˆμ§€λ§Œ, 빗변을 좜λ ₯ν•΄μ•Όν•˜λŠ” Triangle의 경우 if문을 μ‚¬μš©ν•˜κ³  triPrint λ³€μˆ˜λ₯Ό 
  μ‚¬μš©ν•˜μ—¬ μ΅œλŒ€ν•œ κ°„κ²°ν•˜κ³  κ°€λ…μ„±μžˆκ²Œ μ½”λ“œλ₯Ό κ΅¬μ„±ν–ˆλ‹€. 처음으둜 λ¬Έμžμ—΄ 비ꡐ λ©”μ†Œλ“œμΈ equals()λ₯Ό μ‚¬μš©ν•΄λ³Έ 
  점도 ꡉμž₯히 λΏŒλ“―ν–ˆλ‹€. 아직 배열을 μ™„λ²½νžˆ μ΄ν•΄ν•˜μ§€ λͺ» ν•œ μƒνƒœμ—¬μ„œ 쒋은 μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμ„μ§€ λ―Έμ§€μˆ˜μ˜€μœΌλ‚˜, 
  μ½”λ“œλŠ” 정말 짜면 지수둝 완성도가 λ†’μ•„μ§€λŠ” 것 κ°™λ‹€. μƒˆλ‘­κ²Œ κΉ¨λ‹«κ³  λ°°μš°λŠ”κ±΄ 즐겁닀.

β—Ό Application Class

public class Application {

	public static void main(String[] args) {

		// ShapeManager 클래슀의 calcShapeArray λ©”μ†Œλ“œ 호좜
		ShapeManager manager = new ShapeManager();
		
		manager.calcShapeArray();
	}
}

πŸ’‘ Another Solution

β—Ό Another ShapeManager Class for refer

public class ShapeManager {
	
	public void calcShape() {
		
		IShape[] shapeList = {new Rectangle(34.5, 42.7), new Circle(15.5)};
		
		for(IShape shape : shapeList) {
			
			String shapeType = shape instanceof Rectangle ? "Rectangle" : "Circle";
			
			System.out.println(shapeType + " : " + shape.area());
			System.out.println(shapeType + " : " + shape.perimeter());
		}
	}
	public void calcShapeArray() {
		
		IShape[] iarr = new IShape[5];
		
		iarr[0] = new Circle(3.1);
		iarr[1] = new Rectangle(2.3, 4.1);
		iarr[2] = new Triangle(4.0, 2.5);
		iarr[3] = new Circle(1.5);
		iarr[4] = new Triangle(2.0, 3.0);
		
		for(int i = 0; i < iarr.length; i++) {
			
			String shapeType = "";
			
			if(iarr[i] instanceof Circle) {
				shapeType = "Circle";
			} else if(iarr[i] instanceof Rectangle) {
				shapeType = "Rectangle";
			} else if(iarr[i] instanceof Triangle) {
				shapeType = "Triangle";
			}
			System.out.print(shapeType + " - 면적 : " + iarr[i].area() + ", λ‘˜λ ˆ : " + iarr[i].perimeter());
			
			if(iarr[i] instanceof Triangle) {
				System.out.println(", λΉ—λ³€ : " + ((Triangle)iarr[i]).hypotenuse());
			} else {
				System.out.println();
			}
		}
	}
}
profile
Tiny little habits make me

0개의 λŒ“κΈ€