반응형

문제

아래의 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의 선
반응형
반응형

문제

하나의 학생 정보는 Student 클래스로 표현한다. Student 클래스에는 이름, 학과, 학번, 학점 평균을 나타내는 필드가 있다. 키보드로 학생 정보를 5개 입력받아 ArrayList<Student>에 저장한 후에 ArrayList<Student>의 모든 학생 정보를 출력하는 프로그램을 작성하라.

 

소스 코드

import java.util.*;
class Student{
	private String name; // 이름
	private String Department; // 학과
	private int StudentID; // 학번
	private double Score; // 학점
	Student(String name, String Department,int StudentID, double Score){
		this.name = name;
		this.Department = Department;
		this.StudentID = StudentID;
		this.Score = Score;
	}
	public void Print(){
		System.out.println("학생 이름 : "+name);
		System.out.println("학과 이름 : "+Department);
		System.out.println("학번 : "+StudentID);
		System.out.println("학점 : "+Score);
	}
	
}
public class Main5 {
	public static void main(String[] args)
	{
		ArrayList<Student> student = new ArrayList<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("학번 : ");
			int studentID = s.nextInt();
			System.out.print("학점 : ");
			double score = s.nextDouble();
			s.nextLine();
			Student temp = new Student(name,department,studentID,score);
			student.add(temp);
		}
		Iterator<Student> it = student.iterator();
		int i = 1;
		while(it.hasNext()) {
        	System.out.println();
			System.out.println(i+"번 학생");
			it.next().Print();
			i++;
		}
        s.close();
	}
}

결과

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

1번 학생
학생 이름 : 유관순
학과 이름 : 독립
학번 : 19021216
학점 : 4.5

2번 학생
학생 이름 : 이순신
학과 이름 : 무예
학번 : 15450428
학점 : 4.5

3번 학생
학생 이름 : 세종대왕
학과 이름 : 훈민정음
학번 : 13970515
학점 : 4.5

4번 학생
학생 이름 : 백범 김구
학과 이름 : 독립
학번 : 18760829
학점 : 4.5

5번 학생
학생 이름 : 안중근
학과 이름 : 독립
학번 : 18790902
학점 : 4.5
반응형

+ Recent posts