본문 바로가기

Programming/Java

명품 java programming 실습문제 : 인터페이스(3번), 추상클래스(6번)

반응형

실습문제

3. Stack 인터페이스를 구현하는 StringStack 클래스와 이를 실행하는 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
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
import java.util.*;
 
interface Stack{
    int length();
    Object pop(); //삭제
    boolean push(Object ob); //삽입
}
 
class StringStack implements Stack{
    private String[] element;
    private int index;
 
    public StringStack(){ //초기화
        element = new String[10];
        index = 0;
    }
 
    public int length(){
        return element.length;
    }
 
    public Object pop(){
        if(element == null) {
            System.out.println("No element in stack.");
            return null;
        }
        else if(index == 0return null;
        else{
            index --;
            return element[index];
        }
    }
 
    public boolean push(Object ob){
        if(index == length()) return false;
        else {
            element[index] = (String)ob;
            index++;
            return true;
        }
    }
 
}
 
 
public class Exercises {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        Stack stack = new StringStack();
 
        System.out.println("Please input 10 elements.");
        for(int i=0; i<stack.length(); i++){
            if(stack.push(s.next()) == falsebreak;
        }
 
        System.out.println("Full stack. Now start 'pop'");
        for(int i=0; i<stack.length(); i++){
            System.out.print(stack.pop() + " ");
        }
 
    }
}
 
cs

문제 자체에 별도의 설명이 없었기 때문에 main에서 사용자의 입력을 받아 stack에 push하고 이를 pop하며 차례로 return해주는 프로그램을 완성하였다. java에서는 배열 할당 시 배열의 크기를 지정해주고, 그 크기는 고정되어야하기 때문에 프로그램의 스택 크기도 10으로 임의로 설정하였다. 이 프로그램의 출력은 다음과 같다. 




6. 간단한 그래픽 편집기를 콘솔 바탕으로 만들어본다. Line, Rect, Circle의 도형객체는 DObject 클래스를 상속받아 draw() 메소드를 오버라이딩하도록 구성한다. 즉, DObject는 추상 메소드 draw()를 가진 추상클래스이다. 그래픽 편집기의 기능은 삽입, 삭제, 모두보기, 종료의 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
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
123
124
125
126
127
128
129
130
import java.util.*;
 
abstract class DObject{
    public DObject next; //다음 노드를 가리킴
    public DObject() {next = null;}
 
    public void setNext(DObject next) {
        this.next = next; //this는 현재 노드
    }
    public DObject getNext(){
        return next;
    }
 
    abstract public void draw();
}
 
class Line extends DObject{
    public void draw(){
        System.out.println("Line");
    }
}
 
class Rect extends DObject{
    public void draw(){
        System.out.println("Rect");
    }
}
 
class Circle extends DObject{
    public void draw(){
        System.out.println("Circle");
    }
}
 
 
 
public class Exercises {
    static DObject head = null;
    static DObject tail = null;
 
    public static void addGraphic(){ //삽입 메소드
        int submenu;
        DObject dobj;
        Scanner s = new Scanner(System.in);
 
        System.out.print("도형 종류 Line(1), Rect(2), Circle(3) >> ");
        submenu = s.nextInt();
 
        switch(submenu){
            case 1:
                dobj = new Line();
                break;
            case 2:
                dobj = new Rect();
                break;
            case 3:
                dobj = new Circle();
                break;
            default :
                System.out.println("Wrong type. Please try again.");
                return;
        }
        if(tail==null) { //리스트에 원소가 하나도 없다면
            head = tail = dobj; //첫번째 원소로 설정
        }
        else{
            tail.setNext(dobj); //마지막노드의 뒤에 삽입하기 위해 링크 설정
            tail = dobj; //삽입
        }
 
 
    }
 
    public static boolean delGraphic(int index){
        DObject pre = head;
        DObject current = head;
        if(head == nullreturn false//리스트에 원소가 없다면
        for(int i=0; i<index; i++){
            pre = current;
            current = pre.next;
            if(current == nullreturn false//존재하는 원소보다 인덱스가 클때
        }
        if(index == 0){ //1번째 위치의 원소 삭제 시
            head = head.next;
            return true;
        }
        pre.next = current.next;
        current = null
        return true;
    }
 
 
    //---main---
    public static void main(String[] args){
        int menu;
 
        Scanner s = new Scanner(System.in);
 
        System.out.println("**********Graphic Editor**********");
        while(true){
            System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4) >> ");
            menu = s.nextInt();
 
            switch(menu){
                case 1//삽입
                     addGraphic();
                     break;
                case 2//삭제
                     System.out.print("삭제할 도형의 위치 >> ");
                     boolean right = delGraphic((s.nextInt()-1));
                     if (right == false)
                         System.out.println("삭제할 수 없습니다.");
                     else System.out.println("삭제하였습니다. ");
                     break;
                case 3//모두보기
                     DObject d = head;
                     while(d != null){
                         d.draw();
                         d = d.getNext();
                     }
                     break;
                case 4//종료
                    System.out.println("exit.");
                    return;
 
            }
        }
    }
}
 
cs


자료구조의 linked list(단순 연결 리스트)를 자바로 구현하여 코드를 짰다. 이 프로그램을 구현하면서, 한 클래스에 대해 여러 객체를 생성하다보니 현재 접근하고 있는 객체가 무엇인가에 대해 집중하는 연습을 할 수 있었다. 대표적으로는 삭제 기능을 구현할 때를 말할 수 있겠다. 


삭제 기능을 수행한 뒤 -> (3)모두보기 기능을 수행하였을 때를 가정해보자.

1번째 원소를 삭제한다고 했을 때, delGraphic()의 pre.next = current.next; current = null; 코드만으로도 충분히 1번째 원소를 삭제할 수는 있다. 하지만 main()메소드의 case 3: 코드를 주목해보자. DObject d = head; 를 선언했고, 이 d객체를 이용하여 출력을 수행한다. ( d.draw(); ) 즉, DOBject에 대해 '모두보기' 기능을 수행하는 건 head가 기준이다. 아무리 delGraphic()에서 첫번째 요소를 삭제했다고 해도, head 값을 변경해주지 않으면 삭제 전과 똑같은 결과가 출력된다. 물론, 이는 첫번째 요소를 삭제했을 때만 고려해주면 되는 부분이다. head를 할당해줬으므로. 


그래서 delGraphic()메소드에 첫번째 원소를 삭제하고싶을 때 수행하는 조건문을 따로 넣어주어야한다. ( head = head.next; ) 만약 이 코드 앞에, head = null을 추가한다면, NullPointerException이 난다. 


이 프로그램에서 활용한 linked list 코드를 자바로 구현한 것에 대해서는 따로 포스팅하겠다. 다음은 위 코드를 수행한 결과이다. 



반응형