the space coding
▶ Stack / Queue
(클래스을 나눠 주었습니다.제사용성,확장성,유지성을 위해서! )
-크래스를 한쪽을로 몰아 버리면 무거운 것도 있지만 코드보는데 불편함이 있다.
-Stack / Queue의 개념을 이해하고 코드를 바라 보기 바란다.
1.class SunYoung 메인 클래스 입니다.
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 |
public class SunYoung {
public static void main(String[] args) {
Memory m = null;//주석달자
MyStack ms = new MyStack();
MyQueue mq = new MyQueue();
Scanner s = new Scanner(System.in);
int c = 0;
while(c!=3) {
System.out.println("1.스택 2.큐");
System.out.print("choes : ");
c = s.nextInt();
switch(c) {
case 1 : //stack
m = ms;
System.out.println("1.Push 2.Pop");
System.out.print("choes : ");
int sc = s.nextInt();
if(sc == 1) {
System.out.print("입력 : ");
m.push(s.nextInt());
}
if(sc == 2) {
System.out.println(m.pop());
}
break;
case 2 : //queue
m = mq;
System.out.println("1.Push 2.Pop");
System.out.print("choes : ");
int qc = s.nextInt();
if(qc == 1) {
System.out.print("입력 : ");
m.push(s.nextInt());
}
if(qc == 2) {
System.out.println(m.pop());
}
break;
}
}
s.close();
}
}
|
cs |
2 . abstract class Memory (super class)(따로 빼줌)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
abstract class Memory {
protected int[] m;
protected int count;
Memory(){
m = new int[20];
count = 0;
}
public void push(int i) {
if(full()) {
m[count++] = i;
}
}
abstract int pop();
abstract boolean full();
abstract boolean empty();
} |
cs |
3.class MyStack extends Memory (SubClass)
(MyStack= is a관계를 보여준다.)
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 MyStack extends Memory{
@Override
public int pop() {
if(empty()) {
return m[--count];
}
else
return -1;
}
@Override
public boolean full() {
if(count == 20) {
System.out.println("꽉 찼습니다");
return false;
}
else
return true;
}
@Override
public boolean empty(){
if(count == 0) {
System.out.println("텅비었습니다");
count = 0;
return false;
}
else
return true;
}
} |
cs |
5.class MyQueue extends Memory(Has a관계를 보여준다.)
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 MyQueue extends Memory{
private int front;
public MyQueue() {
front = 0;
}
@Override
public int pop() {
if(empty()) {
return m[front];
}
else
return -1;
}
@Override
public boolean full() {
if(count == 20 && front == 0) {
System.out.println("꽉 찼습니다");
return false;
}
return true;
}
@Override
public boolean empty() {
if(count == front) {
System.out.println("텅비었습니다");
count = front = 0;
return false;
}
else
return true;
}
} |
cs |
'JAVA' 카테고리의 다른 글
성적 관리 프로그램 (0) | 2018.04.12 |
---|---|
AVL트리 vs 편향트리 (0) | 2018.03.27 |
[JAVA][래퍼클래스] (0) | 2018.03.22 |
JAVA 단 축 키 (0) | 2018.03.21 |
[java][이칙연산 계산기 (+,-)] (0) | 2018.03.20 |