1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package Chapter4;
public class TV {
private String manufacturer;
private int year;
private int size;
public TV(String manufacturer, int year, int size) {
this.manufacturer = manufacturer;
this.year = year;
this.size = size;
}
public void show() {
System.out.println(manufacturer+"에서 만든 "+year+"년형 "+size+"인치 TV");
}
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32);
myTV.show();
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
public class Grade {
private int math;
private int science;
private int english;
public Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
public int average() {
int sum = this.math + this.science + this.english;
return sum / 3;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
int math = scanner.nextInt();
int science = scanner.nextInt();
int english = scanner.nextInt();
Grade me = new Grade(math, science, english);
System.out.println("평균은 "+me.average());
scanner.close();
}
}
|
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
26
27
28
29
30
|
package Chapter4;
public class Song {
String title;
String artist;
int year;
String country;
public Song() {
this("Dancing Queen", "ABBA", 1978, "스웨덴");
}
public 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 static void main(String[] args) {
Song song = new Song();
Song newSong = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
song.show();
newSong.show();
}
}
|
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
|
package Chapter4;
public class Rectangle {
int x;
int y;
int width;
int height;
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.println("("+x+","+y+") 에서 크기가 "+width+"X"+height+"인 사각형");
}
public boolean contains(Rectangle r) {
if(r.x > x && r.y > y) {
if((x + width)>(r.x + r.width) && (y + height) > (r.y + r.width))
return true;
}
return false;
}
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를 포함합니다.");
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
class Circle {
private double x, y;
private int radius;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void show() {
System.out.println("("+x+","+y+") "+radius);
}
}
public class CircleManager{
public static void main(String[] args) {
Scanner scanner = 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 = scanner.nextDouble();
double y = scanner.nextDouble();
int radius = scanner.nextInt();
c[i] = new Circle(x, y, radius);
}
for(int i=0; i<c.length; i++) c[i].show();
scanner.close();
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
class Circle {
private double x, y;
private int radius;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void show() {
System.out.println("("+x+","+y+") "+radius);
}
public double area() {
return this.radius * this.radius * 3.14;
}
}
public class CircleManager{
public static void main(String[] args) {
Scanner scanner = 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 = scanner.nextDouble();
double y = scanner.nextDouble();
int radius = scanner.nextInt();
c[i] = new Circle(x, y, radius);
}
double area = c[0].area();
for(int i=0; i<c.length; i++) {
if(c[i].area() > area)
area = c[i].area();
}
for(int i = 0; i<c.length; i++) {
if(area == c[i].area()) {
System.out.print("가장 면적이 큰 원은 ");
c[i].show();
}
}
scanner.close();
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
class Day{
private String work;
public void set(String work) { this.work = work; }
public String get() { return work; }
public void show() {
if(work == null) System.out.println("없습니다.");
else System.out.println(work +"입니다.");
}
}
public class MonthSchedule {
Scanner sc = new Scanner(System.in);
private Day [] days;
boolean option = true;
public MonthSchedule(int n) {
days = new Day[n];
for(int i = 0; i<days.length; i++) {
days[i] = new Day();
}
}
public void input() {
System.out.print("날짜(1-30)?");
int day = sc.nextInt();
System.out.print("할일(빈칸없이입력)? ");
String work = sc.next();
days[day-1].set(work);
}
public void view() {
System.out.print("날짜(1-30)?");
int day = sc.nextInt();
System.out.print(day+"의 할 일은 ");
days[day-1].show();
}
public void finish() {
System.out.println("프로그램을 종료합니다.");
option = false;
}
public void run() {
System.out.println("이번달 스케쥴 관리 프로그램.");
while(option) {
System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
int number = sc.nextInt();
switch(number) {
case 1:
input();
break;
case 2:
view();
break;
case 3:
finish();
break;
}
}
}
public static void main(String[] args) {
MonthSchedule april = new MonthSchedule(30);
april.run();
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
class Phone{
String name;
String tel;
public Phone(String name, String tel) {
this.name = name;
this.tel = tel;
}
}
public class PhoneBook {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("인원수>> ");
int person = sc.nextInt();
Phone [] phones = new Phone[person];
for(int i = 0; i<phones.length; i++) {
System.out.print("이름과 전화번호(이름과 번호는 빈 칸없이 입력)>> ");
String name = sc.next();
String tel = sc.next();
phones[i] = new Phone(name, tel);
}
System.out.println("저장되었습니다...");
while(true) {
int i = 0;
System.out.print("검색할 이름>> ");
String search = sc.next();
for(i =0; i<phones.length; i++) {
if(search.equals(phones[i].name)) {
System.out.println(search+"의 번호는 "+phones[i].tel+"입니다.");
break;
}
}
if(search.equals("그만")) break;
if(i == person) {
System.out.println(search+" 이 없습니다.");
}
}
sc.close();
}
}
|
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
|
package Chapter4;
class ArrayUtil{
public static int [] concat(int[] a, int[] b) {
int [] c = new int [a.length+b.length];
for(int i =0; i<a.length; i++) {
c[i] = a[i];
}
for(int i=0; i<b.length; i++) {
int array = a.length;
c[a.length + i] = b[i];
}
return c;
}
public static void print(int[] a) {
System.out.print("[ ");
for(int i=0; i<a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.print("]");
}
}
public class StaticEx {
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);
}
}
|
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
|
package Chapter4;
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) {
int i =0;
for(i =0; i<kor.length; i++) {
if(word.equals(kor[i])) {
System.out.println(word+"은 "+eng[i]);
break;
}
}
if(i == kor.length) System.out.println(word+"는 저의 사전에 없습니다.");
return null;
}
}
public class DicApp {
public static void main(String[] args) {
System.out.println("한영 단어 검색 프로그램입니다.");
Scanner sc = new Scanner(System.in);
Dictionary dic = new Dictionary();
while(true) {
System.out.print("한글 단어?");
String word = sc.next();
if(word.equals("그만")) break;
dic.kor2Eng(word);
}
sc.close();
}
}
|
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
|
package Chapter4;
import java.util.Scanner;
class Add {
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return this.a+this.b;
}
}
class Sub {
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return this.a-this.b;
}
}
class Mul {
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return this.a*this.b;
}
}
class Div {
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return this.a/this.b;
}
}
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
char op = sc.next().charAt(0);
switch(op) {
case '+':
Add ad = new Add();
ad.setValue(a, b);
System.out.println(ad.calculate());
break;
case '-':
Sub sb = new Sub();
sb.setValue(a, b);
System.out.println(sb.calculate());
break;
case '*':
Mul ml = new Mul();
ml.setValue(a, b);
System.out.println(ml.calculate());
break;
case '/':
Div dv = new Div();
dv.setValue(a, b);
System.out.println(dv.calculate());
break;
}
}
}
|
cs |
12. 는 어려워서 넘기겠습니다 ㅎㅎ 다음에 풀게요 !!
'자바 > 명품 자바 프로그래밍' 카테고리의 다른 글
명품 자바 프로그래밍 5장 실습 문제 / 2021.11.16 (0) | 2021.11.16 |
---|---|
명품 자바 프로그래밍 5장 이론 문제 / 2021.11.10 (0) | 2021.11.10 |
명품 자바 프로그래밍 4장 이론 문제 / 2021.07.31 (0) | 2021.07.31 |
명품 자바 프로그래밍 3장 실습 문제 / 2021.07.29 (1) | 2021.07.29 |
명품 자바 프로그래밍 3장 이론 문제 / 2021.07.28 (0) | 2021.07.28 |