본문 바로가기

JAVA/공부

5. 상속

10. PairMap을 상속받는 Dictionary 클래스를 구현하고, 이를 다음과 같이 활용하는 main() 메소드를 가진 클래스 DictionaryApp도 작성하라.

 

PairMap과 Main() 메소드 주어짐

 

abstract class PairMap {
   protected String KeyArray[];
   protected String valueArray[]; 
   abstract String get(String key); // key 값을 가진 value 리턴, 없으면 null 리턴
   abstract void put(String ket, String value);
   abstract String delete(String key); // key 값을 가진 아이템 (value와 함꼐) 삭제, 삭제된 value 값 리턴
   abstract int length();
}
class Dictionary extends PairMap{
	private int i;
	public Dictionary(int num) {KeyArray=new String[num];valueArray=new String[num];this.i=0;}
	String get(String key) {
		String value = null;
		try{for(int k=0;k<i;k++)
			if(KeyArray[k].equals(key))
				value=valueArray[k];}catch(Exception NullPointerException) {
					return null;
				}
		return value;
	}
	void put(String key, String value) {
		for(int k=0;k<i;k++) {
			if(KeyArray[k].equals(key))
				valueArray[k]=value;
		}
		KeyArray[i]=key;valueArray[i]=value;i++;
	}
	String delete(String key) {
		String value=null;
		for(int k=0;k<i;k++) {
			if(KeyArray[k].equals(key)) {
				value=valueArray[k];
				KeyArray[k]=null;
				valueArray[k]=null;}
			}i--;return value;}
	int length() {
		return i;
	}
}
public class DictionaryApp{
	public static void main(String[]args) {
		 Dictionary dic = new Dictionary(10);
		   dic.put("황기태", "자바");
		   dic.put("이재문", "파이선");
		   dic.put("이재문", "C++"); // 이재문의 값을 C++로 수정
		   System.out.println("이재문의 값은 "+dic.get("이재문"));
		   System.out.println("황기태의 값은 "+dic.get("황기태"));
		   dic.delete("황기태"); // 황기태 아이템 삭제
		   System.out.println("황기태의 값은 "+dic.get("황기태")); //삭제된 아이템 접근
		   System.out.println("배열의 요소 개수는 "+dic.length());
	}
}

 

 

 

12. 텍스트로 입출력하는 간단한 그래픽 편집기를 만들어보자. "삽입", "삭제", "모두 보기", "종료"의 4가지 그래픽 편집 기능을 가진 클래스 GraphicEditor을 작성하라.

 

import java.util.Scanner;
public class GraphicEditor {
	public static void main(String[]args) {
		System.out.println("그래픽 에디터 beauty을 실행합니다.");
		Scanner sc=new Scanner(System.in);
		String []s = new String[10];
		int i=0;
		while(true) {
			System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
			int select=sc.nextInt();
			if(select==4)
				break;
			switch(select) {
			case 1:
				System.out.print("Line(1), Rect(2), Circle(3)>>");
				int select2=sc.nextInt();
				if(select2==1)
					{s[i]="Line";i++;}
				else if(select2==2)
				{s[i]="Rect";i++;}
				else if(select2==3)
				{s[i]="Circle";i++;}	
				break;
			case 2:
				System.out.print("삭제할 도형의 위치>>");
				select2=sc.nextInt();
				if(select2>i)
					System.out.println("삭제할 수 없습니다.");
				else
				{s[select2]=null;
				i--;}
				break;
			case 3:
				for(int k=0;k<i;k++)
					System.out.println(s[k]);
			}
		}
		System.out.println("beauty을 종료합니다.");
		sc.close();
	}
}

 

 

 

14. 다음 main() 메소드와 실행 결과를 참고하여, Shape 인터페이스를 구현한 클래스 Oval, Rect를 추가 작성하고 전체 프로그램을 완성하라.

 

Shape 인터페이스는 문제 13번에서 구현, main() 메소드 주어짐

 

interface Shape {
   final double PI = 3.14; // 상수
   void draw(); // 도형을 그리는 추상 메소드
   double getArea(); // 도형의 면적을 리턴하는 추상 메소드
   default public void redraw() { // 디폴트 메소드
      System.out.print("--- 다시 그립니다.");
      draw();
   }
}
class Circle implements Shape{
	private int radius;
	public Circle(int radius) {this.radius=radius;}
	public void draw() {System.out.println(" 반지름이 "+radius+"인 원입니다.");}
	public double getArea() {return radius*radius*PI;}
	public void redraw() {System.out.print("--- 다시 그립니다.");
    draw();}
}
class Oval implements Shape{
	private int x,y;
	public Oval(int x,int y) {this.x=x;this.y=y;}
	public void draw() {System.out.println(" "+x+"x"+y+"에 내접하는 타원입니다.");}
	public double getArea() {return PI*(double)(x/2)*(double)(y/2);}
	public void redraw() {System.out.print("--- 다시 그립니다.");
	draw();}
}
class Rect implements Shape{
	private int x,y;
	public Rect(int x,int y) {this.x=x;this.y=y;}
	public void draw() {System.out.println(" "+x+"x"+y+"크기의 사각형 입니다.");}
	public double getArea() {return x*y;}
	public void redraw() {System.out.print("--- 다시 그립니다.");
	draw();}
}
public class fourteen {
	public static void main(String[] args) {
		   Shape[] list = new Shape[3]; // Shape을 상속받은 클래스 객체의 레퍼런스 배열
		   list[0] = new Circle(10); // 반지름이 10인 원 객체
		   list[1] = new Oval(20, 30); // 20x30 사각형에 내접하는 타원
		   list[2] = new Rect(10, 40); // 10x40 크기의 사각형
		   for(int i=0; i<list.length; i++) list[i].redraw();
		   for(int i=0; i<list.length; i++) System.out.println("면적은 "+ list[i].getArea());
		}
}

 

 

'JAVA > 공부' 카테고리의 다른 글

7. 제네릭과 컬렉션  (0) 2021.01.16
6. 모듈과 패키지 개념, 자바 기본 패키지  (0) 2021.01.14
4. 클래스와 객체  (0) 2021.01.14
3. 반복문과 배열 그리고 예외 처리  (0) 2021.01.13
2. 자바 기본 프로그래밍  (0) 2021.01.13