본문 바로가기

Java/문제

Java_Stack,Queue 상속으로 구현

// 부모클래스로 기본적이 사항만 가지고 있다.

package Day3;

public abstract class Memory { // 추상 클래스

protected int[] area;

protected int input;

protected Memory(int size) {

area = new int[size];

input = 0;

}

// push와 pop은 추상 메소드로 자식 클래스에서는 반드시 정의해야한다.

abstract public void push(int data);// 추상 메소드

abstract public int pop(); // 추상 메소드

}

----------------------------------------------------------------------------------------------------------------------

//Stack클래스로 Memory클래스를 상속받아 구현

package temporary;

public class Stack_Array extends Day3.Memory {

public Stack_Array(int size) {

super(size);

}

public Stack_Array() {

this(5);

}

@Override

public void push(int data) {

if(input < area.length) {

area[input] = data;

input++;

}else {

System.out.println("Stack이 가득찼습니다.\n더 이상 삽입 할 수 없습니다.");

}

}//end method push

@Override

public int pop() {

if(input > 0) {

input--;

return area[input];

}else {

System.out.println("Stack에 값이 없습니다.\n더 이상 출력할 수 없습니다.");

return 0;

}

}//end method pop

}// end class Stack_Array

--------------------------------------------------------------------------------------------------------------

// Queue클래스로 Memory클래스를 상속받아 구현

package temporary;

public class Queue_Array extends Day3.Memory {

// 필드

int output;

int item;

// 생성자

public Queue_Array(int size) {

super(size);

output = 0;

item = 0;

}

public Queue_Array() {

this(5);

}

// 메소드

@Override

public void push(int data) {

if (item < area.length) {

area[input] = data;

item++;

input = ++input % area.length;

} else {

System.out.println("Queue에 저장할 공간이 없습니다.");

}

}// end method push

@Override

public int pop() {

int re = 0;

if (item > 0) {

item--;

re = area[output];

output = ++output%area.length;

return re;

} else {

System.out.println("Queue에 값이 없습니다.");

return re;

}

}//end method pop

}// end class Queue_Array

------------------------------------------------------------------------------------------------------------------

// Stack클래스과 Queue클래스를 생성하여 Memory타입으로 변환시켜 사용

package Day3;

import java.util.Scanner;

import temporary.Queue_Array;

import temporary.Stack_Array;

public class Memory_Main {

public static void main(String[] args) {

Memory m;

@SuppressWarnings("resource")

Scanner inp = new Scanner(System.in);

int respone = 0;

first: while (true) {

System.out.println("1. stack \t 2. queue");

respone = inp.nextInt();

if (respone == 1) {

m = new Stack_Array();

} else if (respone == 2) {

m = new Queue_Array();

} else {

System.out.println("잘못입력하였습니다. 다시 입력해주세요.");

continue;

}

second : while (true) {

System.out.println("1. push \t 2. pop");

respone = inp.nextInt();

if (respone == 1) {

System.out.println("값을 입력해 주세요");

m.push(inp.nextInt());

} else if (respone == 2) {

System.out.println(m.pop());

} else {

System.out.println("잘못 입력하셨습니다. 다시입력해 주세요");

continue;

}

System.out.println("1. 삽입,삭제로 \n2. 처음으로 \n3. 종료 ");

switch (inp.nextInt()) {

case 1:

continue;

case 2 :

break second;

case 3 :

break first;

default :

System.out.println("잘못 입력했습니다. 삽입, 삭제를 다시 합니다.");

}

} // end while

} // end while

}// end method main

}// end method main

'Java > 문제' 카테고리의 다른 글

[자료구조]힙  (0) 2019.06.19
여러 쓰레드에서 하나의 변수에 차례로 접근하  (0) 2019.06.12
List 구현(Java)  (0) 2019.06.11
[Java] 별출력하기  (0) 2019.06.10
급여정보조회 프로그램  (0) 2019.06.10