-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubeTest.java
More file actions
36 lines (30 loc) · 1.04 KB
/
Copy pathCubeTest.java
File metadata and controls
36 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package javaExD;
public class CubeTest {
public static void main(String[] args) {
// 두 정육면체 객체 생성
Cube a = new Cube(3);
Cube b = new Cube(5);
// 두 정육면체의 부피와 겉넓이 출력
System.out.printf("정육면체 a의 부피: %d, 겉넓이: %d\n", a.volume(), a.surfaceArea());
System.out.printf("정육면체 b의 부피: %d, 겉넓이: %d\n", b.volume(), b.surfaceArea());
}
}
class Cube {
// 필드
int length; // 한 변의 길이
// 생성자
public Cube(int i) {
/* 1. 생성자를 완성하세요. */
this.length = i;
}
// 메소드(인스턴스 메소드) - 정육면체의 부피 반환
public int volume() {
/* 2. 부피를 반환하세요. */
return this.length * this.length * this.length;
}
// 메소드(인스턴스 메소드) - 정육면체의 겉넓이 반환
public int surfaceArea() {
/* 3. 생성자를 완성하세요. */
return 6 * this.length * this.length;
}
}