IT 면접 족보25

권단비·2023년 2월 1일
0

IT

목록 보기
53/139
post-thumbnail

1. 아래가 돌아가도록 하시오.

main(){
    	Circle[] circleArr = new Circle[2];
    	circleArr[0] = new Circle(10);
    	circleArr[1] = new Circle(10);

    	//System.out.println(circleArr[0].getArea());
    	//System.out.println(circleArr[1].getArea());
    	    	
    	double areas = Circle.getArrArea(circleArr);
    	System.out.println(areas);
}

▼정답

class Circle9 {
	private double radius;

	public Circle9(double radius) {
		this.radius = radius;
	}

	public double getArea() {
		return radius * radius * Math.PI;
	}

	public static double getArrArea(Circle9[] c) {
		double sum = 0;
		for (int i = 0; i < c.length; i++) {
			sum += c[i].getArea();
		}
		return sum;
	}
}

public class Test36 {
	public static void main(String[] args) {
		Circle9[] circleArr = new Circle9[2];
		circleArr[0] = new Circle9(10);
		circleArr[1] = new Circle9(10);

		// System.out.println(circleArr[0].getArea());
		// System.out.println(circleArr[1].getArea());

		double areas = Circle9.getArrArea(circleArr);
		System.out.println(areas);
	}
}

2. 아래가 18이 나오도록 getStrArr 함수를 만드시오.

String[] strArr = new String[3];
    	
strArr[0] = "ABCDER";
strArr[1] = "ABCDER";
strArr[2] = "ABCDER";
    	
System.out.println(getStrArr(strArr));

▼정답

public class Test36 {
	public static void main(String[] args) {
		String[] strArr = new String[3];
>
		strArr[0] = "ABCDER";
		strArr[1] = "ABCDER";
		strArr[2] = "ABCDER";

		System.out.println(getStrArr(strArr));
	}
>
	static int getStrArr(String[] a) {
		int count = 0;
		for (int i = 0; i < a.length; i++) {
			count += a[i].length();
		}
		return count;
	}
}

3. 아래가 나오도록 Circle 객체를 만드시오.

double areas = Circle.getArrArea(circleArr);
System.out.println(areas);
    	
Rectangle[] recArr = new Rectangle[2];
recArr[0] = new Rectangle(10,20);
recArr[1] = new Rectangle(10,30);

   	    	
areas = Rectangle.getArrArea(recArr);
System.out.println(areas);

▼정답

class Circle9 {
	double radius;

	public Circle9(double radius) {
		this.radius = radius;
	}

	public double getArea() {
		return this.radius * this.radius * Math.PI;
	}

	public static double getArrArea(Circle9[] result) {
		double sum = 0;
		for (int i = 0; i < result.length; i++) {
			sum += result[i].getArea();
		}
		return sum;
	}
}

class Rectangle9 {
	double width;
	double height;

	public Rectangle9(double width, double height) {
		this.width = width;
		this.height = height;
	}

	public double getArea() {
		return this.width * this.height;
	}

	public static double getArrArea(Rectangle9[] result) {
		double sum = 0;
		for (int i = 0; i < result.length; i++) {
			sum += result[i].getArea();
		}
		return sum;
	}
}
public class Test36 {
	public static void main(String[] args) {

		Circle9[] circleArr = new Circle9[2];
		circleArr[0] = new Circle9(10);
		circleArr[1] = new Circle9(10);

		double areas = Circle9.getArrArea(circleArr);
		System.out.println(areas);

		Rectangle9[] recArr = new Rectangle9[2];
		recArr[0] = new Rectangle9(10, 20);
		recArr[1] = new Rectangle9(10, 30);

		areas = Rectangle9.getArrArea(recArr);
		System.out.println(areas);
	}
}

4.main에서 실행된 Box[] ar = new Box[3] 대한 메모리 그림을 그리시오.

class Box {
   private String conts;
   
   Box(String cont) { this.conts = cont; }
   public String toString() {
      return conts;
   }
}

public static void main(String[] args) {
   Box[] ar = new Box[3];

   // 배열에 인스턴스 저장
   ar[0] = new Box("First");
   ar[1] = new Box("Second");
   ar[2] = new Box("Third");

   // 저장된 인스턴스의 참조
   System.out.println(ar[0]);
   System.out.println(ar[1]);
   System.out.println(ar[2]);
}

▼정답

0개의 댓글