반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 개발공부
- 백준단계별로풀어보기
- 빅오 표기법
- 브루트포스
- 알고리즘공부
- BFS
- 자바공부
- ★
- 백준자바
- 백준알고리즘
- 다이나믹 프로그래밍
- 알고리즘
- 시간 복잡도
- 자바의정석연습문제
- 자바개념
- Java개념
- java
- dp
- 백트래킹
- 자바
- 자바의정석연습문제풀이
- dfs
- 무료개발강의
- ☆
- 자바의정석
- 백준9단계
- 무료코딩강의
- 동적계획법
- 코딩공부
Archives
- Today
- Total
더 많이 실패하기
자바의정석 연습문제 풀이 7장 7-23 본문
반응형
자바의 정석 3판 연습문제 풀이
연습문제 파일은 아래 링크에서 다운받을 수 있다
GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition
soure codes and ppt files of javajungsuk 3rd edition - GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition
github.com
http://www.yes24.com/Product/Goods/24259565
Java의 정석 - YES24
최근 7년동안 자바 분야의 베스트 셀러 1위를 지켜온 `자바의 정석`의 최신판. 저자가 카페에서 12년간 직접 독자들에게 답변을 해오면서 초보자가 어려워하는 부분을 잘 파악하고 쓴 책. 뿐만 아
www.yes24.com
자바의 정석 저자 남궁성 님이 직접 올려주신 문제다
7장은 총 29문제로 구성되어 있다
7-23
전체 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Exercise7_22_23 {
static double sumArea(Shape[] arr) {
double sum = 0;
for(int i = 0; i < arr.length; i++) {
sum += arr[i].calcArea();
}
return sum;
}
public static void main(String[] args) {
Shape[] arr = {new Circle(5.0), new Rectangle(3,4), new Circle(1)};
System.out.println("면적의 합:"+sumArea(arr));
}
}
|
cs |
답 코드
1
2
3
4
5
6
7
8
9
10
|
static double sumArea(Shape[] arr) {
double sum = 0;
for(int i = 0; i < arr.length; i++) {
sum += arr[i].calcArea();
}
return sum;
}
|
cs |
7-22까지 포함한 전체 코드
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
public class Exercise7_22_23 {
static double sumArea(Shape[] arr) {
double sum = 0;
for(int i = 0; i < arr.length; i++) {
sum += arr[i].calcArea();
}
return sum;
}
public static void main(String[] args) {
Shape[] arr = {new Circle(5.0), new Rectangle(3,4), new Circle(1)};
System.out.println("면적의 합:"+sumArea(arr));
}
}
abstract class Shape{
Point p;
Shape() {
this(new Point(0,0));
}
Shape(Point p){
this.p = p;
}
abstract double calcArea(); //도형의 면적을 계산해서 반환하는 메서드
Point getPosition() {
return p;
}
void setPosition(Point p) {
this.p = p;
}
}
class Circle extends Shape{
double r;
Circle(double r) {
this(new Point(0,0), r); //Circle(Point p, double r)을 호출
}
Circle(Point p, double r){
super(p); //조상의 멤버는 조상의 생성자가 초기화
this.r = r;
}
double calcArea() {
return Math.PI * r * r;
}
}
class Rectangle extends Shape{
double width;
double height;
Rectangle(double width, double height) {
this(new Point(0,0), width, height);
}
Rectangle(Point p, double width, double height){
super(p);
this.width = width;
this.height = height;
}
boolean isSquare() {
return width*height != 0 && width == height;
}
double calcArea() {
return width * height;
}
}
class Point {
int x;
int y;
Point() {
this(0,0);
}
Point(int x, int y){
this.x = x;
this.y = y;
}
public String toString() {
return "["+x+", "+y+"]";
}
}
|
cs |
Shape타입 배열에는 Shape의 자손 인스턴스가 들어있기 때문에
Shape클래스의 추상메서드 calcArea()를 호출해도
각 인스턴스에 완전히 구현된 calcArea()가 호출된다
반응형
'자바 > 자바의정석-연습문제' 카테고리의 다른 글
자바의정석 연습문제 풀이 7장 7-22 (0) | 2022.10.14 |
---|---|
자바의정석 연습문제 풀이 7장 7-21 (0) | 2022.10.12 |
자바의정석 연습문제 풀이 7장 7-20 (0) | 2022.10.12 |
자바의정석 연습문제 풀이 7장 7-19 (0) | 2022.10.12 |
자바의정석 연습문제 풀이 7장 7-18 (0) | 2022.10.12 |
Comments