728x90
๋ช ํ ์๋ฐ ํ๋ก๊ทธ๋๋ฐ 4์ฅ 241~247์ชฝ ์ฐ์ต๋ฌธ์ ์ค์ต๋ฌธ์ ์ ๋ต
luxury java programming ch4 p241~247 exercise practice correct answer
1. ๋ต:
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
|
class TV { // TV ํด๋์ค ๊ตฌํ ์์
private int size; // private intํ ๋ฉค๋ฒ๋ณ์ size ์ ์ธ
private int year; // private intํ ๋ฉค๋ฒ๋ณ์ year ์ ์ธ
private String manufacturer; // private Stringํ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ ์ ์ธ
public TV() {} // public์ผ๋ก ์ ์ธ๋ ๊ธฐ๋ณธ์์ฑ์
public TV(String manufacturer, int year, int size) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ String, int, int 3๊ฐ์ธ ์์ฑ์
this.manufacturer = manufacturer; // ๋งค๊ฐ๋ณ์ manufacturer๋ฅผ ๋ฉค๋ฒ๋ณ์ manufacturer์ ๋์
์ฐ์ฐ
this.year = year; // ๋งค๊ฐ๋ณ์ year๋ฅผ ๋ฉค๋ฒ๋ณ์ year์ ๋์
์ฐ์ฐ
this.size = size; // ๋งค๊ฐ๋ณ์ size๋ฅผ ๋ฉค๋ฒ๋ณ์ size์ ๋์
์ฐ์ฐ
}
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํด๊ฐ์ด ์๋ ๋ฉค๋ฒํจ์ show() ์ ์ ์์
System.out.println(manufacturer + "์์ ๋ง๋ " + year + "๋
ํ " + size + "์ธ์น TV"); // ๋ฉค๋ฒ๋ณ์ manufacturer, year, size์ ์ ์ฅ๋ ๊ฐ ์ถ๋ ฅ
}
}
public class Ch4_1 { // public์ผ๋ก ์ ์ธ๋ Ch4_1 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
TV myTV = new TV("LG", 2017, 32); // TV ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ ํผ๋ฐ์ค ๋ณ์ myTV ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ String, int, int 3๊ฐ์ธ ์์ฑ์ ํธ์ถํ์ฌ ์ด๊ธฐํ
myTV.show(); // myTV ๊ฐ์ฒด์ ๋ฉค๋ฒํจ์ show() ํธ์ถํ์ฌ ์ถ๋ ฅ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
2. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Grade { // default๋ก ์ ์ธ๋ Grade ํด๋์ค ๊ตฌํ ์์
private int math; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ math ์ ์ธ
private int science; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ science ์ ์ธ
private int english; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ english ์ ์ธ
private int average; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ average ์ ์ธ
public Grade(int math, int science, int english) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ int, int, int 3๊ฐ์ธ ์์ฑ์
this.math = math; // ๋งค๊ฐ๋ณ์ math๋ฅผ ๊ฐ์ฒด์ ํ๋ math์ ๋์
์ฐ์ฐ
this.science = science; // ๋งค๊ฐ๋ณ์ science๋ฅผ ๊ฐ์ฒด์ ํ๋ science์ ๋์
์ฐ์ฐ
this.english = english; // ๋งค๊ฐ๋ณ์ english๋ฅผ ๊ฐ์ฒด์ ํ๋ english์ ๋์
์ฐ์ฐ
}
public int average() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํด๊ฐ์ด intํ์ธ average() ๋ฉ์๋ ์ ์ ์์
this.average = (math + + science + english)/3; //3๊ณผ๋ชฉ ํ๊ท ๊ณ์ฐํ์ฌ ๊ฐ์ฒด ํ๋ average์ ๋์
์ฐ์ฐ
return average; // ๊ฐ์ฒด ํ๋ average ๋ฆฌํด
}
}
public class Ch4_2 { // public์ผ๋ก ์ ์ธ๋ Ch4_2 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ๊ธฐ์ํด scanner ๊ฐ์ฒด ์์ฑ
System.out.print("์ํ, ๊ณผํ, ์์ด ์์ผ๋ก 3๊ฐ์ ์ ์ ์
๋ ฅ>>"); // ์ ์ ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
int math = scanner.nextInt(); // intํ ๋ณ์ math ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ก ์ด๊ธฐํ
int science = scanner.nextInt(); // intํ ๋ณ์ science ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ก ์ด๊ธฐํ
int english = scanner.nextInt(); // intํ ๋ณ์ english ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ก ์ด๊ธฐํ
Grade me = new Grade(math, science, english); // Grade ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ me ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ int, int, int 3๊ฐ์ธ ์์ฑ์ ํธ์ถํ์ฌ ์ด๊ธฐํ
System.out.println("ํ๊ท ์ " + me.average()); // ๊ฐ์ฒด me์ ๋ฉ์๋ average() ํธ์ถํ์ฌ ํ๊ท ์ถ๋ ฅ
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
3. ๋ต:
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
|
public class Song { // public์ผ๋ก ์ ์ธ๋ Song ํด๋์ค ๊ตฌํ ์์
private String title; // private์ผ๋ก ์ ์ธ๋ Stringํ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ํ๋ title ์ ์ธ
private String artist; // private์ผ๋ก ์ ์ธ๋ Stringํ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ํ๋ artist ์ ์ธ
private String country; // private์ผ๋ก ์ ์ธ๋ Stringํ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ํ๋ country ์ ์ธ
private int year; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ year ์ ์ธ
public Song() { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์
}
public Song(String title, String artist, String country, int year) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ String, String, String, int 4๊ฐ์ธ ์์ฑ์
this.title = title; // ๋งค๊ฐ๋ณ์ title์ ๊ฐ์ฒด ํ๋ title์ ๋์
์ฐ์ฐ
this.artist = artist; // ๋งค๊ฐ๋ณ์ artist์ ๊ฐ์ฒด ํ๋ artist์ ๋์
์ฐ์ฐ
this.country = country; // ๋งค๊ฐ๋ณ์ country์ ๊ฐ์ฒด ํ๋ country์ ๋์
์ฐ์ฐ
this.year = year; // ๋งค๊ฐ๋ณ์ year๋ฅผ ๊ฐ์ฒด ํ๋ year์ ๋์
์ฐ์ฐ
}
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ show() ๋ฉ์๋ ์ ์ ์์
System.out.println(year + "๋
" + country + "๊ตญ์ ์ " + artist + "๊ฐ ๋ถ๋ฅธ "+ title); // ๊ฐ์ฒด์ ํ๋ ์ถ๋ ฅ
}
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
Song dancingQueen = new Song("DancingQueen", "ABBA", "์ค์จ๋ด", 1978); // Song ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ dancingQueen ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ String, String, String, int 4๊ฐ์ธ ์์ฑ์ ํธ์ถํ์ฌ ์ด๊ธฐํ
dancingQueen.show(); // dancingQueen์ ๋ฉ์๋ show() ํธ์ถํ์ฌ ๊ฐ์ฒด ํ๋ ์ถ๋ ฅ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
4. ๋ต:
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
|
class Rectangle { // default๋ก ์ ์ธ๋ Rectangle ํด๋์ค ๊ตฌํ ์์
private int x; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ x ์ ์ธ, ์ผ์ชฝ ์๋ ์ x์ขํ
private int y; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ y ์ ์ธ, ์ผ์ชฝ ์๋ ์ y์ขํ
private int width; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ width ์ ์ธ, ๋๋น
private int height; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ width ์ ์ธ, ๋์ด
public Rectangle(int x, int y, int width, int height) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ int, int, int, int 4๊ฐ์ธ ์์ฑ์
this.x = x; // ๋งค๊ฐ๋ณ์ x๋ฅผ ๊ฐ์ฒด ํ๋ x์ ๋์
์ฐ์ฐ
this.y = y; // ๋งค๊ฐ๋ณ์ y๋ฅผ ๊ฐ์ฒด ํ๋ y์ ๋์
์ฐ์ฐ
this.width = width; // ๋งค๊ฐ๋ณ์ width๋ฅผ ๊ฐ์ฒด ํ๋ width์ ๋์
์ฐ์ฐ
this.height = height; // ๋งค๊ฐ๋ณ์ height๋ฅผ ๊ฐ์ฒด ํ๋ height์ ๋์
์ฐ์ฐ
}
public int square() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด int์ธ square() ๋ฉ์๋ ์ ์ ์์
return width*height; // ๋๋น*๋์ด = ์ฌ๊ฐํ์ ๋์ด ๋ฆฌํด
}
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ show() ๋ฉ์๋ ์ ์ ์์
System.out.println("(" + x + "," + y + ")์์ ํฌ๊ธฐ๊ฐ " + width + "x" + height + "์ธ ์ฌ๊ฐํ"); // ๊ฐ์ฒด ํ๋ ์ถ๋ ฅ
}
public boolean contains(Rectangle r) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด boolean์ธ contains() ๋ฉ์๋ ์ ์ ์์
if((x < r.x)&&(r.x < x+width)) { // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ r์ด ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฐ์ฒด์ ํ๋ x๊ฐ ์ฌ๊ฐํ์ ๊ฐ๋ก ๋ฒ์ ์์ ์์ผ๋ฉด
if((y < r.y)&&(r.y < y+height)) { // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ r์ด ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฐ์ฒด์ ํ๋ y๊ฐ ์ฌ๊ฐํ์ ์ธ๋ก ๋ฒ์ ์์ ์์ผ๋ฉด
if((x < r.x + r.width)&&(r.x + r.width< x+width)) { // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ r์ด ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฐ์ฒด์ ํ๋ x + width, ์ฆ ๋ค๋ฅธ ์ ์ x์ขํ๊ฐ ์ฌ๊ฐํ์ ๊ฐ๋ก ๋ฒ์ ์์ ์์ผ๋ฉด
if((y < r.y + r.height)&&(r.y + r.height < y+height)) { // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ r์ด ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฐ์ฒด์ ํ๋ y + height, ์ฆ ๋ค๋ฅธ ์ ์ y์ขํ๊ฐ ์ฌ๊ฐํ์ ์ธ๋ก ๋ฒ์ ์์ ์์ผ๋ฉด
return true; // ์ฌ๊ฐํ r์ ํฌํจํ๊ณ ์์ผ๋ฏ๋ก true๋ฅผ ๋ฆฌํด
}
}
}
}
return false; // ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ฆ ์ฌ๊ฐํ r์ ํฌํจํ๊ณ ์์ง ์์ผ๋ฉด false๋ฅผ ๋ฆฌํด
}
}
public class Ch4_4 { // public์ผ๋ก ์ ์ธ๋ Ch4_4 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // public์ผ๋ก ์ ์ธ๋ main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
Rectangle r = new Rectangle(2, 2, 8, 7); // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ r ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ intํ 4๊ฐ์ธ ์์ฑ์๊ฐ ํธ์ถ๋์ด ์ด๊ธฐํ
Rectangle s = new Rectangle(5, 5, 6, 6); // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ s ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ intํ 4๊ฐ์ธ ์์ฑ์๊ฐ ํธ์ถ๋์ด ์ด๊ธฐํ
Rectangle t = new Rectangle(1, 1, 10, 10); // Rectangle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ t ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ intํ 4๊ฐ์ธ ์์ฑ์๊ฐ ํธ์ถ๋์ด ์ด๊ธฐํ
r.show(); // ๊ฐ์ฒด r์ ๋ฉ์๋ show() ํธ์ถํ์ฌ ํ๋ ์ถ๋ ฅ
System.out.println("s์ ๋ฉด์ ์ " + s.square()); // ๊ฐ์ฒด s์ ๋ฉ์๋ square() ํธ์ถํ์ฌ ๋ฉด์ ์ถ๋ ฅ
if(t.contains(r)) System.out.println("t๋ r์ ํฌํจํฉ๋๋ค."); // ๋ง์ฝ t๊ฐ r์ ํฌํจํ๊ณ ์์ผ๋ฉด ์ถ๋ ฅ
if(t.contains(s)) System.out.println("t๋ s์ ํฌํจํฉ๋๋ค."); // ๋ง์ฝ t๊ฐ s์ ํฌํจํ๊ณ ์์ผ๋ฉด ์ถ๋ ฅ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
5. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Circle { // default๋ก ์ ์ธ๋ Circle ํด๋์ค ๊ตฌํ ์์
private double x, y; // private์ผ๋ก ์ ์ธ๋ doubleํ ํ๋ x, y ์ ์ธ
private int radius; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ radius ์ ์ธ
public Circle(double x, double y, int radius) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ double, double, int 3๊ฐ์ธ ์์ฑ์
this.x = x; // ๊ฐ์ฒด ํ๋ x์ ๋งค๊ฐ๋ณ์ x๋ฅผ ๋์
์ฐ์ฐ
this.y = y; // ๊ฐ์ฒด ํ๋ y์ ๋งค๊ฐ๋ณ์ y๋ฅผ ๋์
์ฐ์ฐ
this.radius = radius; // ๊ฐ์ฒด ํ๋ radius์ ๋งค๊ฐ๋ณ์ radius๋ฅผ ๋์
์ฐ์ฐ
}
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ show() ๋ฉ์๋ ์ ์ ์์
System.out.println("(" + x + "," + y +")" + radius); // ๊ฐ์ฒด ํ๋ ์ถ๋ ฅ
}
}
public class CircleManager { // public์ผ๋ก ์ ์ธ๋ CircleManager ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // public์ผ๋ก ์ ์ธ๋ main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ๊ธฐ์ํด Scanner ๊ฐ์ฒด ์์ฑ
Circle[] c = new Circle[3]; // Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ c ์ ์ธ ๋์์ ํฌ๊ธฐ๊ฐ 3์ธ ๋ ํผ๋ฐ์ค ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
for(int i = 0; i<c.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ c.length, ์ฆ c์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
System.out.print("x, y, radius >>"); // x, y, radius ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
double x = scanner.nextDouble(); // doubleํ ๋ณ์ y ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ค์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
double y = scanner.nextDouble(); // doubleํ ๋ณ์ y ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ค์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
int radius = scanner.nextInt(); // intํ ๋ณ์ radius ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ ์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
c[i] = new Circle(x, y, radius); // Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค๋ฅผ ์ ์ฅํ๋ c[i] ์์์ Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
}
for(int i = 0; i<c.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ c.length, ์ฆ ๋ฐฐ์ด c์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
c[i].show(); // c[i] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉ์๋ show() ํธ์ถํ์ฌ ํ๋ ์ถ๋ ฅ
}
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
6. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Circle { // default๋ก ์ ์ธ๋ Circle ํด๋์ค ๊ตฌํ ์์
private double x, y; // private์ผ๋ก ์ ์ธ๋ doubleํ ํ๋ x, y ์ ์ธ
private int radius; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ radius ์ ์ธ
private double area; // private์ผ๋ก ์ ์ธ๋ doubleํ ํ๋ area ์ ์ธ
public Circle(double x, double y, int radius) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ double, double, int 3๊ฐ์ธ ์์ฑ์
this.x = x; // ๊ฐ์ฒด ํ๋ x์ ๋งค๊ฐ๋ณ์ x๋ฅผ ๋์
์ฐ์ฐ
this.y = y; // ๊ฐ์ฒด ํ๋ y์ ๋งค๊ฐ๋ณ์ y๋ฅผ ๋์
์ฐ์ฐ
this.radius = radius; // ๊ฐ์ฒด ํ๋ radius์ ๋งค๊ฐ๋ณ์ radius๋ฅผ ๋์
์ฐ์ฐ
}
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ show() ๋ฉ์๋ ์ ์ ์์
System.out.println("(" + x + "," + y +")" + radius); // ๊ฐ์ฒด ํ๋ ์ถ๋ ฅ
}
public void setArea() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ setArea() ๋ฉ์๋ ์ ์ ์์
area = 3.14*radius*radius; // ์์ ๋ฉด์ ๊ณ์ฐํ์ฌ ๊ฐ์ฒด ํ๋ area์ ๋์
์ฐ์ฐ
}
public double getArea() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด double์ธ getArea() ๋ฉ์๋ ์ ์ ์์
return area; // ์์ ๋ฉด์ ์ด ์ ์ฅ๋ ๊ฐ์ฒด ํ๋ area์ ๋์
์ฐ์ฐ
}
public static int getMaxIndex(Circle[] c) { // public์ผ๋ก ์ ์ธ๋ static ๋ฆฌํดํ์
์ด int์ธ getMaxIndex() ๋ฉ์๋ ์ ์ ์์
int maxIndex = 0; // intํ ๋ณ์ maxIndex ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ
for(int i = 1; i<c.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 1๋ก ์ด๊ธฐํ, i๊ฐ c.length, ์ฆ ๋ฐฐ์ด c์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
if(c[i].getArea() > c[maxIndex].getArea()) { // ๋ง์ฝ c[i]์ ๋ฉด์ ์ด c[maxIndex]์ ๋ฉด์ ๋ณด๋ค ํฌ๋ฉด
maxIndex = i; // maxIndex์ i ๋์
์ฐ์ฐ
}
} // ๋ฐฐ์ด c ์ฒ์๋ถํฐ ๋๊น์ง ๊ฒ์ฌ ๋
return maxIndex; // maxIndex๋ฅผ ๋ฆฌํด
}
} // Circle ํด๋์ค ๊ตฌํ ๋
public class CircleManager { // public์ผ๋ก ์ ์ธ๋ CircleManager ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // public์ผ๋ก ์ ์ธ๋ main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ๊ธฐ์ํด Scanner ๊ฐ์ฒด ์์ฑ
Circle[] c = new Circle[3]; // Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ c ์ ์ธ ๋์์ ํฌ๊ธฐ๊ฐ 3์ธ ๋ ํผ๋ฐ์ค ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
for(int i = 0; i<c.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ c.length, ์ฆ c์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
System.out.print("x, y, radius >>"); // x, y, radius ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
double x = scanner.nextDouble(); // doubleํ ๋ณ์ y ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ค์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
double y = scanner.nextDouble(); // doubleํ ๋ณ์ y ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ค์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
int radius = scanner.nextInt(); // intํ ๋ณ์ radius ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ ์ ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
c[i] = new Circle(x, y, radius); // Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค๋ฅผ ์ ์ฅํ๋ c[i] ์์์ Circle ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
c[i].setArea(); // c[i] ์์์ ๋ฉ์๋ setArea() ํธ์ถํ์ฌ ๊ฐ์ฒด ํ๋ area ์ค์
}
System.out.print("๊ฐ์ฅ ๋ฉด์ ์ด ํฐ ์์ "); // ๊ฐ์ฅ ๋ฉด์ ์ด ํฐ ์ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
int maxIndex = Circle.getMaxIndex(c); // intํ ๋ณ์ maxIndex ์ ์ธ ๋์์ Circleํด๋์ค์ static ๋ฉ์๋ getMaxIndex() ํธ์ถํ์ฌ ๋ฐฐ์ด c์ ์์ ์ค ๊ฐ์ฅ ๋ฉด์ ์ด ํฐ ์์์ ์ธ๋ฑ์ค๋ก ์ด๊ธฐํ
c[maxIndex].show(); // c[maxIndex]์ ๋ฉ์๋ show() ํธ์ถํ์ฌ, ๊ฐ์ฅ ํฐ ๋ฉด์ ์ด ํฐ ์์ ํ๋ ์ถ๋ ฅ
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
7. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Day { // default๋ก ์ ์ธ๋ Day ํด๋์ค ๊ตฌํ ์์
private String work; // private์ผ๋ก ์ ์ธ๋ Stringํ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ work ์ ์ธ
public void set(String work) { this.work = work; } // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ ๋ฉ์๋ set() ์ ์, ๋งค๊ฐ๋ณ์ work๋ฅผ ํ๋ worrk์ ๋์
์ฐ์ฐ
public String get() { return work; } // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด String์ธ ๋ฉ์๋ get() ์ ์
public void show() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํฐํ์
์ด void์ธ ๋ฉ์๋ show() ์ ์
if(work == null) System.out.println("์์ต๋๋ค."); // ๋ง์ฝ work๊ฐ null์ด๋ฉด, ์ฆ work์ ์ ์ฅ๋ ๋ฌธ์์ด ๊ฐ์ฒด๊ฐ ์์ผ๋ฉด ์์ต๋๋ค. ๋ฉ์ธ์ง ์ถ๋ ฅ
else System.out.println(work + "์
๋๋ค."); // ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ฆ work์ ์ ์ฅ๋ ๋ฌธ์์ด ๊ฐ์ฒด๊ฐ ์์ผ๋ฉด ํ ์ผ ๋ฉ์ธ์ง ์ถ๋ ฅ
}
} // Day ํด๋์ค ๊ตฌํ ๋
class MonthSchedule { // default๋ก ์ ์ธ๋ MonthSchedule ๊ตฌํ ์์
private Scanner scanner; // private์ผ๋ก ์ ์ธ๋ Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ scanner ์ ์ธ
private Day[] month; // private์ผ๋ก ์ ์ธ๋ Day ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ month ์ ์ธ
public MonthSchedule(int size) { // public์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์๊ฐ int 1๊ฐ์ธ ์์ฑ์
month = new Day[size]; // ๋ ํผ๋ฐ์ค ๋ฐฐ์ด ๋ง๋ค์ด ๋ ํผ๋ฐ์ค ๋ณ์ month ์ด๊ธฐํ
for(int i = 0; i<month.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ month.length, ์ฆ ๋ฐฐ์ด month์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
month[i] = new Day(); // month[i] ์์์ Day ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋์
์ฐ์ฐํ์ฌ ์ด๊ธฐํ
}
scanner = new Scanner(System.in); //์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ๊ธฐ์ํด scanner ๊ฐ์ฒด ์์ฑ
}
public void input() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ input() ๋ฉ์๋ ์ ์, ํ ์ผ ์
๋ ฅ ๊ธฐ๋ฅ
System.out.print("๋ ์ง(1~30)>?"); // ๋ ์ง ์
๋ ฅ ๋ฉ์ธ์ง ์ถ๋ ฅ
int date = scanner.nextInt(); // intํ ๋ณ์ date ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ ์๋ฅผ ๋์
์ฐ์ฐ
System.out.print("ํ ์ผ(๋น์นธ์์ด์
๋ ฅ)?"); // ํ ์ผ ์
๋ ฅ ๋ฉ์ธ์ง ์ถ๋ ฅ
month[date].set(scanner.next()); // month[date] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉ์๋ set() ํธ์ถํ์ฌ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ฌธ์์ด ์ ์ฅ
}
public void view() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ view() ๋ฉ์๋ ์ ์, ์ ์ฅ๋ ํ ์ผ ์ถ๋ ฅ
System.out.print("๋ ์ง(1~30)>?"); // ๋ ์ง ์
๋ ฅ ๋ฉ์ธ์ง ์ถ๋ ฅ
int date = scanner.nextInt(); // intํ ๋ณ์ date ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ์ ์๋ฅผ ๋์
์ฐ์ฐ
System.out.print(date + "์ผ์ ํ ์ผ์ "); // ํ ์ผ ๋ฉ์ธ์ง ์ถ๋ ฅ
month[date].show(); // month[date] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉ์๋ show() ํธ์ถํ์ฌ ์ ์ฅ๋ ๋ฌธ์์ด์ด ์๋ค๋ฉด ์ถ๋ ฅ
}
public void finish() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ finish() ๋ฉ์๋ ์ ์ ์์
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
System.out.println("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค."); // ํ๋ก๊ทธ๋จ ์ข
๋ฃ ๋ฉ์ธ์ง ์ถ๋ ฅ
}
public void run() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ run() ๋ฉ์๋ ์ ์ ์์
System.out.println("์ด๋ฒ๋ฌ ์ค์ผ์ฅด ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ"); // ์ด๋ฒ๋ฌ ์ค์ผ์ฅด ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
int menu; // intํ ๋ณ์ menu ์ ์ธ
while(true) { // ๋ฌดํ๋ฐ๋ณต
System.out.print("ํ ์ผ(์
๋ ฅ:1, ๋ณด๊ธฐ:2, ๋๋ด๊ธฐ:3) >>"); // ๋ฉ๋ด ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
menu = scanner.nextInt(); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ฅผ menu์ ๋์
์ฐ์ฐ
switch(menu) { // menu๊ฐ
case 1: // 1์ธ ๊ฒฝ์ฐ
input(); // input() ๋ฉ์๋ ํธ์ถ
break; // switch-case๋ฌธ ๋น ์ ธ๋๊ฐ, ๋ค์ ๋ฐ๋ณต์ผ๋ก ๊ฐ
case 2: // 2์ธ ๊ฒฝ์ฐ
view(); // view() ๋ฉ์๋ ํธ์ถ
break; // switch-case๋ฌธ ๋น ์ ธ๋๊ฐ, ๋ค์ ๋ฐ๋ณต์ผ๋ก ๊ฐ
case 3: // 3์ธ ๊ฒฝ์ฐ
finish(); // finish() ๋ฉ์๋ ํธ์ถ
return; // run() ๋ฉ์๋ ํธ์ถ๋ ๊ณณ์ผ๋ก ๋๋์๊ฐ
} // switch-case๋ฌธ ๋
} // while๋ฌธ ๋
} // run() ๋ฉ์๋ ๋
} // MonthSchedule ํด๋์ค ๊ตฌํ ๋
public class Ch4_7 { // public์ผ๋ก ์ ์ธ๋ Ch4_7 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
MonthSchedule april = new MonthSchedule(30); // MonthSchedule ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ april ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ, ๋งค๊ฐ๋ณ์๊ฐ int 1๊ฐ์ธ ์์ฑ์ ํธ์ถํ์ฌ ์ด๊ธฐํ
april.run(); // april ๋ ํผ๋ฐ์ค ๋ณ์๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉ์๋ run() ํธ์ถ
} // main() ๋ฉ์๋ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
8. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Phone { // default๋ก ์ ์ธ๋ Phone ํด๋์ค ๊ตฌํ ์์
private String name; // private์ผ๋ก ์ ์ธ๋ String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ ํ๋ name ์ ์ธ
private String tel; // private์ผ๋ก ์ ์ธ๋ String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ ํ๋ tel ์ ์ธ
public Phone() { // public์ผ๋ก ์ ์ธ๋ ๊ธฐ๋ณธ ์์ฑ์
}
public void setName(String name) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ด๊ณ ๋งค๊ฐ๋ณ์๊ฐ String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ name์ธ setName() ๋ฉ์๋ ์ ์ ์์
this.name = name; // ๊ฐ์ฒด ํ๋ name์ ๋งค๊ฐ๋ณ์ name์ ๋์
์ฐ์ฐ, ๋ ํผ๋ฐ์ค๊ฐ ๋ณต์ฌ๋์ด ์ ๋ฌ๋๋ค.
}
public void setTel(String tel) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ด๊ณ ๋งค๊ฐ๋ณ์๊ฐ String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ tel์ธ setTel() ๋ฉ์๋ ์ ์ ์์
this.tel = tel; // ๊ฐ์ฒด ํ๋ tel์ ๋งค๊ฐ๋ณ์ tel์ ๋์
์ฐ์ฐ, ๋ ํผ๋ฐ์ค๊ฐ ๋ณต์ฌ๋์ด ์ ๋ฌ๋๋ค.
}
public String getName() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค์ธ getName() ๋ฉ์๋ ์ ์ ์์
return name; // ๊ฐ์ฒด ํ๋ name ๋ฆฌํด
}
public String getTel() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค์ธ getTel() ๋ฉ์๋ ์ ์ ์์
return tel; // ๊ฐ์ฒด ํ๋ tel ๋ฆฌํด
}
} // ํด๋์ค ๊ตฌํ ๋
class PhoneBook { // default๋ก ์ ์ธ๋ PhoneBook ํด๋์ค ๊ตฌํ ์์
private Scanner scanner; // private์ผ๋ก ์ ์ธ๋ Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์์ธ ํ๋ scanner ์ ์ธ
private Phone[] phoneArray; // private์ผ๋ก ์ ์ธ๋ Phone ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์์ธ ํ๋ phoneArray ์ ์ธ
public PhoneBook() { // public์ผ๋ก ์ ์ธ๋ ๊ธฐ๋ณธ ์์ฑ์
scanner = new Scanner(System.in); // scanner ํ๋์ Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
System.out.print("์ธ์์>>"); // ์ธ์์ ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
int size = scanner.nextInt(); // private์ผ๋ก ์ ์ธ๋ intํ ๋ณ์ size ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ก ์ด๊ธฐํ, ๊ฐ์ฒด ๋ฐฐ์ด์ ํฌ๊ธฐ
phoneArray = new Phone[size]; // phoneArray ํ๋์ ํฌ๊ธฐ๊ฐ size์ธ Phone ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ฐฐ์ด ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
for(int i = 0; i<phoneArray.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ phoneArray.length, ๋ฐฐ์ด phoneArray์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
phoneArray[i] = new Phone(); // phoneArray[i] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค์ Phone ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ์์ฑํ์ฌ ๋์
์ฐ์ฐ
System.out.print("์ด๋ฆ๊ณผ ์ ํ๋ฒํธ(์ด๋ฆ๊ณผ ๋ฒํธ๋ ๋น ์นธ์์ด ์
๋ ฅ)>>"); // ์ด๋ฆ๊ณผ ์ ํ๋ฒํธ ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
phoneArray[i].setName(scanner.next()); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ phoneArray[i] ์์์ ๋ฉ์๋ setName() ํธ์ถํ์ฌ name ํ๋์ ์ ์ฅ
phoneArray[i].setTel(scanner.next()); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ phoneArray[i] ์์์ ๋ฉ์๋ setTel() ํธ์ถํ์ฌ tel ํ๋์ ์ ์ฅ
}
System.out.println("์ ์ฅ๋์์ต๋๋ค..."); // ์ ์ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
}
public void run() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ ๋ฉ์๋ run() ์ ์ ์์
Boolean exist = false; // Booleanํ ๋ณ์ exist ์ ์ธ ๋์์ ์ด๊ธฐํ
while(true) { // ๋ฌดํ๋ฐ๋ณต
System.out.print("๊ฒ์ํ ์ด๋ฆ>>"); // ๊ฒ์ํ ์ด๋ฆ ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
String name = scanner.next(); // String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ name ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด๋ก ์ด๊ธฐํ
if(name.equals("๊ทธ๋ง")) { // ๋ง์ฝ name์ด ๊ทธ๋ง์ด๋ฉด, ์ฆ ์ฌ์ฉ์๊ฐ ๊ทธ๋ง์ ์
๋ ฅํ์ผ๋ฉด
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
return; // run() ๋ฉ์๋ ํธ์ถํ ๊ณณ์ผ๋ก ๋์๊ฐ๋ค.
}
for(int i = 0; i<phoneArray.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ phoneArray.length ์ฆ phoneArray์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
if(name.equals(phoneArray[i].getName())) { // ๋ง์ฝ name์ด phoneArray[i] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ํ๋ name๊ณผ ๊ฐ์ผ๋ฉด
exist = true; // exist์ true ๋์
์ฐ์ฐ
System.out.print( name + "์ ๋ฒํธ๋ "); // name์ ๋ฒํธ ์ถ๋ ฅ ์๋ด ๋ฉ์ธ์ง
System.out.println(phoneArray[i].getTel() + " ์
๋๋ค."); // phoneArray[i] ์์์ ์ ์ฅ๋ ๋ ํผ๋ฐ์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉ์๋ getTel()์ ํธ์ถํ์ฌ ํ๋ tel ์ถ๋ ฅ
}
}
if(exist == false) { // ๋ง์ฝ exist๊ฐ false์ด๋ฉด, ์ฆ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ name์ด phoneArray์ ์์ผ๋ฉด
System.out.println( name + " ์ด ์์ต๋๋ค."); // ์์ต๋๋ค. ์ถ๋ ฅ
}
exist = false; // exist์ ์ ์ฅ๋ true๋ฅผ false๋ก ๋ฐ๊พผ๋ค. ์ด๊ธฐํ ์์
}
}
}
public class Ch4_8 { // public์ผ๋ก ์ ์ธ๋ Ch4_8 ํด๋์ค ๊ตฌํ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
PhoneBook phoneBook = new PhoneBook(); // PhoneBookํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ phoneBook ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
phoneBook.run(); // phoneBook ๊ฐ์ฒด์ ๋ฉ์๋ run() ํธ์ถ
} // main() ๋ฉ์๋ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
9. ๋ต:
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
|
class ArrayUtil { // default๋ก ์ ์ธํ ArrayUtil ํด๋์ค ๊ตฌํ ์์
public static int[] concat(int[] a, int[] b) { // public์ผ๋ก ์ ์ธ๋ static๋ฉค๋ฒ ๋ฆฌํดํ์
์ด intํ ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค์ธ concat() ๋ฉ์๋ ์ ์ ์์
int[] temp = new int[a.length +b.length]; // intํ ๋ฐฐ์ด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ temp ์ ์ธ ๋์์ ํฌ๊ธฐ๊ฐ a.length+b.length, ์ฆ ๋ฐฐ์ด a, b์ ํฌ๊ธฐ๋ฅผ ํฉํ ํฌ๊ธฐ์ ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
int index; // intํ ๋ณ์ index ์ ์ธ
for(int i = 0; i<a.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ a.length ์ฆ a์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
index = i; // index์ i ๋์
์ฐ์ฐ
temp[index] = a[i]; // temp[index] ์์์ a[i] ์์ ๋์
์ฐ์ฐ
}
for(int i = 0; i<b.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ b.length ์ฆ b์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
index = a.length + i; // index์ a.length+i ๋์
์ฐ์ฐ
temp[index] = b[i]; // temp[index] ์์์ b[i] ์์ ๋์
์ฐ์ฐ
}
return temp; // ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ temp ๋ฆฌํด
}
public static void print(int[] a) { // public์ผ๋ก ์ ์ธ๋ static ๋ฉค๋ฒ ๋ฆฌํดํ์
์ด void์ธ print() ๋ฉ์๋ ์ ์ ์์
System.out.print("[ "); // [ ์ถ๋ ฅ
for(int i = 0; i<a.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ, i๊ฐ a.length ์ฆ ๋ฐฐ์ด a์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
System.out.print(a[i] + " "); // a[i] ์์ ์ถ๋ ฅ
}
System.out.println("]"); // ] ์ถ๋ ฅ
}
} // ํด๋์ค ๊ตฌํ ๋
public class StaticExd { // public์ผ๋ก ์ ์ธ๋ StaticEx ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
int[] array1 = {1, 5, 7, 9}; // intํ ๋ฐฐ์ด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ array1 ์ ์ธ ๋์์ ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
int[] array2 = {3, 6, -1, 100, 77}; // intํ ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ array2 ์ ์ธ ๋์์ ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
int[] array3 = ArrayUtil.concat(array1, array2); // intํ ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ array3 ์ ์ธ ๋์์ ArrayUtil ํด๋์ค์ static ๋ฉ์๋ concat() ํธ์ถํ์ฌ array1, array2 ํฉ์น ๋ฐฐ์ด ๋ฆฌํดํ์ฌ ์ด๊ธฐํ
ArrayUtil.print(array3); // ArrayUtil ํด๋์ค์ static ๋ฉค๋ฒ ๋ฉ์๋ print() ํธ์ถํ์ฌ array3 ๋ฐฐ์ด ์ถ๋ ฅ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
10. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Dictionary { // default๋ก ์ ์ธ๋ ํด๋์ค Dictionary ๊ตฌํ ์์
private static String[] kor = { "์ฌ๋", "์๊ธฐ", "๋", "๋ฏธ๋", "ํฌ๋ง" }; // private์ผ๋ก ์ ์ธ๋ static ๋ฉค๋ฒ Stringํ ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค kor ์ ์ธ ๋์์ ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
private static String[] eng = {"love", "baby", "money", "future", "hope" }; // private์ผ๋ก ์ ์ธ๋ static ๋ฉค๋ฒ Stringํ ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ํ ๋ ํผ๋ฐ์ค kor ์ ์ธ ๋์์ ๋ฐฐ์ด ์์ฑํ์ฌ ์ด๊ธฐํ
public static String kor2Eng(String word) { // public์ผ๋ก ์ ์ธ๋ static ๋ฉค๋ฒ ๋ฆฌํดํ์
์ด Stringํ ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์์ธ kor2Eng() ๋ฉ์๋ ์ ์ ์์
String answer = ""; // Stringํ ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ answer ์ ์ธ ๋์์ "" ๋์
์ฐ์ฐ
Boolean exist = false; // Booleanํ ๋ณ์ exist ์ ์ธ ๋์์ false ๋์
์ฐ์ฐ
for(int i = 0; i<kor.length; i++) { // intํ ๋ณ์ i ์ ์ธ ๋์์ 0์ผ๋ก ์ ์ธ, i๊ฐ kor.length ์ฃฝ kor ๋ฐฐ์ด์ ํฌ๊ธฐ๋ณด๋ค ์์๋์ ๋ฐ๋ณต, i์ +1
if(word.equals(kor[i])) { // ๋ง์ฝ word๊ฐ kor[i] ์์์ ๊ฐ์ผ๋ฉด
exist = true; // exist์ true ๋์
์ฐ์ฐ
answer = eng[i]; // answer์ eng[i] ๋์
์ฐ์ฐ
break; // ์์ด๋จ์ด ์ฐพ๋ for๋ฌธ ๋น ์ ธ๋๊ฐ
}
}
if(exist == false) { // ๋ง์ฝ exist๊ฐ false์ด๋ฉด
System.out.println(word + "๋ ์ ์ ์ฌ์ ์ ์์ต๋๋ค."); // ์ฌ์ ์ ์์ต๋๋ค. ์ถ๋ ฅ
}
else { // ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ฆ exist๊ฐ true์ด๋ฉด
System.out.println(word + "์ " + answer); // ํ๊ธ๋จ์ด์ ๋ง๋ ์์ด๋จ์ด ์ถ๋ ฅ
}
exist = false; // exist๋ false๋ก ์ด๊ธฐํ
return answer; // answer ๋ฆฌํด
}
} // ํด๋์ค ๊ตฌํ ๋
class DicApp { // default๋ก ์ ์ธ๋ DicApp ํด๋์ค ๊ตฌํ ์์
private Scanner scanner; // private์ผ๋ก ์ ์ธ๋ Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ํ๋ scanner ์ ์ธ
private String search; // private์ผ๋ก ์ ์ธ๋ String ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ํ๋ search ์ ์ธ
public DicApp() { // public์ผ๋ก ์ ์ธ๋ ๊ธฐ๋ณธ ์์ฑ์
scanner = new Scanner(System.in); // scanner์ Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด ์์ฑํ์ฌ ๋์
์ฐ์ฐ
}
public void run() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ run() ๋ฉ์๋ ์ ์ ์์
System.out.println("ํ์ ๋จ์ด ๊ฒ์ ํ๋ก๊ทธ๋จ์
๋๋ค."); // ํ์ ๋จ์ด ๊ฒ์ ํ๋ก๊ทธ๋จ ์
๋๋ค. ์ถ๋ ฅ
while(true) { // ๋ฌดํ๋ฐ๋ณต
System.out.print("ํ๊ธ ๋จ์ด?"); // ํ๊ธ ๋จ์ด ์
๋ ฅ ๋ฉ์ธ์ง ์ถ๋ ฅ
search = scanner.next(); // ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ search์ ๋์
์ฐ์ฐ
if(search.equals("๊ทธ๋ง")) { // ๋ง์ฝ search๊ฐ ๊ทธ๋ง์ด๋ฉด, ์ฆ ์ฌ์ฉ์๊ฐ ๊ทธ๋ง์ ์
๋ ฅํ์ผ๋ฉด
break; // ๋จ์ด ์ฐพ๋ ๋ฐ๋ณต๋ฌธ while๋ฌธ์ ๋น ์ ธ๋๊ฐ
}
Dictionary.kor2Eng(search); // Dictionary ํด๋์ค์ static ๋ฉ์๋ kor2Eng() ํธ์ถํ์ฌ search์ ์ ์ฅ๋ ๋จ์ด ์ฐพ๊ธฐ
}
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
return; // ํจ์ ํธ์ถํ ๊ณณ์ผ๋ก ๋ค์ ๋๋์๊ฐ
}
}
public class Ch4_10 { // public์ผ๋ก ์ ์ธ๋ Ch4_10 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
DicApp dicApp = new DicApp(); // DicApp ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ dicApp ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
dicApp.run(); // dicApp ๊ฐ์ฒด ๋ฉ์๋ run() ํธ์ถ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
11. ๋ต:
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
|
import java.util.Scanner; // Scanner ํด๋์ค ์ฌ์ฉํ๊ธฐ์ํด ์ปดํ์ผ๋ฌ์๊ฒ ๊ฒฝ๋ก๋ช
์๋ ค์ค
class Add { // default๋ก ์ ์ธ๋ Add ํด๋์ค ๊ตฌํ ์์
private int a, b; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ a, b ์ ์ธ
public void setValue(int a, int b) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ setVaule() ๋ฉ์๋ ์ ์ ์์
this.a = a; // ํ๋ a์ ๋งค๊ฐ๋ณ์ a๋ฅผ ๋์
์ฐ์ฐ
this.b = b; // ํ๋ b์ ๋งค๊ฐ๋ณ์ b๋ฅผ ๋์
์ฐ์ฐ
}
public int calculate() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด int์ธ calculate() ๋ฉ์๋ ์ ์ ์์
return a+b; // a+b ๋ฆฌํด
}
}
class Sub { // default๋ก ์ ์ธ๋ Sub ํด๋์ค ๊ตฌํ ์์
private int a, b; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ a, b ์ ์ธ
public void setValue(int a, int b) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ setVaule() ๋ฉ์๋ ์ ์ ์์
this.a = a; // ํ๋ a์ ๋งค๊ฐ๋ณ์ a๋ฅผ ๋์
์ฐ์ฐ
this.b = b; // ํ๋ b์ ๋งค๊ฐ๋ณ์ b๋ฅผ ๋์
์ฐ์ฐ
}
public int calculate() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด int์ธ calculate() ๋ฉ์๋ ์ ์ ์์
return a-b; // a-b ๋ฆฌํด
}
}
class Mul { // default๋ก ์ ์ธ๋ Mul ํด๋์ค ๊ตฌํ ์์
private int a, b; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ a, b ์ ์ธ
public void setValue(int a, int b) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ setVaule() ๋ฉ์๋ ์ ์ ์์
this.a = a; // ํ๋ a์ ๋งค๊ฐ๋ณ์ a๋ฅผ ๋์
์ฐ์ฐ
this.b = b; // ํ๋ b์ ๋งค๊ฐ๋ณ์ b๋ฅผ ๋์
์ฐ์ฐ
}
public int calculate() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด int์ธ calculate() ๋ฉ์๋ ์ ์ ์์
return a*b; // a*b ๋ฆฌํด
}
}
class Div { // default๋ก ์ ์ธ๋ Sub ํด๋์ค ๊ตฌํ ์์
private int a, b; // private์ผ๋ก ์ ์ธ๋ intํ ํ๋ a, b ์ ์ธ
public void setValue(int a, int b) { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด void์ธ setVaule() ๋ฉ์๋ ์ ์ ์์
this.a = a; // ํ๋ a์ ๋งค๊ฐ๋ณ์ a๋ฅผ ๋์
์ฐ์ฐ
this.b = b; // ํ๋ b์ ๋งค๊ฐ๋ณ์ b๋ฅผ ๋์
์ฐ์ฐ
}
public int calculate() { // public์ผ๋ก ์ ์ธ๋ ๋ฆฌํดํ์
์ด int์ธ calculate() ๋ฉ์๋ ์ ์ ์์
if(b==0) { // ๋ง์ฝ ๋๋๋ ์๊ฐ 0์ด๋ฉด
System.out.println("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค."); // 0์ผ๋ก ๋๋์ ์์ต๋๋ค. ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
return -1; // -1 ๋ฆฌํด
}
return a/b; // a*b ๋ฆฌํด
}
}
public class Ch4_11 { // public์ผ๋ก ์ ์ธ๋ Ch4_11 ํด๋์ค ๊ตฌํ ์์
public static void main(String[] args) { // main() ๋ฉ์๋ ์ ์ ์์
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in); // Scanner ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ scanner ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
System.out.print("๋ ์ ์์ ์ฐ์ฐ์๋ฅผ ์
๋ ฅํ์์ค>>"); // ์
๋ ฅ ์๋ด ๋ฉ์ธ์ง ์ถ๋ ฅ
int a = scanner.nextInt(); // intํ ๋ณ์ a ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ฅผ ๋์
์ฐ์ฐ
int b = scanner.nextInt(); // intํ ๋ณ์ b ์ ์ธ ๋์์ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ์ ์๋ฅผ ๋์
์ฐ์ฐ
char operator = scanner.next().charAt(0); // charํ ๋ณ์ operator ์ ์ธ ๋์์ charAt() ๋ฉ์๋ ํธ์ถํ์ฌ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด์ 0๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฌธ์ํ ๋ณ์๋ก ๋ณํํ ๋ค ๋์
์ฐ์ฐ
int answer = 0; // intํ ๋ณ์ answer ์ ์ธ ๋์์ 0์ผ๋ก ์ด๊ธฐํ
switch(operator) { // operator์ ์ ์ฅ๋ ๋ฌธ์์ด์ด
case '+': // +์ธ ๊ฒฝ์ฐ
Add add = new Add(); // Add ํด๋์ค๋ก ๋ง๋ ๊ฐ์ฒด์ ๋ํ ๋ ํผ๋ฐ์ค ๋ณ์ add ์ ์ธ ๋์์ ๊ฐ์ฒด ์์ฑํ์ฌ ์ด๊ธฐํ
add.setValue(a, b); // add์ ๋ฉ์๋ setValue() ํธ์ถํ์ฌ ๊ฐํ ํ๋ a, b ์ค์
answer = add.calculate(); // add์ ๋ฉ์๋ calculate() ํธ์ถํ์ฌ ๊ณ์ฐ๋์ด ๋ฆฌํด๋ ๊ฐ์ answer์ ๋์
์ฐ์ฐ
break; // switch-case๋ฌธ ๋น ์ ธ๋๊ฐ
case '-':
Sub sub = new Sub();
sub.setValue(a, b);
answer = sub.calculate();
break;
case '*':
Mul mul = new Mul();
mul.setValue(a, b);
answer = mul.calculate();
break;
case '/':
Div div = new Div();
div.setValue(a, b);
answer = div.calculate();
break;
}
System.out.println(answer); // answer์ ์ ์ฅ๋ ๊ฐ ์ถ๋ ฅ
scanner.close(); // scanner ๊ฐ์ฒด ๋ซ๊ธฐ
} // main() ๋ฉ์๋ ์ ์ ๋
} // ํด๋์ค ๊ตฌํ ๋
|
cs |
12. ๋ต:
728x90
'1. Java ์๋ฐ > 1_0. ์ฑ , ๊ฐ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐํ๋ก๊ทธ๋๋ฐ1] 2์ฃผ์ฐจ ๊ฐ์_์บก์ฒ (0) | 2024.12.04 |
---|---|
[์๋ฐํ๋ก๊ทธ๋๋ฐ1] 1์ฃผ์ฐจ ๊ฐ์_์บก์ฒ (0) | 2024.12.04 |
๋ช ํ ์๋ฐ ํ๋ก๊ทธ๋๋ฐ 10์ฅ 556์ชฝ ์ฒดํฌ ํ์ ์ ๋ต (0) | 2024.03.27 |
๋ช ํ ์๋ฐ ํ๋ก๊ทธ๋๋ฐ 10์ฅ 553์ชฝ ์ฒดํฌ ํ์ ์ ๋ต (0) | 2024.03.27 |
๋ช ํ ์๋ฐ ํ๋ก๊ทธ๋๋ฐ 9์ฅ 525์ชฝ ์ฒดํฌ ํ์ ์ ๋ต (0) | 2024.03.27 |