본문 바로가기
Programming Study/명품자바프로그래밍

[명품자바프로그래밍] 7장 실습문제(1)

by 푸고배 2018. 9. 26.

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

<소스코드> Chapter7_1.java

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

public class Chapter7_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<Double> v = new Vector<Double>();
		Scanner sc = new Scanner(System.in);
		for(int i=0; i<10; i++){
			v.add(sc.nextDouble());	
		}
		double max = v.get(0);
		for(int i=0; i<v.size(); i++){
			if(v.get(i)>max){
				max=v.get(i);
			}
		}
		System.out.println("가장 큰 수는 "+ max);
		
	}

}

 

2. Scanner 클래스를 사용하여 5개의 학점('A', 'B', 'C', 'D', 'F')을 문자로 입력받아 ArrayList에 저장하고, ArrayList를 검색하여 학점을 점수(A=4.0, B=3.0, C=2.0, D=1.0, F=0으로 변환하여 출력하는 프로그램을 작성하라.

<소스코드> Chapter7_2.java

import java.util.ArrayList;
import java.util.Scanner;

public class Chapter7_2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList<String> arraylist = new ArrayList<String>();
		for(int i=0; i<10; i++){
			arraylist.add(sc.next());
		}
		
		for(int i=0; i<arraylist.size(); i++){
			switch(arraylist.get(i)){
				case "A":
					System.out.println(4.0);
					break;
				case "B":
					System.out.println(3.0);
					break;
				case "C":
					System.out.println(2.0);
					break;
				case "D":
					System.out.println(1.0);
					break;
				case "F":
					System.out.println(0);
					break;
				default:
					System.out.println("잘못된 값 입력");
					break;
			}
		}
	}
}

 

3. 키보드로 10개의 나라 이름과 인구를 입력받아 저장하고, 다시 나라 이름을 키보드로부터 입력받아 인구를 출력하는 프로그램을 다음과 같이 해시맵을 이용하여 작성하라.

HashMap<String, Integer> nations = new HashMap<String, Integer>();

<소스코드> Chapter7_3.java

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

public class Chapter7_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String, Integer> nations = new HashMap<String, Integer>();
		Scanner sc = new Scanner(System.in);
		for(int i=0; i<10; i++){
			nations.put(sc.next(), sc.nextInt());
		}
		System.out.print("나라 이름을 입력해주세요 : ");
		String country = sc.next();
		System.out.println(nations.get(country));
		
	}

}

 

 

4. 다음 프로그램은 ArrayList에 20개의 임의의 실수를 삽입하고 모든 실수를 출력하는 프로그램이다. 모든 실수를 출력하는 부분을 Iterator를 이용하여 수정하라.

import java.util.ArrayList;

public class Example {

public static void main(String[] args) {

ArrayList<Double> a = new ArrayList<Double>();

for(int i=0;i<20;i++){

double d = Math.random()*100; // 0.0 ~ 100.0 사이의 랜덤한 실수

a.add(d);

}

for(int i=0;i<20;i++)

System.out.println(a.get(i));

}

}

<소스코드> Chapter7_4.java

import java.util.ArrayList;
import java.util.Iterator;

public class Chapter7_4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Double> a = new ArrayList<Double>();
		// Iterator<Double> it = a.iterator(); 여기서 지정 시 오류 발생
		for(int i=0;i<20;i++){
			double d = Math.random()*100; //0.0 ~ 100.0 사이의 랜덤한 난수
			a.add(d);
		}
		Iterator<Double> it = a.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
}

 

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

<소스코드> Chapter7_5.java

import java.util.ArrayList;
import java.util.Scanner;

public class Chapter7_5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Student> a = new ArrayList<Student>();
		Scanner sc = new Scanner(System.in);
		for(int i=0; i<3;i++){
			Student st = new Student();
			System.out.print("이름을 입력하시오 : ");
			st.name = sc.next();
			System.out.print("학과를 입력하시오 : ");
			st.department = sc.next();
			System.out.print("학번을 입력하시오 : ");
			st.classnum = sc.nextInt();
			System.out.print("학점평균을 입력하시오 : ");
			st.averscore = sc.nextDouble();
			System.out.println();
			a.add(st);
		}
		for(int i=0;i<a.size();i++){
			System.out.println("이름 : "+a.get(i).name);
			System.out.println("학과 : "+a.get(i).department);
			System.out.println("학번 : "+a.get(i).classnum);
			System.out.println("학점평균 : "+a.get(i).averscore);
		}
	}
}
class Student{
	String name;
	String department;
	int classnum;
	double averscore;
}

 

6. main() 함수를 다음과 같이 수행할 수 있도록 GraphicObject를 상속받는 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() {

........... // 완성하라.

}

pubic static void main(String []arg) {

GraphicEditor g =new GraphicEditor();

g.add(new Rect(2, 2, 3, 4); //(2, 2)에서 3*4짜리 사각형 

g.add(new Line(3, 3, 8, 8); //(3, 3)에서 8*8짜리 사각형 내의 대각선 직선 

g.add(new Line(2, 5, 6, 6); //(2, 5)에서 6*6짜리 사각형 내의 대각선 직선

g.draw();

}

}

 

 

 

<소스코드> Chapter7_6.java

import java.util.Vector;

abstract class GraphicObject{
	int x,y,w,h;
	public 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{
	public Rect(int x, int y, int w, int h) {
		super(x, y, w, h);
	}

	@Override
	public void view() {
		System.out.println(x+","+y+" -> "+w+","+h+"의 사각형");
	}
}

class Line extends GraphicObject{
	public Line(int x, int y, int w, int h) {
		super(x, y, w, h);
	}

	@Override
	public void view() {
		System.out.println(x+","+y+" -> "+w+","+h+"의 선");
	}	
}

public class Chapter7_6 {
	Vector<GraphicObject> v = new Vector<GraphicObject>();
	void add(GraphicObject ob){
		v.add(ob);
	}
	void draw(){
		for(int i=0;i<v.size();i++){
			v.get(i).view();
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Chapter7_6 c = new Chapter7_6();
		c.add(new Rect(2,2,3,4));
		c.add(new Line(3,3,8,8));
		c.add(new Line(2,5,6,6));
		c.draw();
	}

}

 

참고자료 : 생능출판 명품 JAVA Programming

github 주소 : 

https://github.com/ch1517/Masterwork-JAVA-Programming/tree/master/Chapter7/Chapter7

반응형

댓글