안녕하세요.
다음 포스팅은 주관적 풀이이므로 오답이 있을 수 있습니다.
오답이라 생각되는 부분, 질문, 피드백 등 자유롭게 댓글을 남겨주시면 감사드리겠습니다.
1. 다음 main() 메소드와 실행 결과를 참조하여 TV를 상속받은 ColorTV 클래스를 작성하라.
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
|
class TV{
private int size=0;
public TV(int size) {this.size = size;}
protected int getSize() {return size;}
}
class ColorTV extends TV{
private int Color_size = 0;
public ColorTV(int size, int Color_size) {
super(size);
this.Color_size = Color_size;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + Color_size +"컬러");
}
}
public class num_1 {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
|
2. 다음 main() 메소드와 실행 결과를 참조하여 문제 1의 ColorTV를 상속받는 IPTV 클래스를 작성하라.
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
|
class TV{
private int size=0;
public TV(int size) {this.size = size;}
protected int getSize() {return size;}
}
class ColorTV extends TV{
private int Color_size = 0;
public ColorTV(int size, int Color_size) {
super(size);
this.Color_size = Color_size;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + Color_size +"컬러");
}
}
class IPTV extends ColorTV{
private String ip;
public IPTV(String ip, int size, int Color_size) {
super(size, Color_size);
this.ip = ip;
}
public void printProperty() {
System.out.println("나의 IPTV는 " + ip + " 주소의 ");
super.printProperty();
}
}
public class num_2 {
public static void main(String[] args) {
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
|
3. Converter 클래스를 상속받아 원화를 달러로 변환하는 Won2Dollar 클래스를 작성하라. 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
|
import java.util.Scanner;
abstract class Converter{
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = sc.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
sc.close();
}
}
class Won2Dollar extends Converter{
public Won2Dollar(double ratio) {this.ratio = ratio;}
protected double convert(double src) {return src/ratio;}
protected String getSrcString() {return "원";}
protected String getDestString() {return "달러";}
}
public class num_3 {
public static void main(String[] args) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
|
4. Converter 클래스를 상속받아 Km를 mile(마일)로 변환하는 Km2Mile 클래스를 작성하라. 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
|
import java.util.Scanner;
abstract class Converter{
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = sc.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
sc.close();
}
}
class Km2Mile extends Converter{
public Km2Mile(double ratio){ this.ratio = ratio;}
protected double convert(double src) {return src/ratio;}
protected String getSrcString() {return "Km";}
protected String getDestString() {return "mile";}
}
public class num_4 {
public static void main(String[] args) {
Km2Mile toMile = new Km2Mile(1.6);
toMile.run();
}
}
|
5. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class ColorPoint extends Point{
private String Color = null;
public ColorPoint(int x, int y, String Color){
super(x,y);
this.Color = Color;
}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String Color) {this.Color = Color;}
public String toString() {
return Color + "색의 (" + getX() + "," + getY() +")의 점";
}
}
public class num_5 {
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다.");
}
}
|
6. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class ColorPoint extends Point{
private String Color = null;
public ColorPoint() {super(0,0); Color="BLACK";}
public ColorPoint(int x, int y){super(x,y);}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String Color) {this.Color = Color;}
public String toString() {
return Color + "색의 (" + getX() + "," + getY() +")의 점";
}
}
public class num_6 {
public static void main(String[] args) {
ColorPoint zeroPoint = new ColorPoint();
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint cp = new ColorPoint(10, 10);
cp.setXY(5, 5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다.");
}
}
|
7. Point를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class Point3D extends Point{
private int z = 0;
public Point3D(int x, int y, int z){super(x,y); this.z = z;}
public void setXY(int x, int y) {move(x,y);}
public void move(int x, int y, int z) {move(x,y); this.z = z;}
public void moveUp() {z++;}
public void moveDown() {z--;}
public String toString() {return "(" + getX() + "," + getY() + "," + z +")의 점";}
}
public class num_7 {
public static void main(String[] args) {
Point3D p = new Point3D(1, 2, 3);
System.out.println(p.toString() + "입니다.");
p.moveUp();
System.out.println(p.toString() + "입니다.");
p.moveDown();
p.move(10,10);
System.out.println(p.toString() + "입니다.");
p.move(100, 200, 300);
System.out.println(p.toString() + "입니다.");
}
}
|
8. Point를 상속받아 양수의 공간에서만 점을 나타내는 PostivePoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class PositivePoint extends Point{
public PositivePoint(){ super(0,0);}
public PositivePoint(int x, int y) {
super(0,0);
move(x,y);
}
protected void move(int x, int y) {
if(x>0 && y>0){
super.move(x,y);
}
}
public void setXY(int x, int y) {move(x,y);}
public String toString() {return "(" + getX() + "," + getY() + ")의 점";}
}
public class num_8 {
public static void main(String[] args) {
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString() + "입니다.");
p.move(-5, 5);
System.out.println(p.toString() + "입니다.");
PositivePoint p2 = new PositivePoint(-10, -10);
System.out.println(p2.toString() + "입니다.");
}
}
|
9. 다음 Stack 인터페이스를 상속받아 실수를 실수를 저장하는 StringStack 클래스를 구성하라.
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
|
import java.util.Scanner;
interface Stack{
int length();
int capacity();
String pop();
boolean push(String val);
}
class StringStack implements Stack{
private String Stack[];
private int top =0;
public StringStack(int number) {Stack = new String[number];}
// 현재 스택에 저장된 개수 리턴
public int length() {return top;}
// 전체 저장 가능한 개수 리턴
public int capacity() {return Stack.length;}
// 스택의 톱(top)에 실수 저장
public String pop() {
if(top==0) {
return "빈 상태";
}
else {
String stay = Stack[top-1];
top--;
return stay;
}
}
// 스택의 톱(top)에 저장된 실수 리턴
public boolean push(String val) {
if (top==Stack.length) {
return false;
}
else{
Stack[top] =val;
top++;
return true;
}
}
}
public class num_9 {
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int number = sc.nextInt();
StringStack ss = new StringStack(number);
while(true) {
System.out.print("문자열 입력 >> ");
String text = sc.next();
if(text.equals("그만")) {break;}
boolean isCheck = ss.push(text);
if(isCheck ==false) {System.out.println("스택이 꽉 차서 푸시 불가!");}
}
int textPop = ss.length();
for(int i=0;i<textPop;i++) {
System.out.print(ss.pop() +" ");
}
}
public static void main(String[] args) {
num_9 n = new num_9();
n.run();
}
}
|
10. PairMap을 상속받는 Dictionary 클래스를 구현하고, 이를 다음과 같이 활용하는 main() 메소드를 가진 클래스 DictionaryApp도 작성하라.
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
|
abstract class PairMap{
protected String keyArray[];
protected String valueArray [];
abstract String get(String key);
abstract void put(String key, String value);
abstract String delete(String key);
abstract int length();
}
class Dictionary extends PairMap{
private int count=0;
public Dictionary(int array) {
keyArray = new String [array];
valueArray = new String[array];
}
@Override
public String get(String key) {
for(int i=0; i<count; i++) {
if(key.equals(keyArray[i])){
return valueArray[i];
}
}
return null;
}
@Override
public void put(String key, String value) {
for(int i=0; i<count;i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = value;
return;
}
}
keyArray[count] = key;
valueArray[count] = value;
count++;
}
@Override
public String delete(String key) {
for(int i=0; i<count;i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = null;
return null;
}
}
return key;
}
@Override
public int length() {return count;}
}
public class num_10 {
public static void main(String[] args) {
Dictionary dic = new Dictionary(10);
dic.put("황기태", "자바");
dic.put("이재문", "파이선");
dic.put("이재문", "C++");
System.out.println("이재문의 값은 " + dic.get("이재문"));
System.out.println("황기태의 값은 " + dic.get("황기태"));
dic.delete("황기태");
System.out.println("황기태의 값은 " + dic.get("황기태"));
}
}
|
11. 철수 학생은 다음 3개의 필드와 메소드를 가진 4개의 클래스 Add, Sub, Mul, Div를 작성하려고 한다.(4장 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
|
import java.util.Scanner;
abstract class Calc{
protected int a;
protected int b;
abstract void setValue(int a, int b);
abstract int calculate();
}
class Add extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a+b;}
}
class Sub extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a-b;}
}
class Mul extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a*b;}
}
class Div extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a/b;}
}
public class num_11 {
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
String op = sc.next();
switch(op.charAt(0)) {
case '+':
Add add = new Add();
add.setValue(a, b);
System.out.println(add.calculate());
break;
case '-':
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(sub.calculate());
break;
case '*':
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(mul.calculate());
break;
case '/':
Div div = new Div();
div.setValue(a, b);
System.out.println(div.calculate());
break;
default:
System.out.println("잘못 된 값입니다.");
break;
}
}
public static void main(String[] args) {
num_11 num_11 = new num_11();
num_11.run();
}
}
|
12. 텍스트로 입출력하는 간단한 그래픽 편집기를 만들어보자. 추상 클래스인 Shape, Line, Rect, Circle 클래스 코드를 잘 완성하고 "삽입", "삭제", "모두 보기", "종료" 4가지 그래픽 편집 기능을 가진 클래스 GraphicEditor을 작성하라.
public class가 같은 코드 내에 중복할 수 없으므로 java파일을 분할해서 작성했습니다. 또한 12번 문제는 이전 문제에서 Stack이 나온 것과 같이 구조체로 Linkedlist가 나왔다는 가정하에 풀었습니다. 또한 추상화 클래스와 메인 클래스를 분리해서 푼 문제이므로 하나의 클래스에서 선언하려면 추상 클래스의 public 클래스를 제외하고 코드를 사용하셔야 합니다.
public abstract class Shape
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
|
public abstract class Shape{
private Shape next;
public Shape() {next = null;}
public void setNext(Shape obj) {next = obj;}
public Shape getNext() {return next;}
public abstract void draw();
}
class Line extends Shape{public void draw() {System.out.println("Line");}}
class Rect extends Shape{public void draw() {System.out.println("Rect");}}
class Circle extends Shape{public void draw() {System.out.println("Circle");}}
class Editor{
private Shape head;
private Shape tail;
public Editor(){
this.head = null;
this.tail = null;
}
public void Insert(int insert) {
Shape obj = null;
switch(insert) {
case 1:
obj = new Line();
break;
case 2:
obj = new Rect();
break;
case 3:
obj = new Circle();
break;
default:
System.out.println("잘못 된 값을 입력하셨습니다.");
break;
}
if (head==null) {
head = obj;
tail = obj;
}
else {
tail.setNext(obj);
tail = obj;
}
}
public void Delete(int del) {
Shape objHead = head;
Shape objTail = tail;
Shape objReverse = head;
try {
if(objHead==null) {
System.out.println("삭제할 수 없습니다.");
return;
}
if (del<1) {
System.out.println("잘못된 값을 입력하셨습니다.");
return;
}else if(del==1) {
if(head==tail){
objHead = null;
objTail = null;
}
else {
head=head.getNext();
return;
}
}
if(del>1) {
if(objHead!=null) {
for(int i=1;i<del;i++){
objReverse = objHead;
objHead = objHead.getNext();
}
}
if(objHead==objTail) {
tail=objReverse;
tail.setNext(null);
}else {
objReverse.setNext(objHead.getNext());
}
}
}catch(NullPointerException e){
System.out.println("삭제할 수 없습니다.");
}
}
public void printAll() {
Shape obj = head;
while(obj != null){
obj.draw();
obj = obj.getNext();
}
}
}
|
main class
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
|
import java.util.Scanner;
public class GraphicEditor {
Scanner sc = new Scanner(System.in);
Editor et = new Editor();
public GraphicEditor() {
System.out.println("그래픽 에디터 beauty을 실행합니다.");
}
public void run() {
while(true) {
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
int num = sc.nextInt();
switch(num){
case 1:
System.out.print("Line(1), Rect(2), Circle(3)>>");
int insert = sc.nextInt();
et.Insert(insert);
break;
case 2:
System.out.print("삭제할 도형의 위치>>");
int del = sc.nextInt();
et.Delete(del);
break;
case 3:
et.printAll();
break;
case 4:
System.out.println("beauty을 종료합니다.");
return;
default:
System.out.println("잘못 된 값을 입력하셨습니다.");
break;
}
}
}
public static void main(String[] args) {
GraphicEditor ge = new GraphicEditor();
ge.run();
}
}
|
13. 인터페이스 Shape을 구현한 클래스 Circle를 작성하고 전체 프로그램을 완성하라.
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
|
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다.");
draw();
}
}
class Circle implements Shape {
private int size=0;
public Circle(int size){this.size = size;}
@Override
public void draw() {
System.out.println("반지름이 " + size +"인 원입니다.");
}
@Override
public double getArea() {
double cal = size * size * PI;
return cal;
}
}
public class num_13 {
public static void main(String[] args) {
Shape donut = new Circle(10);
donut.redraw();
System.out.println("면적은 " + donut.getArea());
}
}
|
14. 문제 13의 Shape 인터페이스를 구현한 클래스 Oval, Rect를 추가 작성하고 전체 프로그램을 완성하라.
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
|
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다."); draw();
}
}
class Circle implements Shape {
private int size=0;
public Circle(int size){this.size = size;}
@Override
public void draw() {System.out.println("반지름이 " + size +"인 원입니다.");}
@Override
public double getArea() {
double calCircle = size * size * PI;
return calCircle;
}
}
class Oval implements Shape{
private int x;
private int y;
public Oval(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void draw() {System.out.println(x + "X" + y +"에 내접하는 타원입니다.");}
@Override
public double getArea() {
double calOval = x * y * PI;
return calOval;
}
}
class Rect implements Shape{
private int x;
private int y;
public Rect(int x, int y){
this.x = x;
this.y = y;
}
@Override
public void draw() {System.out.println(x+ "X" + y + "크기의 사각형 입니다.");}
@Override
public double getArea() {
double calRect = x * y;
return calRect;
}
}
public class num_14 {
public static void main(String[] args) {
Shape []list = new Shape[3];
list[0] = new Circle(10);
list[1] = new Oval(20, 30);
list[2] = new Rect(10, 40);
for(int i=0; i<list.length; i++) list[i].redraw();
for(int i=0; i<list.length; i++) System.out.println("면적은 " + list[i].getArea());
}
}
|
오탈자가 있다면 그것에 대한 댓글 감사드립니다. 또한 코드에 이해가 안가는 부분이 있으신 경우에도 댓글 주시면 최대한 아는 부분에서 설명드리겠습니다. 감사드립니다.
다음 포스팅 때 뵙겠습니다.
'개인 학습용 해설 > 명품 Java Programming' 카테고리의 다른 글
[Java] 명품 JAVA Programming 4판 6장 연습문제 - 이론문제 (4) | 2022.04.16 |
---|---|
명품 JAVA Programming 4판 5장 연습문제 - 이론문제 (0) | 2021.12.04 |
명품 JAVA Programming 4판 4장 연습문제 - 실습문제 (0) | 2021.11.18 |
명품 JAVA Programming 4판 4장 연습문제 - 이론문제 (4) | 2021.11.15 |
명품 JAVA Programming 4판 3장 연습문제 - 실습문제 개인 풀이 (0) | 2021.11.08 |
안녕하세요.
다음 포스팅은 주관적 풀이이므로 오답이 있을 수 있습니다.
오답이라 생각되는 부분, 질문, 피드백 등 자유롭게 댓글을 남겨주시면 감사드리겠습니다.
1. 다음 main() 메소드와 실행 결과를 참조하여 TV를 상속받은 ColorTV 클래스를 작성하라.
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
|
class TV{
private int size=0;
public TV(int size) {this.size = size;}
protected int getSize() {return size;}
}
class ColorTV extends TV{
private int Color_size = 0;
public ColorTV(int size, int Color_size) {
super(size);
this.Color_size = Color_size;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + Color_size +"컬러");
}
}
public class num_1 {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
|
2. 다음 main() 메소드와 실행 결과를 참조하여 문제 1의 ColorTV를 상속받는 IPTV 클래스를 작성하라.
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
|
class TV{
private int size=0;
public TV(int size) {this.size = size;}
protected int getSize() {return size;}
}
class ColorTV extends TV{
private int Color_size = 0;
public ColorTV(int size, int Color_size) {
super(size);
this.Color_size = Color_size;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + Color_size +"컬러");
}
}
class IPTV extends ColorTV{
private String ip;
public IPTV(String ip, int size, int Color_size) {
super(size, Color_size);
this.ip = ip;
}
public void printProperty() {
System.out.println("나의 IPTV는 " + ip + " 주소의 ");
super.printProperty();
}
}
public class num_2 {
public static void main(String[] args) {
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
|
3. Converter 클래스를 상속받아 원화를 달러로 변환하는 Won2Dollar 클래스를 작성하라. 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
|
import java.util.Scanner;
abstract class Converter{
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = sc.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
sc.close();
}
}
class Won2Dollar extends Converter{
public Won2Dollar(double ratio) {this.ratio = ratio;}
protected double convert(double src) {return src/ratio;}
protected String getSrcString() {return "원";}
protected String getDestString() {return "달러";}
}
public class num_3 {
public static void main(String[] args) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
|
4. Converter 클래스를 상속받아 Km를 mile(마일)로 변환하는 Km2Mile 클래스를 작성하라. 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
|
import java.util.Scanner;
abstract class Converter{
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = sc.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
sc.close();
}
}
class Km2Mile extends Converter{
public Km2Mile(double ratio){ this.ratio = ratio;}
protected double convert(double src) {return src/ratio;}
protected String getSrcString() {return "Km";}
protected String getDestString() {return "mile";}
}
public class num_4 {
public static void main(String[] args) {
Km2Mile toMile = new Km2Mile(1.6);
toMile.run();
}
}
|
5. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class ColorPoint extends Point{
private String Color = null;
public ColorPoint(int x, int y, String Color){
super(x,y);
this.Color = Color;
}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String Color) {this.Color = Color;}
public String toString() {
return Color + "색의 (" + getX() + "," + getY() +")의 점";
}
}
public class num_5 {
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다.");
}
}
|
6. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class ColorPoint extends Point{
private String Color = null;
public ColorPoint() {super(0,0); Color="BLACK";}
public ColorPoint(int x, int y){super(x,y);}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String Color) {this.Color = Color;}
public String toString() {
return Color + "색의 (" + getX() + "," + getY() +")의 점";
}
}
public class num_6 {
public static void main(String[] args) {
ColorPoint zeroPoint = new ColorPoint();
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint cp = new ColorPoint(10, 10);
cp.setXY(5, 5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다.");
}
}
|
7. Point를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class Point3D extends Point{
private int z = 0;
public Point3D(int x, int y, int z){super(x,y); this.z = z;}
public void setXY(int x, int y) {move(x,y);}
public void move(int x, int y, int z) {move(x,y); this.z = z;}
public void moveUp() {z++;}
public void moveDown() {z--;}
public String toString() {return "(" + getX() + "," + getY() + "," + z +")의 점";}
}
public class num_7 {
public static void main(String[] args) {
Point3D p = new Point3D(1, 2, 3);
System.out.println(p.toString() + "입니다.");
p.moveUp();
System.out.println(p.toString() + "입니다.");
p.moveDown();
p.move(10,10);
System.out.println(p.toString() + "입니다.");
p.move(100, 200, 300);
System.out.println(p.toString() + "입니다.");
}
}
|
8. Point를 상속받아 양수의 공간에서만 점을 나타내는 PostivePoint 클래스를 작성하라.
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
|
class Point{
private int x,y;
public Point (int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
class PositivePoint extends Point{
public PositivePoint(){ super(0,0);}
public PositivePoint(int x, int y) {
super(0,0);
move(x,y);
}
protected void move(int x, int y) {
if(x>0 && y>0){
super.move(x,y);
}
}
public void setXY(int x, int y) {move(x,y);}
public String toString() {return "(" + getX() + "," + getY() + ")의 점";}
}
public class num_8 {
public static void main(String[] args) {
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString() + "입니다.");
p.move(-5, 5);
System.out.println(p.toString() + "입니다.");
PositivePoint p2 = new PositivePoint(-10, -10);
System.out.println(p2.toString() + "입니다.");
}
}
|
9. 다음 Stack 인터페이스를 상속받아 실수를 실수를 저장하는 StringStack 클래스를 구성하라.
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
|
import java.util.Scanner;
interface Stack{
int length();
int capacity();
String pop();
boolean push(String val);
}
class StringStack implements Stack{
private String Stack[];
private int top =0;
public StringStack(int number) {Stack = new String[number];}
// 현재 스택에 저장된 개수 리턴
public int length() {return top;}
// 전체 저장 가능한 개수 리턴
public int capacity() {return Stack.length;}
// 스택의 톱(top)에 실수 저장
public String pop() {
if(top==0) {
return "빈 상태";
}
else {
String stay = Stack[top-1];
top--;
return stay;
}
}
// 스택의 톱(top)에 저장된 실수 리턴
public boolean push(String val) {
if (top==Stack.length) {
return false;
}
else{
Stack[top] =val;
top++;
return true;
}
}
}
public class num_9 {
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int number = sc.nextInt();
StringStack ss = new StringStack(number);
while(true) {
System.out.print("문자열 입력 >> ");
String text = sc.next();
if(text.equals("그만")) {break;}
boolean isCheck = ss.push(text);
if(isCheck ==false) {System.out.println("스택이 꽉 차서 푸시 불가!");}
}
int textPop = ss.length();
for(int i=0;i<textPop;i++) {
System.out.print(ss.pop() +" ");
}
}
public static void main(String[] args) {
num_9 n = new num_9();
n.run();
}
}
|
10. PairMap을 상속받는 Dictionary 클래스를 구현하고, 이를 다음과 같이 활용하는 main() 메소드를 가진 클래스 DictionaryApp도 작성하라.
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
|
abstract class PairMap{
protected String keyArray[];
protected String valueArray [];
abstract String get(String key);
abstract void put(String key, String value);
abstract String delete(String key);
abstract int length();
}
class Dictionary extends PairMap{
private int count=0;
public Dictionary(int array) {
keyArray = new String [array];
valueArray = new String[array];
}
@Override
public String get(String key) {
for(int i=0; i<count; i++) {
if(key.equals(keyArray[i])){
return valueArray[i];
}
}
return null;
}
@Override
public void put(String key, String value) {
for(int i=0; i<count;i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = value;
return;
}
}
keyArray[count] = key;
valueArray[count] = value;
count++;
}
@Override
public String delete(String key) {
for(int i=0; i<count;i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = null;
return null;
}
}
return key;
}
@Override
public int length() {return count;}
}
public class num_10 {
public static void main(String[] args) {
Dictionary dic = new Dictionary(10);
dic.put("황기태", "자바");
dic.put("이재문", "파이선");
dic.put("이재문", "C++");
System.out.println("이재문의 값은 " + dic.get("이재문"));
System.out.println("황기태의 값은 " + dic.get("황기태"));
dic.delete("황기태");
System.out.println("황기태의 값은 " + dic.get("황기태"));
}
}
|
11. 철수 학생은 다음 3개의 필드와 메소드를 가진 4개의 클래스 Add, Sub, Mul, Div를 작성하려고 한다.(4장 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
|
import java.util.Scanner;
abstract class Calc{
protected int a;
protected int b;
abstract void setValue(int a, int b);
abstract int calculate();
}
class Add extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a+b;}
}
class Sub extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a-b;}
}
class Mul extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a*b;}
}
class Div extends Calc{
public void setValue(int a, int b) {this.a = a; this.b =b;}
public int calculate() {return a/b;}
}
public class num_11 {
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
String op = sc.next();
switch(op.charAt(0)) {
case '+':
Add add = new Add();
add.setValue(a, b);
System.out.println(add.calculate());
break;
case '-':
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(sub.calculate());
break;
case '*':
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(mul.calculate());
break;
case '/':
Div div = new Div();
div.setValue(a, b);
System.out.println(div.calculate());
break;
default:
System.out.println("잘못 된 값입니다.");
break;
}
}
public static void main(String[] args) {
num_11 num_11 = new num_11();
num_11.run();
}
}
|
12. 텍스트로 입출력하는 간단한 그래픽 편집기를 만들어보자. 추상 클래스인 Shape, Line, Rect, Circle 클래스 코드를 잘 완성하고 "삽입", "삭제", "모두 보기", "종료" 4가지 그래픽 편집 기능을 가진 클래스 GraphicEditor을 작성하라.
public class가 같은 코드 내에 중복할 수 없으므로 java파일을 분할해서 작성했습니다. 또한 12번 문제는 이전 문제에서 Stack이 나온 것과 같이 구조체로 Linkedlist가 나왔다는 가정하에 풀었습니다. 또한 추상화 클래스와 메인 클래스를 분리해서 푼 문제이므로 하나의 클래스에서 선언하려면 추상 클래스의 public 클래스를 제외하고 코드를 사용하셔야 합니다.
public abstract class Shape
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
|
public abstract class Shape{
private Shape next;
public Shape() {next = null;}
public void setNext(Shape obj) {next = obj;}
public Shape getNext() {return next;}
public abstract void draw();
}
class Line extends Shape{public void draw() {System.out.println("Line");}}
class Rect extends Shape{public void draw() {System.out.println("Rect");}}
class Circle extends Shape{public void draw() {System.out.println("Circle");}}
class Editor{
private Shape head;
private Shape tail;
public Editor(){
this.head = null;
this.tail = null;
}
public void Insert(int insert) {
Shape obj = null;
switch(insert) {
case 1:
obj = new Line();
break;
case 2:
obj = new Rect();
break;
case 3:
obj = new Circle();
break;
default:
System.out.println("잘못 된 값을 입력하셨습니다.");
break;
}
if (head==null) {
head = obj;
tail = obj;
}
else {
tail.setNext(obj);
tail = obj;
}
}
public void Delete(int del) {
Shape objHead = head;
Shape objTail = tail;
Shape objReverse = head;
try {
if(objHead==null) {
System.out.println("삭제할 수 없습니다.");
return;
}
if (del<1) {
System.out.println("잘못된 값을 입력하셨습니다.");
return;
}else if(del==1) {
if(head==tail){
objHead = null;
objTail = null;
}
else {
head=head.getNext();
return;
}
}
if(del>1) {
if(objHead!=null) {
for(int i=1;i<del;i++){
objReverse = objHead;
objHead = objHead.getNext();
}
}
if(objHead==objTail) {
tail=objReverse;
tail.setNext(null);
}else {
objReverse.setNext(objHead.getNext());
}
}
}catch(NullPointerException e){
System.out.println("삭제할 수 없습니다.");
}
}
public void printAll() {
Shape obj = head;
while(obj != null){
obj.draw();
obj = obj.getNext();
}
}
}
|
main class
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
|
import java.util.Scanner;
public class GraphicEditor {
Scanner sc = new Scanner(System.in);
Editor et = new Editor();
public GraphicEditor() {
System.out.println("그래픽 에디터 beauty을 실행합니다.");
}
public void run() {
while(true) {
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
int num = sc.nextInt();
switch(num){
case 1:
System.out.print("Line(1), Rect(2), Circle(3)>>");
int insert = sc.nextInt();
et.Insert(insert);
break;
case 2:
System.out.print("삭제할 도형의 위치>>");
int del = sc.nextInt();
et.Delete(del);
break;
case 3:
et.printAll();
break;
case 4:
System.out.println("beauty을 종료합니다.");
return;
default:
System.out.println("잘못 된 값을 입력하셨습니다.");
break;
}
}
}
public static void main(String[] args) {
GraphicEditor ge = new GraphicEditor();
ge.run();
}
}
|
13. 인터페이스 Shape을 구현한 클래스 Circle를 작성하고 전체 프로그램을 완성하라.
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
|
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다.");
draw();
}
}
class Circle implements Shape {
private int size=0;
public Circle(int size){this.size = size;}
@Override
public void draw() {
System.out.println("반지름이 " + size +"인 원입니다.");
}
@Override
public double getArea() {
double cal = size * size * PI;
return cal;
}
}
public class num_13 {
public static void main(String[] args) {
Shape donut = new Circle(10);
donut.redraw();
System.out.println("면적은 " + donut.getArea());
}
}
|
14. 문제 13의 Shape 인터페이스를 구현한 클래스 Oval, Rect를 추가 작성하고 전체 프로그램을 완성하라.
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
|
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다."); draw();
}
}
class Circle implements Shape {
private int size=0;
public Circle(int size){this.size = size;}
@Override
public void draw() {System.out.println("반지름이 " + size +"인 원입니다.");}
@Override
public double getArea() {
double calCircle = size * size * PI;
return calCircle;
}
}
class Oval implements Shape{
private int x;
private int y;
public Oval(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void draw() {System.out.println(x + "X" + y +"에 내접하는 타원입니다.");}
@Override
public double getArea() {
double calOval = x * y * PI;
return calOval;
}
}
class Rect implements Shape{
private int x;
private int y;
public Rect(int x, int y){
this.x = x;
this.y = y;
}
@Override
public void draw() {System.out.println(x+ "X" + y + "크기의 사각형 입니다.");}
@Override
public double getArea() {
double calRect = x * y;
return calRect;
}
}
public class num_14 {
public static void main(String[] args) {
Shape []list = new Shape[3];
list[0] = new Circle(10);
list[1] = new Oval(20, 30);
list[2] = new Rect(10, 40);
for(int i=0; i<list.length; i++) list[i].redraw();
for(int i=0; i<list.length; i++) System.out.println("면적은 " + list[i].getArea());
}
}
|
오탈자가 있다면 그것에 대한 댓글 감사드립니다. 또한 코드에 이해가 안가는 부분이 있으신 경우에도 댓글 주시면 최대한 아는 부분에서 설명드리겠습니다. 감사드립니다.
다음 포스팅 때 뵙겠습니다.
'개인 학습용 해설 > 명품 Java Programming' 카테고리의 다른 글
[Java] 명품 JAVA Programming 4판 6장 연습문제 - 이론문제 (4) | 2022.04.16 |
---|---|
명품 JAVA Programming 4판 5장 연습문제 - 이론문제 (0) | 2021.12.04 |
명품 JAVA Programming 4판 4장 연습문제 - 실습문제 (0) | 2021.11.18 |
명품 JAVA Programming 4판 4장 연습문제 - 이론문제 (4) | 2021.11.15 |
명품 JAVA Programming 4판 3장 연습문제 - 실습문제 개인 풀이 (0) | 2021.11.08 |