반응형
안녕하세요. 명품 자바 4판 4장 연습문제 - 실습문제입니다.
주관적으로 푼 풀이입니다.
1. 지문 생략
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class TV{
private String name = null;
private int year = 0;
private int inch = 0;
TV() {} // 생략 가능
TV(String name, int year, int inch) {
this.name = name;
this.year = year;
this.inch = inch;
}
void show() {
System.out.println(name + "에서 만든 " + year +"년형 " + inch + "인치" + " TV");
}
}
public class num_1 {
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32);
myTV.show();
}
}
|
2. 3과목의 점수를 입력받아 Grade 객체를 생성하고 성적 평균을 출력하는 main()~ 이후 생략
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
|
import java.util.Scanner;
class Grade{
private int math = 0;
private int science = 0;
private int english = 0;
private int avg =0;
public Grade(){}
public Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
public int average() {
avg = (math + science + english)/3;
return avg;
}
}
public class num_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
int math = sc.nextInt();
int science = sc.nextInt();
int english = sc.nextInt();
Grade me = new Grade(math, science, english);
System.out.println("평균은 " + me.average());
sc.close();
}
}
|
3. 노래 한 곡을 나타내는 Song 클래스를 작성하라.
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
|
class Song{
private String title = null;
private String artist = null;
private int year = 0;
private String country = null;
Song(){}
Song(String title, String artist, int year, String country){
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
public void show() {
System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
}
}
public class num_3 {
public static void main(String[] args) {
Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
song.show();
}
}
|
4. 다음 멤버를 가지고 직사각형을 표현하는 Rectangle 클래스를 작성하라.
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
|
class Rectangle{
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
public Rectangle(){}
public Rectangle(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int square() {
return width * height;
}
public void show() {
System.out.printf("(%d,%d)에서 크기가 %dx%d인 사각형 \n", x, y, width, height);
}
public boolean contains (Rectangle r) {
// x,y 좌표가 r의 좌표에 포함 되는지 그리고(and) 가로 세로의 값에 x,y 좌표 값을 더한 것 보다 큰 경우. 포함된다.
if ((x < r.x) && (y < r.y) && ( x + width > r.x + r.width && y + height > r.y + r.height ))
return true;
else
return false;
}
}
public class num_4 {
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 2, 8, 7);
Rectangle s = new Rectangle(5, 5, 6, 6);
Rectangle t = new Rectangle(1, 1, 10, 10);
r.show();
System.out.println("s의 면적은 " + s.square());
if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
if(t.contains(s)) System.out.println("t는 s을 포함합니다.");
}
}
|
5. 다음 설명대로 Circle 클래스와 CircleManager 클래스를 완성하라.
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
|
import java.util.Scanner;
class Circle {
private double x=0;
private double y=0;
private int radius=0;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void show() {
System.out.printf("(%.1f,%.1f)%d\n", x, y, radius);
}
}
public class num_5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle c[] = new Circle[3];
for(int i=0 ; i<c.length ; i++) {
System.out.print("x, y, radius >>");
double x = sc.nextDouble();
double y = sc.nextDouble();
int radius = sc.nextInt();
c[i] = new Circle(x, y, radius);
}
for(int i=0; i<c.length;i++) {
c[i].show();
}
sc.close();
}
}
|
문제에서는 Main()클래스를 CircleManager로 생성하라 했습니다.
저는 편의상 단원의 번호별로 보기 위해서 num_n으로 클래스를 작성했습니다.
6. 앞의 5번 문제는 정답이 공개되어 있다. 이 정답을 참고하여 Circle 클래스와 CircleManager 클래스를 수정하여 다음 실행 결과처럼 되게 하라.
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
|
import java.util.Scanner;
class Circle {
private double x=0;
private double y=0;
private int radius=0;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public int getR(){
return radius;
}
public void show() {
System.out.printf("가장 면적이 큰 원은 (%.1f,%.1f)%d\n", x, y, radius);
}
}
public class num_6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle c[] = new Circle[3];
int max =0;
for(int i=0 ; i<c.length ; i++) {
System.out.print("x, y, radius >>");
double x = sc.nextDouble();
double y = sc.nextDouble();
int radius = sc.nextInt();
c[i] = new Circle(x, y, radius);
}
for(int i=0; i<c.length;i++) {
if(c[max].getR()<c[i].getR())
max = i;
}
c[max].show();
sc.close();
}
}
|
7. 하루의 할 일을 표현하는 클래스 Day는 다음과 같다. 한 달의 할 일을 표현하는 MonthSchedule 클래스를 작성하라.
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
|
import java.util.Scanner;
class Day{
private String work = null;
public String get() {return work;}
public void set(String work){this.work = work;}
public void show() {
if(work==null)
System.out.println("없습니다.");
else
System.out.println("있습니다.");
}
}
public class num_7 {
Scanner sc = new Scanner(System.in);
Day day[] = null;
boolean isDay = true;
int dayNum = 0;
public num_7(int days) {
dayNum = days;
day = new Day[dayNum];
for(int i=0; i<day.length;i++) {
day[i] = new Day();
}
}
public void input() {
System.out.print("날짜(1~30)?");
int days = sc.nextInt();
System.out.print("할일(빈칸없이입력)?");
String work = sc.next();
day[days-1].set(work);
}
public void view() {
System.out.print("날짜(1~30)");
int days = sc.nextInt();
System.out.println(days+"일의 할 일은 " + day[days-1].get() + "입니다.");
}
public void finish() {
System.out.println("프로그램을 종료합니다.");
isDay = false;
}
public void run() {
System.out.println("이번달 스케쥴 관리 프로그램.");
while(isDay) {
System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
int choice = sc.nextInt();
switch(choice) {
case 1:
input();
break;
case 2:
view();
break;
case 3:
finish();
break;
default:
System.out.println("1~3 값을 입력해주세요");
}
System.out.println();
}
}
public static void main(String[] args) {
num_7 april = new num_7(30);
april.run();
}
}
|
8. 이름(name), 전화번호(tel) 필드와 생성자 등을 가진 Phone 클래스를 작성하고, 실행 예시와 같이 작동하는 PhoneBook 클래스를 작성하라.
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
|
import java.util.Scanner;
class Phone {
private String name = null;
private String tel = null;
public Phone() {}
public Phone(String name, String tel) {
this.name = name;
this.tel = tel;
}
public String getName() {return name;}
public String getTel() {return tel;}
}
public class num_8 {
Scanner sc = new Scanner(System.in);
Phone phone[];
int number = 0;
public num_8(){
System.out.print("인원수>>");
number = sc.nextInt();
phone = new Phone[number];
}
public void input() {
for(int i=0; i<phone.length;i++) {
System.out.print("이름과 번호는 빈 칸없이 입력)>>");
String text = sc.next();
String tel = sc.next();
phone[i] = new Phone(text, tel);
}
System.out.println("저장되었습니다...");
}
public void search() {
while(true) {
boolean op = false;
boolean op2 = false;
System.out.print("검색할 이름>>");
String name = sc.next();
if(name.equals("그만"))
break;
for(int i=0; i<phone.length; i++) {
if(name.equals(phone[i].getName())) {
System.out.println(name + "의 번호는 " + phone[i].getTel() + "입니다.");
op = true;
}
}
if(op == false) {
System.out.println(name + " 이 없습니다.");
}
}
}
public static void main(String[] args) {
num_8 Num_8 = new num_8();
Num_8.input();
Num_8.search();
}
}
|
9. 다음 2개의 static 가진 ArrayUil 클래스를 만들어보자. 다음 코드의 실행 결과를 참고하여 concat()와 print()를 작성하여 ArrayUil 클래스를 완성하라.
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 ArrayUtil{
public static int[] concat(int a[], int b[]) {
int array[] = new int[a.length+b.length];
for(int i=0;i<a.length;i++)
array[i] = a[i];
for(int i=0;i<b.length;i++)
array[i+a.length] = b[i];
return array;
}
public static void print(int a[]) {
System.out.print("[ ");
for(int i =0; i<a.length;i++) {
System.out.print(a[i] + " ");
}
System.out.println("]");
}
}
public class num_9 {
public static void main(String[] args) {
int array1[] = {1, 5, 7, 9};
int array2[] = {3, 6, -1, 100, 77};
int array3[] = ArrayUtil.concat(array1, array2);
ArrayUtil.print(array3);
}
}
|
10. 다음과 같은 Dictionary 클래스가 있다. 실행 결과와 같이 작동하도록 Dictionary 클래스의 kor2Eng() 메소드와 DicApp 클래스를 작성하라.
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
|
import java.util.Scanner;
class Dictionary{
private static String [] kor = {"사랑", "아기", "돈", "미래", "희망"};
private static String [] eng = {"love", "baby", "money", "future", "hope"};
public static String kor2Eng(String word) {
for(int i=0;i<kor.length;i++) {
if (word.equals(kor[i]))
return eng[i];
}
return null;
}
}
public class num_10 {
public num_10(){
System.out.println("한영 단어 검색 프로그램입니다.");
}
public void run() {
Scanner sc = new Scanner(System.in);
Dictionary dt = new Dictionary();
while(true) {
System.out.print("한글 단어?");
String text = sc.next();
if(text.equals("그만"))
break;
String text2 = dt.kor2Eng(text);
if(text2!=null)
System.out.println(text + "은 " + text2);
else
System.out.println(text + "는 저의 사전에 없습니다.");
}
sc.close();
}
public static void main(String[] args) {
num_10 Num_10 = new num_10();
Num_10.run();
}
}
|
11. 다수의 클래스를 만들고 활용하는 연습을 해보자. 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 수행하는 각 클래스 Add, Sub, Mul, Div를 만들어라. 이들은 모두 다음 필드와 메소드를 가진다.
각 클래스로 만들어야 한다.
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
|
import java.util.Scanner;
class Add {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a+b;
}
}
class Sub {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a-b;
}
}
class Mul {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a*b;
}
}
class Div {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a/b;
}
}
public class num_11 {
Scanner sc = new Scanner(System.in);
Add add = new Add();
Sub sub = new Sub();
Mul mul = new Mul();
Div div = new Div();
public num_11(){
System.out.print("두 정수와 연산자를 입력하시오>>");
}
public void run() {
int a = sc.nextInt();
int b = sc.nextInt();
String text = sc.next();
int set=0;
switch(text){
case "+":
add.setValue(a,b);
set = add.calculate();
break;
case "-":
sub.setValue(a,b);
set = sub.calculate();
break;
case "*":
mul.setValue(a,b);
set = mul.calculate();
break;
case "/":
div.setValue(a,b);
set = div.calculate();
break;
default:
break;
}
System.out.print(set);
}
public static void main(String[] args) {
num_11 Num_11 = new num_11();
Num_11.run();
}
}
|
12. 간단한 콘서트 예약 시스템을 만들어보자. 다수의 클래스를 다루고 객체의 배열을 다루기에는 아직 자바 프로그램 개발이 익숙하지 않은 초보자들에게 다소 무리가 있을 것이다. 그러나 반드시 넘어야 할 산이다. 이 도전을 통해 산을 넘어갈 수 있는 체력을 키워보자. 예약 시스템의 기능은 다음과 같다.
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import java.util.*;
class Seat {
String seat[] = new String[10];
public Seat() {
for(int i=0; i<seat.length;i++) {
seat[i] = " ---";
}
}
public void setData(String name, int countNum) {
if(countNum>0 && countNum<10)
seat[countNum-1] = name;
else
System.out.println("10개 이내로 입력해주세요.");
}
public void oneShow() {
for(int i=0; i<seat.length;i++) {
System.out.print(seat[i] + " ");
}
System.out.println();
}
public boolean check(String name) {
for(int i=0;i<seat[i].length();i++) {
if(seat[i].equals(name)){
seat[i] ="---";
return true;
}
}
return false;
}
}
class Reservation {
Scanner sc_sub = new Scanner(System.in);
private String Grade[] = {"S", "A", "B"};
private Seat seat[] = new Seat[3];
public Reservation() {
for(int i=0; i<seat.length;i++) {
seat[i] = new Seat();
}
}
public void Reserve() {
try{
System.out.print("좌석구분 S(1), A(2), B(3)>>");
int seatGrade = sc_sub.nextInt();
System.out.print(Grade[seatGrade-1]+">>");
seat[seatGrade-1].oneShow();
System.out.print("이름>>");
String name = sc_sub.next();
System.out.print("번호>>");
int countNum = sc_sub.nextInt();
seat[seatGrade-1].setData(name, countNum);
}
catch(InputMismatchException e) {
System.out.println("잘못입력했습니다.");
}
}
public void SeeAll() {
for(int i=0;i<Grade.length;i++) {
System.out.print(Grade[i] +">> ");
seat[i].oneShow();
}
System.out.println("<<조회를 완료하였습니다.>>");
}
public void Cancel() {
System.out.print("좌석 S:1, A:2, B:3>>");
int seatGrade = sc_sub.nextInt();
System.out.print(Grade[seatGrade-1] + ">> ");
seat[seatGrade-1].oneShow();
System.out.print("이름>>");
String name = sc_sub.next();
boolean result =seat[seatGrade-1].check(name);
if(result ==false)
System.out.println("예약되지 않은 이름입니다.");
}
}
public class num_12 {
Scanner sc_main = new Scanner(System.in);
Reservation rs = new Reservation();
public num_12() {
System.out.println("명품콘서트홀 예약 시스템입니다.");
}
public void run() {
while(true) {
System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
int num = sc_main.nextInt();
switch(num){
case 1:
rs.Reserve();
break;
case 2:
rs.SeeAll();
break;
case 3:
rs.Cancel();
break;
case 4:
return;
default:
System.out.println("1~4중에서 다시 입력해주세요.");
break;
}
}
}
public static void main(String[] args) {
num_12 Num_12 = new num_12();
Num_12.run();
}
}
|
감사합니다. 다음 포스팅 때 뵙겠습니다!
소스코드에 문제가 있다면 댓글 부탁드립니다.
반응형
'개인 학습용 해설 > 명품 Java Programming' 카테고리의 다른 글
명품 JAVA Programming 4판 5장 연습문제 - 실습문제 (0) | 2021.12.09 |
---|---|
명품 JAVA Programming 4판 5장 연습문제 - 이론문제 (0) | 2021.12.04 |
명품 JAVA Programming 4판 4장 연습문제 - 이론문제 (4) | 2021.11.15 |
명품 JAVA Programming 4판 3장 연습문제 - 실습문제 개인 풀이 (0) | 2021.11.08 |
명품 JAVA Programming 4판 3장 연습문제 - 이론문제 개인 풀이 및 개인해설 (0) | 2020.10.16 |
반응형
안녕하세요. 명품 자바 4판 4장 연습문제 - 실습문제입니다.
주관적으로 푼 풀이입니다.
1. 지문 생략
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class TV{
private String name = null;
private int year = 0;
private int inch = 0;
TV() {} // 생략 가능
TV(String name, int year, int inch) {
this.name = name;
this.year = year;
this.inch = inch;
}
void show() {
System.out.println(name + "에서 만든 " + year +"년형 " + inch + "인치" + " TV");
}
}
public class num_1 {
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32);
myTV.show();
}
}
|
2. 3과목의 점수를 입력받아 Grade 객체를 생성하고 성적 평균을 출력하는 main()~ 이후 생략
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
|
import java.util.Scanner;
class Grade{
private int math = 0;
private int science = 0;
private int english = 0;
private int avg =0;
public Grade(){}
public Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
public int average() {
avg = (math + science + english)/3;
return avg;
}
}
public class num_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
int math = sc.nextInt();
int science = sc.nextInt();
int english = sc.nextInt();
Grade me = new Grade(math, science, english);
System.out.println("평균은 " + me.average());
sc.close();
}
}
|
3. 노래 한 곡을 나타내는 Song 클래스를 작성하라.
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
|
class Song{
private String title = null;
private String artist = null;
private int year = 0;
private String country = null;
Song(){}
Song(String title, String artist, int year, String country){
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
public void show() {
System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
}
}
public class num_3 {
public static void main(String[] args) {
Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
song.show();
}
}
|
4. 다음 멤버를 가지고 직사각형을 표현하는 Rectangle 클래스를 작성하라.
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
|
class Rectangle{
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
public Rectangle(){}
public Rectangle(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int square() {
return width * height;
}
public void show() {
System.out.printf("(%d,%d)에서 크기가 %dx%d인 사각형 \n", x, y, width, height);
}
public boolean contains (Rectangle r) {
// x,y 좌표가 r의 좌표에 포함 되는지 그리고(and) 가로 세로의 값에 x,y 좌표 값을 더한 것 보다 큰 경우. 포함된다.
if ((x < r.x) && (y < r.y) && ( x + width > r.x + r.width && y + height > r.y + r.height ))
return true;
else
return false;
}
}
public class num_4 {
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 2, 8, 7);
Rectangle s = new Rectangle(5, 5, 6, 6);
Rectangle t = new Rectangle(1, 1, 10, 10);
r.show();
System.out.println("s의 면적은 " + s.square());
if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
if(t.contains(s)) System.out.println("t는 s을 포함합니다.");
}
}
|
5. 다음 설명대로 Circle 클래스와 CircleManager 클래스를 완성하라.
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
|
import java.util.Scanner;
class Circle {
private double x=0;
private double y=0;
private int radius=0;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void show() {
System.out.printf("(%.1f,%.1f)%d\n", x, y, radius);
}
}
public class num_5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle c[] = new Circle[3];
for(int i=0 ; i<c.length ; i++) {
System.out.print("x, y, radius >>");
double x = sc.nextDouble();
double y = sc.nextDouble();
int radius = sc.nextInt();
c[i] = new Circle(x, y, radius);
}
for(int i=0; i<c.length;i++) {
c[i].show();
}
sc.close();
}
}
|
문제에서는 Main()클래스를 CircleManager로 생성하라 했습니다.
저는 편의상 단원의 번호별로 보기 위해서 num_n으로 클래스를 작성했습니다.
6. 앞의 5번 문제는 정답이 공개되어 있다. 이 정답을 참고하여 Circle 클래스와 CircleManager 클래스를 수정하여 다음 실행 결과처럼 되게 하라.
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
|
import java.util.Scanner;
class Circle {
private double x=0;
private double y=0;
private int radius=0;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public int getR(){
return radius;
}
public void show() {
System.out.printf("가장 면적이 큰 원은 (%.1f,%.1f)%d\n", x, y, radius);
}
}
public class num_6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle c[] = new Circle[3];
int max =0;
for(int i=0 ; i<c.length ; i++) {
System.out.print("x, y, radius >>");
double x = sc.nextDouble();
double y = sc.nextDouble();
int radius = sc.nextInt();
c[i] = new Circle(x, y, radius);
}
for(int i=0; i<c.length;i++) {
if(c[max].getR()<c[i].getR())
max = i;
}
c[max].show();
sc.close();
}
}
|
7. 하루의 할 일을 표현하는 클래스 Day는 다음과 같다. 한 달의 할 일을 표현하는 MonthSchedule 클래스를 작성하라.
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
|
import java.util.Scanner;
class Day{
private String work = null;
public String get() {return work;}
public void set(String work){this.work = work;}
public void show() {
if(work==null)
System.out.println("없습니다.");
else
System.out.println("있습니다.");
}
}
public class num_7 {
Scanner sc = new Scanner(System.in);
Day day[] = null;
boolean isDay = true;
int dayNum = 0;
public num_7(int days) {
dayNum = days;
day = new Day[dayNum];
for(int i=0; i<day.length;i++) {
day[i] = new Day();
}
}
public void input() {
System.out.print("날짜(1~30)?");
int days = sc.nextInt();
System.out.print("할일(빈칸없이입력)?");
String work = sc.next();
day[days-1].set(work);
}
public void view() {
System.out.print("날짜(1~30)");
int days = sc.nextInt();
System.out.println(days+"일의 할 일은 " + day[days-1].get() + "입니다.");
}
public void finish() {
System.out.println("프로그램을 종료합니다.");
isDay = false;
}
public void run() {
System.out.println("이번달 스케쥴 관리 프로그램.");
while(isDay) {
System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
int choice = sc.nextInt();
switch(choice) {
case 1:
input();
break;
case 2:
view();
break;
case 3:
finish();
break;
default:
System.out.println("1~3 값을 입력해주세요");
}
System.out.println();
}
}
public static void main(String[] args) {
num_7 april = new num_7(30);
april.run();
}
}
|
8. 이름(name), 전화번호(tel) 필드와 생성자 등을 가진 Phone 클래스를 작성하고, 실행 예시와 같이 작동하는 PhoneBook 클래스를 작성하라.
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
|
import java.util.Scanner;
class Phone {
private String name = null;
private String tel = null;
public Phone() {}
public Phone(String name, String tel) {
this.name = name;
this.tel = tel;
}
public String getName() {return name;}
public String getTel() {return tel;}
}
public class num_8 {
Scanner sc = new Scanner(System.in);
Phone phone[];
int number = 0;
public num_8(){
System.out.print("인원수>>");
number = sc.nextInt();
phone = new Phone[number];
}
public void input() {
for(int i=0; i<phone.length;i++) {
System.out.print("이름과 번호는 빈 칸없이 입력)>>");
String text = sc.next();
String tel = sc.next();
phone[i] = new Phone(text, tel);
}
System.out.println("저장되었습니다...");
}
public void search() {
while(true) {
boolean op = false;
boolean op2 = false;
System.out.print("검색할 이름>>");
String name = sc.next();
if(name.equals("그만"))
break;
for(int i=0; i<phone.length; i++) {
if(name.equals(phone[i].getName())) {
System.out.println(name + "의 번호는 " + phone[i].getTel() + "입니다.");
op = true;
}
}
if(op == false) {
System.out.println(name + " 이 없습니다.");
}
}
}
public static void main(String[] args) {
num_8 Num_8 = new num_8();
Num_8.input();
Num_8.search();
}
}
|
9. 다음 2개의 static 가진 ArrayUil 클래스를 만들어보자. 다음 코드의 실행 결과를 참고하여 concat()와 print()를 작성하여 ArrayUil 클래스를 완성하라.
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 ArrayUtil{
public static int[] concat(int a[], int b[]) {
int array[] = new int[a.length+b.length];
for(int i=0;i<a.length;i++)
array[i] = a[i];
for(int i=0;i<b.length;i++)
array[i+a.length] = b[i];
return array;
}
public static void print(int a[]) {
System.out.print("[ ");
for(int i =0; i<a.length;i++) {
System.out.print(a[i] + " ");
}
System.out.println("]");
}
}
public class num_9 {
public static void main(String[] args) {
int array1[] = {1, 5, 7, 9};
int array2[] = {3, 6, -1, 100, 77};
int array3[] = ArrayUtil.concat(array1, array2);
ArrayUtil.print(array3);
}
}
|
10. 다음과 같은 Dictionary 클래스가 있다. 실행 결과와 같이 작동하도록 Dictionary 클래스의 kor2Eng() 메소드와 DicApp 클래스를 작성하라.
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
|
import java.util.Scanner;
class Dictionary{
private static String [] kor = {"사랑", "아기", "돈", "미래", "희망"};
private static String [] eng = {"love", "baby", "money", "future", "hope"};
public static String kor2Eng(String word) {
for(int i=0;i<kor.length;i++) {
if (word.equals(kor[i]))
return eng[i];
}
return null;
}
}
public class num_10 {
public num_10(){
System.out.println("한영 단어 검색 프로그램입니다.");
}
public void run() {
Scanner sc = new Scanner(System.in);
Dictionary dt = new Dictionary();
while(true) {
System.out.print("한글 단어?");
String text = sc.next();
if(text.equals("그만"))
break;
String text2 = dt.kor2Eng(text);
if(text2!=null)
System.out.println(text + "은 " + text2);
else
System.out.println(text + "는 저의 사전에 없습니다.");
}
sc.close();
}
public static void main(String[] args) {
num_10 Num_10 = new num_10();
Num_10.run();
}
}
|
11. 다수의 클래스를 만들고 활용하는 연습을 해보자. 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 수행하는 각 클래스 Add, Sub, Mul, Div를 만들어라. 이들은 모두 다음 필드와 메소드를 가진다.
각 클래스로 만들어야 한다.
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
|
import java.util.Scanner;
class Add {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a+b;
}
}
class Sub {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a-b;
}
}
class Mul {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a*b;
}
}
class Div {
private int a=0;
private int b=0;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a/b;
}
}
public class num_11 {
Scanner sc = new Scanner(System.in);
Add add = new Add();
Sub sub = new Sub();
Mul mul = new Mul();
Div div = new Div();
public num_11(){
System.out.print("두 정수와 연산자를 입력하시오>>");
}
public void run() {
int a = sc.nextInt();
int b = sc.nextInt();
String text = sc.next();
int set=0;
switch(text){
case "+":
add.setValue(a,b);
set = add.calculate();
break;
case "-":
sub.setValue(a,b);
set = sub.calculate();
break;
case "*":
mul.setValue(a,b);
set = mul.calculate();
break;
case "/":
div.setValue(a,b);
set = div.calculate();
break;
default:
break;
}
System.out.print(set);
}
public static void main(String[] args) {
num_11 Num_11 = new num_11();
Num_11.run();
}
}
|
12. 간단한 콘서트 예약 시스템을 만들어보자. 다수의 클래스를 다루고 객체의 배열을 다루기에는 아직 자바 프로그램 개발이 익숙하지 않은 초보자들에게 다소 무리가 있을 것이다. 그러나 반드시 넘어야 할 산이다. 이 도전을 통해 산을 넘어갈 수 있는 체력을 키워보자. 예약 시스템의 기능은 다음과 같다.
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import java.util.*;
class Seat {
String seat[] = new String[10];
public Seat() {
for(int i=0; i<seat.length;i++) {
seat[i] = " ---";
}
}
public void setData(String name, int countNum) {
if(countNum>0 && countNum<10)
seat[countNum-1] = name;
else
System.out.println("10개 이내로 입력해주세요.");
}
public void oneShow() {
for(int i=0; i<seat.length;i++) {
System.out.print(seat[i] + " ");
}
System.out.println();
}
public boolean check(String name) {
for(int i=0;i<seat[i].length();i++) {
if(seat[i].equals(name)){
seat[i] ="---";
return true;
}
}
return false;
}
}
class Reservation {
Scanner sc_sub = new Scanner(System.in);
private String Grade[] = {"S", "A", "B"};
private Seat seat[] = new Seat[3];
public Reservation() {
for(int i=0; i<seat.length;i++) {
seat[i] = new Seat();
}
}
public void Reserve() {
try{
System.out.print("좌석구분 S(1), A(2), B(3)>>");
int seatGrade = sc_sub.nextInt();
System.out.print(Grade[seatGrade-1]+">>");
seat[seatGrade-1].oneShow();
System.out.print("이름>>");
String name = sc_sub.next();
System.out.print("번호>>");
int countNum = sc_sub.nextInt();
seat[seatGrade-1].setData(name, countNum);
}
catch(InputMismatchException e) {
System.out.println("잘못입력했습니다.");
}
}
public void SeeAll() {
for(int i=0;i<Grade.length;i++) {
System.out.print(Grade[i] +">> ");
seat[i].oneShow();
}
System.out.println("<<조회를 완료하였습니다.>>");
}
public void Cancel() {
System.out.print("좌석 S:1, A:2, B:3>>");
int seatGrade = sc_sub.nextInt();
System.out.print(Grade[seatGrade-1] + ">> ");
seat[seatGrade-1].oneShow();
System.out.print("이름>>");
String name = sc_sub.next();
boolean result =seat[seatGrade-1].check(name);
if(result ==false)
System.out.println("예약되지 않은 이름입니다.");
}
}
public class num_12 {
Scanner sc_main = new Scanner(System.in);
Reservation rs = new Reservation();
public num_12() {
System.out.println("명품콘서트홀 예약 시스템입니다.");
}
public void run() {
while(true) {
System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
int num = sc_main.nextInt();
switch(num){
case 1:
rs.Reserve();
break;
case 2:
rs.SeeAll();
break;
case 3:
rs.Cancel();
break;
case 4:
return;
default:
System.out.println("1~4중에서 다시 입력해주세요.");
break;
}
}
}
public static void main(String[] args) {
num_12 Num_12 = new num_12();
Num_12.run();
}
}
|
감사합니다. 다음 포스팅 때 뵙겠습니다!
소스코드에 문제가 있다면 댓글 부탁드립니다.
반응형
'개인 학습용 해설 > 명품 Java Programming' 카테고리의 다른 글
명품 JAVA Programming 4판 5장 연습문제 - 실습문제 (0) | 2021.12.09 |
---|---|
명품 JAVA Programming 4판 5장 연습문제 - 이론문제 (0) | 2021.12.04 |
명품 JAVA Programming 4판 4장 연습문제 - 이론문제 (4) | 2021.11.15 |
명품 JAVA Programming 4판 3장 연습문제 - 실습문제 개인 풀이 (0) | 2021.11.08 |
명품 JAVA Programming 4판 3장 연습문제 - 이론문제 개인 풀이 및 개인해설 (0) | 2020.10.16 |