반응형

문제

나라와 수도 맞추기 게임의 예시는 다음과 같다.

**** 수도 맞추기 게임을 시작합니다 ****
입력:1, 퀴즈: 2, 종료:3 >> 1
현재 9개 나라와 수도가 입력되어 있습니다.
나라와 수도 입력 10 >> 한국 서울
나라와 수도 입력 11 >> 그리스 아테네
그리스는 이미 있습니다!
나라와 수도 입력 11 >> 호주 시드니
나라와 수도 입력 12 >> 이탈리아 로마
나라와 수도 입력 13 >> 그만
입력:1, 퀴즈:2, 종료:3>> 2
호주의 수도는? 시드니
정답!!
독일의 수도는? 하얼빈
아닙니다!!
멕시코의 수도는? 멕시코시티
정답!!
이탈리아의 수도는? 로마
정답!!
한국의 수도는? 서울
정답!!
영국의 수도는? 런던
정답!!
중국의 수도는? 그만
입렵:1, 퀴즈:2, 종료: 3 >> 3
게임을 종료합니다.

 

(1) 나라 이름(country)과 수도(capital)로 구성된 Nation클래스를 작성하고 Vector<Nation>컬렉션을 이용하여 프로그램을 작성하라.

 

소스코드

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

class Nation {
	String country;
	String capital;
	Nation(String country,String capital){
		this.country = country;
		this.capital = capital;
	}
	public String getcountry() {
		return country;
	}
	public String getcapital() {
		return capital;
	}
}
class CapitalGame{
	Vector<Nation> v = new Vector<Nation>();
	int count = 0;
	public void Menu() {
		System.out.println("**** 수도 맞추기 게임을 시작합니다 ****");
		Scanner s = new Scanner(System.in);
		while(true) {
			System.out.print("입력:1, 퀴즈: 2, 종료:3 >> ");
			int input = s.nextInt();
			switch(input) {
			case 1:
				insert(s);
				break;
			case 2:
				run(s);
				break;
			case 3:
				System.out.println("게임을 종료합니다");
				return;
			default:
				break;
			}
		}
	}
	private void insert(Scanner s) {
		
		while(true) {
		count ++;
		System.out.print("나라와 수도 입력 "+ count +" >>");
		String country = s.next();
		if(country.equals("그만"))
			break;
		if(Checking(country)){
			System.out.println(country + "는 이미 있습니다!");
			continue;
        }
        String capital = s.next();
		v.add(new Nation(country,capital));
		}
	}
	private void run(Scanner s) {
		for(int i =0;i<v.size();i++)
		{
			System.out.print(v.get(i).getcountry() + "의 수도는? ");
			String input = s.next();
			if(v.get(i).getcapital().equals(input))
				System.out.println("정답!!");
			else
				System.out.println("아닙니다!!");
		}
	}
	private boolean Checking(String country) {
		for(int i = 0;i<v.size();i++) {
			if(v.get(i).getcountry().equals(country))
					return true;
		}
		return false;
	}
}
public class Main11 {

	public static void main(String[] args) {
		CapitalGame CG  = new CapitalGame();
		CG.Menu();
	}

}

 

(2) 이 프로그램을 HashMap<String, String>을 이용하여 작성하라. '키' 는 나라 이름이고 '값'은 수도이다.

 

소스 코드

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

class CapitalGameHash{
	HashMap<String,String> h = new HashMap<String,String>();
	
	
	int count = 0;
	public void Menu() {
		System.out.println("**** 수도 맞추기 게임을 시작합니다 ****");
		Scanner s = new Scanner(System.in);
		while(true) {
			System.out.print("입력:1, 퀴즈: 2, 종료:3 >> ");
			int input = s.nextInt();
			switch(input) {
			case 1:
				insert(s);
				break;
			case 2:
				run(s);
				break;
			case 3:
				System.out.println("게임을 종료합니다");
				return;
			default:
				break;
			}
		}
	}
	private void insert(Scanner s) {
		
		while(true) {
		count ++;
		System.out.print("나라와 수도 입력 "+ count +" >>");
		String country = s.next();
		if(country.equals("그만"))
			break;
		if(Checking(country)) {
			System.out.println(country + "는 이미 있습니다!");
			continue;
		}
		String capital = s.next();
		h.put(country,capital);
		}
	}
	private void run(Scanner s) {
		Set<String> keys = h.keySet();
		
		Iterator<String> it = keys.iterator();
		
		while(it.hasNext()) {
			String temp = it.next();
			System.out.print(temp + "의 수도는? ");
			String input = s.next();
			if(h.get(temp).equals(input))
				System.out.println("정답!!");
			else
				System.out.println("아닙니다!!");
		}
		
	}
	private boolean Checking(String country) {
		for(int i = 0;i<h.size();i++) {
			if(h.containsKey(country))
					return true;
		}
		return false;
	}
}
public class Main11_1 {
	public static void main(String[] args) {
		CapitalGameHash CGH= new CapitalGameHash();
		CGH.Menu();
	}

}

 

반응형
반응형

이전 문제까지는 명품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();
	}

}
반응형

+ Recent posts