반응형

이전 문제까지는 명품JAVA 개정판이였고 이번 문제부터 개정 4판입니다.

 

문제

Vector<Shape>의 벡터를 이용하여 그래픽 편집기를 만들어보자. 본문 5.6절과 5.7절에서 사례로 든 추상 클래스 Shape과 Line, Rect, Circle 클래스 코드를 잘 완성하고 이를 활용하여 "삽입", "삭제", "모두 보기", "종료"의 4가지 그래픽 편집 기능을 프로그램으로 작성하라. 6장 실습문제 6번을 Vector<Shape>을 이용하여 재작성하는 연습이다. Vector를 이용하면 6장 실습문제 6번보다 훨씬 간단히 작성됨을 경험할 수 있다.

출력 예시

그래픽 에디터 beauty을 실행합니다.
삽입(1), 삭제(2), 모두보기(3), 종료(4) >> 1
Line(1), Rect(2), Circle(3) >> 2
삽입(1), 삭제(2), 모두보기(3), 종료(4)>>1
Line(1), Rect(2), Circle(3) >> 3
삽입(1), 삭제(2), 모두보기(3), 종료(4)>>3
Rect
Circle
삽입(1), 삭제(2), 모두보기(3), 종료(4)>>2
삭제할 도형의 위치 >> 3
삭제할 수 없습니다.
삽입(1), 삭제(2), 모두보기(3), 종료(4)>>4
beauty을 종료합니다.

소스 코드

import java.util.Scanner;
import java.util.Vector;

abstract class Shape{
	public Shape() {};
	public abstract String draw(); 
}
class LineTen extends Shape{
	@Override
	public String draw() {
		return "Line";
	}
}
class RectTen extends Shape{
	@Override
	public String draw() {
		return "Rect";
	}
}
class CircleTen extends Shape{
	@Override
	public String draw() {
		return "Circle";
	}
}

class GraphicEditorbeauty{
	Vector<Shape> v = new Vector<Shape>();
	
	public void Menu() {
		Scanner s = new Scanner(System.in);
		System.out.println("그래픽 에디터 beauty을 실행합니다.");
		while(true) {
		System.out.print("삽입(1), 삭제(2), 모두보기(3), 종료(4)>>");
		int input = s.nextInt();
		switch(input) {
		case 1:
			addshape(s);
			break;
		case 2:
			removeshape(s);
			break;
		case 3:
			Print();
			break;
		case 4:
			System.out.println("beauty을 종료합니다.");
			System.exit(0);
			break;
		default:
			break;
		}
		}
	}
	private void Print() {
		for(int i = 0;i<v.size();i++)
		{
			System.out.println(v.get(i).draw());
		}
	}
	private void addshape(Scanner s) {
		System.out.print("Line(1), Rect(2), Circle(3) >> ");
		int input = s.nextInt();
		switch(input) {
		case 1:
			LineTen L = new LineTen();
			v.add(L);
			break;
		case 2:
			RectTen R = new RectTen();
			v.add(R);
			break;
		case 3:
			CircleTen C = new CircleTen();
			v.add(C);
			break;
		default:
			System.out.println("잘못된 입력");
			break;
		}
	}
	private void removeshape(Scanner s) {
		System.out.println("삭제할 도형의 위치 >> ");
		int remove = s.nextInt();
		if(v.size() <= remove) {
			System.out.println("삭제할 수 없습니다.");
		}
		else
			v.remove(remove);
	}
	
}

public class Main10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GraphicEditorbeauty GE = new GraphicEditorbeauty();
		GE.Menu();
	}

}
반응형
반응형

문제

하나의 학생 정보는 Student 클래스 표현한다. Student 클래스에는 이름, 학과, 학번, 학점 평균을 나타내는 필드가 있다. 여기서 학번을 String 타입으로 선언한다. 키보드에서 학번 정보를 5개 입력받아 학번을 '키' 로 하여 HashMap<String, Student>에 저장하고 학번으로 학생 정보를 검색하여 출력하는 프로그램을 작성하라. 다음과 같은 해시맵을 이용하라.

 

예시 해쉬맵

HashMap<String, Student> dept = new HashMap<String, Student>();

 

소스 코드

import java.util.HashMap;
import java.util.Scanner;

class Student{
	private String name;
	private String department;
	private double Score;
	Student(String name,String department,double Score){
		this.name = name;
		this.department = department;
		this.Score = Score;
	}
	public void Print(){
		System.out.println("학생 이름 : "+name);
		System.out.println("학과 이름 : "+department);
		System.out.println("학점 : "+Score);
	}
}

public class Main9 {
	public static void main(String[] args) {
		HashMap<String, Student> dept = new HashMap<String,Student>();
		Scanner s = new Scanner(System.in);
		System.out.println("학생 5명의 정보를 입력하시오");
		for(int i = 0;i<5;i++)
		{
			System.out.print("학생 이름 : ");
			String name = s.nextLine();
			System.out.print("학과 이름 : ");
			String department = s.nextLine();
			System.out.print("학번 : ");
			String studentID = s.nextLine();
			System.out.print("학점 : ");
			double score = s.nextDouble();
			s.nextLine();
			Student temp = new Student(name,department,score);
			dept.put(studentID,temp);
		}
		
		System.out.print("검색할 학생의 학번을 입력하시오 : ");
		String find = s.nextLine();
		
        System.out.println();
       	
		if(dept.containsKey(find)) {
			System.out.println("학번 : " + find);
			dept.get(find).Print();
		}
		else {
			System.out.println("학생이 존재하지 않습니다.");
		}
		s.close();
	}

}

 

결과

학생 5명의 정보를 입력하시오
학생 이름 : 유관순
학과 이름 : 독립
학번 : 19021216
학점 : 4.5
학생 이름 : 이순신
학과 이름 : 무예
학번 : 15450428
학점 : 4.5
학생 이름 : 세종대왕
학과 이름 : 훈민정음
학번 : 13970515
학점 : 4.5
학생 이름 : 백범 김구
학과 이름 : 독립
학번 : 18760829
학점 : 4.5
학생 이름 : 안중근
학과 이름 : 독립
학번 : 18790902
학점 : 4.5

검색할 학생의 학번을 입력하시오 : 18790902
학번 : 18790902
학생 이름 : 안중근
학과 이름 : 독립
학점 : 4.5
반응형
반응형

문제

다음은 String만 다루는 MyClass 코드이다. MyClass를 제네릭 클래스 MyClass<E>로 일반화하고, 이를 이용하는 main() 메소드를 만들어 프로그램을 완성하라.

문제 소스 코드

public class MyClass {
	private String s;
	public MyClass(String s) {
		this.s = s;
	}
	void setS(String s) {
		this.s = s;
	}
	String getS() {
		return s;
	}
}

 

소스 코드

public class MyClass<E> {
	private E s;
	public MyClass(E s) {
		this.s = s;
	}
	void setS(E s) {
		this.s = s;
	}
	E getS() {
		return s;
	}
	public static void main(String[] args) {
		int n = 10;
		MyClass<Integer> myclass = new MyClass<>(n);
		System.out.println(myclass.getS());
	}

}

 

결과

10
반응형

+ Recent posts