반응형

문제

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

**** 수도 맞추기 게임을 시작합니다 ****
입력: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();
	}

}

 

반응형
반응형

문제

아래의 HV 클래스는 해쉬맵을 인자로 받아 벡터를 리턴하는 hashToVector() 메소드를 가지고 있다. 이 메소드는 해쉬맵 내의 '값(value)' 을 모두 Vector<String>에 삽입하여 리턴한다. hashToVector()를 작성하라.

 

문제 소스 코드

import java.util.*;

public class HV {
	public static Vector<String> hashToVector(HashMap<String, String> h){
    
    }
	
	public static void main(String[] args)
	{
		HashMap<String,String> h = new HashMap<String,String>();
		h.put("범죄", "112");
		h.put("화재", "119");
		h.put("전화번호", "114");
		Vector<String> v = HV.hashToVector(h);
		for(int n = 0;n<v.size();n++) {
			System.out.println(v.get(n));
		}
	}
}

 

소스 코드

import java.util.*;

class HV{
	public static Vector<String> hashToVector(HashMap<String,String> h){
		Vector<String> temp = new Vector<String>();
		Set<String> keys = h.keySet();
		for(String s : keys) {
			temp.add(h.get(s));
		}
		return temp;
	}
}
public class Main7 {
	
	public static void main(String[] args)
	{
		HashMap<String,String> h = new HashMap<String,String>();
		h.put("범죄", "112");
		h.put("화재", "119");
		h.put("전화번호", "114");
		Vector<String> v = HV.hashToVector(h);
		for(int n = 0;n<v.size();n++) {
			System.out.println(v.get(n));
		}
	}
}

 

결과

119
112
114
반응형
반응형

문제

main() 함수를 다음과 같이 수행할 수 있도록 GrapgicObject를 상속받는 Rect와 Line을 작성하고, GraphicEditor 클래스에 필요한 메소드 add()와 draw()를 작성하여 완성하라

문제 소스 코드

abstract class GraphicObject{
	int x,y,w,h;
	GraphicObject(int x,int y,int w,int h){
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}
	public abstract void view();
}

public class GraphicEditor {
	Vector<GraphicObject> v= new Vector<GraphicObject>();
	
	void add(GraphicObject ob) {
		
	}
	void draw() {
		
	}
	
	public static void main(String[] args) {
		GraphicEditor g = new GraphicEditor();
		g.add(new Rect(2,2,3,4)); // (2,2)에서 3x4짜리 사각형
		g.add(new Line(3,3,8,8)); // (3,3)에서 8x8의 사각형 내의 대각선 직선
		g.add(new Line(2,5,6,6)); // (2,5)에서 6x6의 사각형 내의 대각선 직선
        	g.draw();
	}

}

결과 예시

2, 2 -> 3, 4의 사각형
3, 3 -> 8, 8의 선
2, 5 -> 6, 6의 선

소스코드

import java.util.Iterator;
import java.util.Vector;

abstract class GraphicObject{
	int x,y,w,h;
	GraphicObject(int x,int y,int w,int h){
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}
	public abstract void view();
}
class Rect extends GraphicObject{
	Rect(int x,int y,int w,int h){
		super(x,y,w,h);
	}
	
	public void view() {
		System.out.println(x + ", " + y +" -> " + w + ", " + h + "의 사각형");
	}
}

class Line extends GraphicObject{
	Line(int x,int y,int w,int h){
		super(x,y,w,h);
	}
	
	public void view() {
		System.out.println(x + ", " + y +" -> " + w + ", " + h + "의 선");
	}
}
public class GraphicEditor {
	Vector<GraphicObject> v= new Vector<GraphicObject>();
	
	void add(GraphicObject ob) {
		v.add(ob);
	}
	void draw() {
		Iterator<GraphicObject> it = v.iterator();
		while(it.hasNext()) {
			it.next().view();
		}
	}
	
	public static void main(String[] args) {
		GraphicEditor g = new GraphicEditor();
		g.add(new Rect(2,2,3,4)); // (2,2)에서 3x4짜리 사각형
		g.add(new Line(3,3,8,8)); // (3,3)에서 8x8의 사각형 내의 대각선 직선
		g.add(new Line(2,5,6,6)); // (2,5)에서 6x6의 사각형 내의 대각선 직선
		g.draw();
	}

}

결과

2, 2 -> 3, 4의 사각형
3, 3 -> 8, 8의 선
2, 5 -> 6, 6의 선
반응형
반응형

문제

Scanner 클래스를 사용하여 10개의 실수 값을 키보드로부터 읽어 벡터에 저장한 후, 벡터를 검색하여 가장 큰 수를 출력하는 프로그램을 작성하라.

 

소스코드

import java.util.*;
// Vector연습
public class Main1 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		Vector<Double> num = new Vector<Double>();
		System.out.println("10개의 실수를 입력하시오");
		for (int i = 0; i < 10; i++) {
			num.add(scan.nextDouble());
		}
		
		double max = 0.0;
		double temp = 0.0;
		Iterator<Double>it = num.iterator();
		while (it.hasNext()) {
			temp = it.next();
			max = (max > temp) ? max : temp;
		}
		System.out.println("10개의 실수 중 최대값은 : "+ max);
        scan.close();
	}

}

결과

반응형

+ Recent posts