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);
}
}
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;
}
}
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);
}
}
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]);
}
▼정답