πβ λ€μκ³Ό κ°μ 쑰건μ λ§μ‘±νλ νλ‘κ·Έλ¨μ μμ± νμΈμ
π© Example Output
Rectangle λ©΄μ : 1473.15
Rectangle λλ : 154.4
Circle λ©΄μ : 754.7676350249478
Circle λλ : 97.38937226128358
public abstract class Shape {
/* μΆμ λ©μλ */
public abstract double area();
public abstract double perimeter();
}
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;
}
}
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;
}
}
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λ¬ΈμΌλ‘ λ°λ³΅νλ
λ°©λ²μΌλ‘ λ¬Έμ₯μ λ μ€λ‘ μ€μ΄λ ν¨μ¬ ν¨μ¨μ μΈ μ½λκ° μμ±λμλ€. νκ³ λ λ€, κ΅μ₯ν λΏλ―ν λ¬Ένμ΄μλ€.
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
public interface IShape {
public double area();
public double perimeter();
}
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;
}
}
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;
}
}
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);
}
}
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()λ₯Ό μ¬μ©ν΄λ³Έ
μ λ κ΅μ₯ν λΏλ―νλ€. μμ§ λ°°μ΄μ μλ²½ν μ΄ν΄νμ§ λͺ» ν μνμ¬μ μ’μ μ½λλ₯Ό μμ±ν μ μμμ§ λ―Έμ§μμμΌλ,
μ½λλ μ λ§ μ§λ©΄ 지μλ‘ μμ±λκ° λμμ§λ κ² κ°λ€. μλ‘κ² κΉ¨λ«κ³ λ°°μ°λ건 μ¦κ²λ€.
public class Application {
public static void main(String[] args) {
// ShapeManager ν΄λμ€μ calcShapeArray λ©μλ νΈμΆ
ShapeManager manager = new ShapeManager();
manager.calcShapeArray();
}
}
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();
}
}
}
}