\
classMemory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 |
class Stack extends Memory 메모리 한테서 상속
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 | public class Stack extends Memory { @Override int pop() { if(empty()) { return m[--count]; } else return -1; } @Override boolean full() { if(count == 20) { System.out.println("Full"); return false; } else { return true; } } @Override boolean empty() { if(count==0) { System.out.println("엠프리"); count = 0; return false; } else return true; } } | cs |
class Queue extends Memory
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 | public class Queue extends Memory{ int front; Queue(){ front =0; } @Override int pop() { if (empty()) { return m[front]; } else { return -1; } } @Override boolean full() { if (count == 20 && front ==0) { System.out.println("가득"); return false; } else { return true; } } @Override boolean empty() { if(count == front) { System.out.println("엠프리"); count = front =0; return false; } else return true; } } | cs |
class SQmgn (mian)
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 | import java.util.Scanner; public class SQmgn { public static void main(String[] args) { Memory m = null; //초기화를 해주어야한다 Stack ms = new Stack(); Queue mq = new Queue(); Scanner s = new Scanner(System.in); int c = 0; while(c!=3) { System.out.println("1.Stack 2.Queue"); System.out.print("선택 : "); c = s.nextInt(); switch(c) { case 1 : //stack m = ms; System.out.println("1.Push 2.Pop"); System.out.print("선택 : "); 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("선택 : "); 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 |
finally(예외처리)/미완성 (0) | 2018.03.22 |
---|---|
어나니머스(Anonymous)class (6) | 2018.03.22 |
LocerClass( ) (3) | 2018.03.22 |
[자바]inner class ( static) 코드 예제 (0) | 2018.03.22 |
내부 클래스 사용하기 (inner class) (0) | 2018.03.22 |