비트교육_단기과정 Generic Memory - package week_2; import java.util.Scanner; public class MemoryExe { public static void main(String[] args) { int num; Scanner sc = new Scanner(System.in); // Memory mr = null; while (true) { System.out.println("입력할 값의 타입을 정하시오"); System.out.println("1.INT 2.String ==>"); num = sc.nextInt(); System.out.print("1. 스택 2. 큐 => "); int num2 = sc.nextInt(); if (num == 1) {// 입력값이 int if (num2 == 1) { Memory<Integer> mr = new MyStack<Integer>(new Integer[5]); mainExe(mr, sc, num); } else { Memory<Integer> mr = new MyQueue<Integer>(new Integer[5]); mainExe(mr, sc, num); } } else {// 입력값이 String if (num2 == 1) { Memory<String> mr = new MyStack<String>(new String[5]); mainExe(mr, sc, num); } else { Memory<String> mr = new MyQueue<String>(new String[5]); mainExe(mr, sc, num); } } } } public static void mainExe(Memory mr, Scanner sc, int num) { out: while (true) { System.out.print("1. push 2. pop 3. 나가기 => "); int value = sc.nextInt(); switch (value) { case 1: System.out.print("값을 입력하시오 : "); if (num == 1) { mr.push(sc.nextInt()); } else { mr.push(sc.next()); } break; case 2: System.out.println("출력값 : " + mr.pop()); break; case 3: break out; default: System.out.println("형식에 맞지않습니다"); break; } } } } Colored by Color Scriptercs package week_2; public class MyStack<T> extends Memory<T> { public MyStack(T[] values) { super(values); } @Override public T pop() { if (cnt > 0) { return values[--cnt]; } return null; } } Colored by Color Scriptercs package week_2; public abstract class Memory<T> { protected T[] values; protected int cnt; public Memory(T[] values) { this.values = values; } public final void push(T i) {// override 금지 if (cnt < values.length) { values[cnt++] = i; } else { System.out.println("여유공간이 없습니다"); } }; public abstract T pop(); } Colored by Color Scriptercs package week_2; public class MyQueue<T> extends Memory<T> { public MyQueue(T[] values) { super(values); } @Override public T pop() { if (cnt > 0) { T value = values[0]; for (int i = 0; i < values.length - 1; i++) { values[i] = values[i + 1]; } cnt--; return value; } else return null; }} Colored by Color Scriptercs 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기'기'발하고 '창'의적인 블로그 Contents 당신이 좋아할만한 콘텐츠 Set<E> 성적관리 2022.07.08 List를 사용한 성적관리 2022.07.07 스레드 동기화(ATM 번갈아서) : 마기창 2022.07.05 12장_추상 클래스와 인터페이스 연습문제 풀이 2022.07.04 댓글 0 + 이전 댓글 더보기