반응형

문제

Scanner로 입력받은 이름과 전화번호를 한 줄에 한 사람씩 c:\temp\phone.txt파일에 저장하라. 
"그만"을 입력하면 프로그램을 종료한다.

 

입력 예시

전화번호 입력 프로그램입니다.
이름 전화번호 >> 황기태 010-5555-7777
이름 전화번호 >> 이재문 011-3333-4444
이름 전화번호 >> 김남윤 065-2222-1111
이름 전화번호 >> 그만

 

소스 코드

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main1 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		FileWriter fout = null;
		System.out.println("전화번호 입력 프로그램입니다.");
		try {
			fout = new FileWriter("파일 위치");
			while(true) {
				System.out.print("이름 전화번호 >> ");
				String line = scanner.nextLine();
				if(line.length()==0 || line.equals("그만"))
					break;
				fout.write(line,0,line.length());
				fout.write("\r\n",0,2);
			}
			fout.close();
		}catch(IOException e) {
			System.out.println("입출력 오류");
		}
		scanner.close();
	}

}

phone.txt 가 아닌 test.txt로 저장하였습니다.

test.txt
0.00MB

반응형
반응형

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

}
반응형
반응형

문제

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

 

소스코드

import java.util.*;
// ArrayList연습
public class Main2 {

	public static void main(String[] args) {
		ArrayList<String> array = new ArrayList<String>();
		
		Scanner scan = new Scanner(System.in);
		System.out.print("5개의 학점을 입력하세요 : ");
		for(int i =0 ;i<5;i++) {
			String temp = scan.next();
			array.add(temp);
		}
		
		for(int i = 0;i<array.size();i++) {
			String temp = array.get(i); // i번째 문자열 얻어오기
			if(temp.equals("A")) {
				System.out.println("학점 ["+temp + "] = 4.0");
			}
			else if(temp.equals("B")){
				System.out.println("학점 [" + temp + "] = 3.0");
			}
			else if(temp.equals("C")){
				System.out.println("학점 [" + temp + "] = 2.0");
			}
			else if(temp.equals("D")){
				System.out.println("학점 [" + temp + "] = 1.0");
			}
			else
				System.out.println("학점 [" + temp + "] = 0");
		}
        scan.close();
	}

}

결과

5개의 학점을 입력하세요 : A B C D F
학점 [A] = 4.0
학점 [B] = 3.0
학점 [C] = 2.0
학점 [D] = 1.0
학점 [F] = 0
반응형
반응형

문제

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