반응형
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
- 백준자바
- 시간 복잡도
- 백준9단계
- 빅오 표기법
- 백준단계별로풀어보기
- 자바
- 자바의정석연습문제풀이
- dp
- java
- 코딩공부
- 자바공부
- 다이나믹 프로그래밍
- 자바의정석연습문제
- 동적계획법
- Java개념
- 백트래킹
- 무료개발강의
- BFS
- 알고리즘
- 브루트포스
- 백준알고리즘
- 개발공부
- 백준
- 자바의정석
- 자바개념
- ☆
- 무료코딩강의
- 알고리즘공부
- dfs
- ★
Archives
- Today
- Total
더 많이 실패하기
자바의정석 연습문제 풀이 7장 7-19 본문
반응형
자바의 정석 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-19
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
|
public class Exercise7_19 {
public static void main(String[] args) {
Buyer b = new Buyer();
b.buy(new Tv());
b.buy(new Computer());
b.buy(new Tv());
b.buy(new Audio());
b.buy(new Computer());
b.buy(new Computer());
b.buy(new Computer());
b.summary();
}
}
class Buyer {
int money = 1000;
Product[] cart = new Product[3];
int i = 0;
void buy(Product p) {
if(money < p.price) {
System.out.println("잔액이 부족하여"+p+"을/를 살 수 없습니다.");
return;
}
money -= p.price; //- 안되고 -=
add(p); //add()가 아니라 add(p) (매개변수 있는 메서드)
}
void add(Product p) {
if(i >= cart.length) {
Product[] newCart = new Product[cart.length * 2];
// newCart = cart.clone(); 실패, 아래같이 써야 함
System.arraycopy(cart, 0, newCart, 0, cart.length);
cart = newCart;
}
// for(int i = 0; i < cart.length; i++) {
// cart[i] = p;
// } //이렇게 말고 아래처럼
cart[i++] = p;
}
void summary() {
String itemList = ""; //
int sum = 0;
for(int i = 0; i < cart.length; i++) {
if(cart[i] == null)
break;
itemList += cart[i] + ",";
sum += cart[i].price;
}
System.out.println("구입한 물건:"+itemList);
System.out.println("사용한 금액:"+sum);
System.out.println("남은 금액:"+money);
}
}
class Product {
int price;
Product(int price){
this.price = price;
}
}
class Tv extends Product {
Tv() {super(100);}
public String toString() {return "Tv";}
}
class Computer extends Product {
Computer() {super(200);}
public String toString() {return "Computer";}
}
class Audio extends Product {
Audio() {super(50);}
public String toString() {return "Audio";}
}
|
cs |
책에도 있는 문제
잘못쓴 부분이 꽤 있었다
반응형
'자바 > 자바의정석-연습문제' 카테고리의 다른 글
자바의정석 연습문제 풀이 7장 7-21 (0) | 2022.10.12 |
---|---|
자바의정석 연습문제 풀이 7장 7-20 (0) | 2022.10.12 |
자바의정석 연습문제 풀이 7장 7-18 (0) | 2022.10.12 |
자바의정석 연습문제 풀이 7장 7-17 (0) | 2022.10.10 |
자바의정석 연습문제 풀이 7장 7-16 (0) | 2022.10.10 |
Comments