<명품 JAVA Programming> - Chapter4 연습문제(7, 8, 9, 10, 11, 12)

7. 하루의 할 일을 표현하는 클래스 Day는 다음과 같다. 한 달의 할 일을 표현하는 MonthSchedule 클래스를 작성하라.

 

package chapter4;
import java.util.Scanner;

class Day{
	private String work;	//하루의 할 일을 나타내는 문자열
	public void set(String work) {this.work = work;}
	public String et() {return work;}
	public void show() {
		if(work == null) System.out.println("없습니다.");
		else System.out.println(work + "입니다.");
	}
}

public class MonthSchedule {
	private Scanner scanner;
	private int size;
	private Day[] d;

	public MonthSchedule(int size) {
		this.size = size;
		this.d = new Day[size];
		for(int i = 0; i < d.length; i++)
			d[i] = new Day();
		scanner = new Scanner(System.in);
		System.out.println("이번달 스케쥴 관리 프로그램.");
	}

	public void input() {
		System.out.print("날짜(1~30)? ");
		int date = scanner.nextInt();
		System.out.print("할일(빈칸없이입력)? ");
		String work = scanner.next();
		
		d[date].set(work);
	}

	public void view() {
		System.out.print("날짜(1~30)? ");
		int date = scanner.nextInt();
		System.out.print(date + "일의 할 일은 " );
		this.d[date].show();
	}

	public void finish() {
		System.out.println("프로그램을 종료합니다.");
	}

	public void run() {
		while(true) {
			System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
			int menu = scanner.nextInt();
			switch(menu) {
			case 1:
				input();
				break;
			case 2:
				view();
				break;
			case 3:
				finish();
				scanner.close();
				System.exit(0);
				break;
			default:
				System.out.println("잘못 입력하셨습니다.");
				break;	
			}
		}
	}
	public static void main(String[] args) {

		MonthSchedule april = new MonthSchedule(30);	//4월달 할 일
		april.run();
	}
}

 


 

8. 이름(name), 전화번호(tel) 필드와 생성자 등을 가진 Phone 클래스를 작성하고, 실행 예시와 같이 작동하는 PhoneBook 클래스를 작성하라.

 

package chapter4;
import java.util.Scanner;

class Phone {
	private String name, tel;
	void set(String name, String tel) {
		this.name = name; this.tel = tel;
	}
	boolean find(String name) {
		if(this.name.equals(name)) return true;
		return false;
	}
	void showTel() {
		System.out.println(this.name + "의 번호는 " + this.tel + "입니다.");
	}
}

public class PhoneBook {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		int people_number;
		String name, tel;
		System.out.print("인원수>>");
		people_number = scanner.nextInt();
		Phone[] p = new Phone[people_number];

		for(int i = 0; i < people_number; i++)
			p[i] = new Phone();

		for(int i = 0; i < people_number; i++) {
			System.out.print("이름과 전화번호(이름과 번호는 빈 칸 없이 입력>>");
			name = scanner.next();
			tel = scanner.next();
			p[i].set(name, tel);
		}
		System.out.println("저장되었습니다...");

		while(true) {
			boolean f = false;
			System.out.print("검색할 이름>>");
			name = scanner.next();
			if(name.equals("그만")) break;
			for(int i = 0; i < p.length; i++) {
				if(p[i].find(name)) {
					p[i].showTel();
					f = true;
					break;
				}
			}
			if(f == false)
				System.out.println(name + "이 없습니다.");
		}
		scanner.close();
	}
}

 


 

9. 다음 2개의 static을 가진 ArrayUtil 클래스를 만들어보자. 다음 코드의 실행 결과를 참고하여 concat()와 print()를 작성하여 ArryayUtil 클래스를 완성하라.

 

package chapter4;

class ArrayUtil {
	public static int[] concat(int[] a, int[] b) {
		//배열 a와 b를 연결한 새로운 배열 리턴
		int size = a.length + b.length;
		int array[] = new int[size];
		
		for(int i = 0; i < a.length; i++)
			array[i] = a[i];
		for(int i = a.length; i < size; i++)
			array[i] = b[i - a.length];
		
		return array;
	}
	public static void print(int[] a ) {
		//배열 a 출력
		System.out.print("[");
		for(int i = 0; i < a.length; i++)
			System.out.print(" " + a[i]);
		System.out.print("]");
	}
}
public class StaticEx {
	public static void main(String[] args) {
		int[] array1 = {1, 5, 7, 9};
		int[] array2 = {3, 6, -1, 100, 77};
		int[] array3 = ArrayUtil.concat(array1, array2);
		ArrayUtil.print(array3);
	}
}

 


 

10. 다음과 같은 Dictionary 클래스가 있다. 실행 결과와 같이 작동하도록 Dictionary 클래스의 kor2Eng() 메소드와 DicApp 클래스를 작성하라.

 

package chapter4;
import java.util.Scanner;
class dictionary {
	private static String [] kor = {"사랑", "아기", "돈", "미래", "희망"};
	private static String [] eng = {"love", "baby", "money", "future", "hope"};
	public static String kor2Eng(String word) {
		String a = "저의 사전에 없습니다.";
		for(int i = 0; i < 5; i++) {
			if (kor[i].equals(word)) 
				return eng[i];
		}
		return a;
	}
}
public class DicApp {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String word, engword;
		
		System.out.println("한영 단어 검색 프로그램입니다.");
		do {
			System.out.print("한글 단어?");
			word = scanner.next();
			engword = dictionary.kor2Eng(word);
			
			System.out.println(word + "은 " + engword);
		} while(!word.equals("그만"));
		
		scanner.close();
	}
}

 


 

11. 다수의 클래스를 만들고 활용하는 연습을 해보자. 더하기, 빼기, 곱하기, 나누기를 수행하는 각 클래스 Add, Sub, Mul, Div를 만들어라.

 

package chapter4;
import java.util.Scanner;

class Add {
	private int a, b;
	void setValue(int a, int b) {
		this.a = a; this.b = b;
	}
	int calculate() {return a + b;}
}

class Sub {
	private int a, b;
	void setValue(int a, int b) {
		this.a = a; this.b = b;
	}
	int calculate() {return a - b;}
}

class Mul {
	private int a, b;
	void setValue(int a, int b) {
		this.a = a; this.b = b;
	}
	int calculate() {return a * b;}
}

class Div {
	private int a, b;
	void setValue(int a, int b) {
		this.a = a; this.b = b;
	}
	int calculate() {return a / b;}
}

public class Calculation {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int a, b, res = 0;
		String op;
				
		System.out.print("두 정수와 연산자를 입력하시오.");
		a = scanner.nextInt();
		b = scanner.nextInt();
		op = scanner.next();
		
		if(op.charAt(0) == '+') {
			Add add = new Add();
			add.setValue(a, b);
			res = add.calculate();
		}
		else if(op.charAt(0) == '-') {
			Sub sub = new Sub();
			sub.setValue(a, b);
			res = sub.calculate();
		}
		else if(op.charAt(0) == '*') {
			Mul mul = new Mul();
			mul.setValue(a, b);
			res = mul.calculate();
		}
		else if(op.charAt(0) == '/') {
			Div div = new Div();
			div.setValue(a, b);
			res = div.calculate();
		}
		else 
			System.out.println("잘못 입력하셨습니다.");
		
		System.out.println(res);
		
		scanner.close();
	}
}

 


 

12. 간단한 콘서트 예약 시스템을 만들어보자.

 

package chapter4;
import java.util.Scanner;

class Seat {	//좌석 객체
	private int number; private String name;
	
	public void set(int number, String name) {	//좌석 지정
		this.number = number; this.name = name;
	}
	public void show() {	//이름을 보여주는 메소드
		System.out.print(name  + "  ");
	}
	public boolean check_seat() {	//좌석이 찼는지 확인
		if (this.number != 0)
			return true;
		return false;
	}
}

public class Reservation {
	private Scanner scanner;
	private Seat[][] seat = new Seat[3][10]; //좌석 객체 배열 생성

	public Reservation() {	//생성자
		scanner = new Scanner(System.in);
		for(int i = 0; i < 3; i++) {	//각 좌석 객체 생성
			for(int j = 0; j < 10; j++) {
				seat[i][j] = new Seat(); 
				seat[i][j].set(0, ""); //각 좌석 객체 초기화
			}
		}
	}
	
	public void view_menu() {
		System.out.print("좌석구분 S(1), A(2), B(3)>>");
	}
	
	public void view_seat(int l) {	//한 줄의 좌석을 보여주는 메소드
		if(l == 1) System.out.print("S>>");
		else if (l==2) System.out.print("A>>");
		else System.out.print("B>>");
		for(int i = 0; i < 10; i++) {	//지정되었으면 이름을 보이고
			if(seat[l-1][i].check_seat())
				seat[l-1][i].show();
			else
				System.out.print("___  ");	//아니면 밑줄 출력
		}
		System.out.println();
	}
	public void reserve() {	//예약 메소드
		int line; int number; String name;
		view_menu();
		line = scanner.nextInt();
		view_seat(line);
		System.out.println();
		System.out.print("이름>>");
		name = scanner.next();
		System.out.print("번호>>");
		number = scanner.nextInt();
		seat[line-1][number-1].set(number, name);
	}
	
	public void check() {	//조회 메소드
		for(int i = 0; i < 3; i++)
			view_seat(i+1);
		System.out.println("조회를 완료했습니다.");
		System.out.println();
	}
	
	public void cancel() {	//취소 메소드
		int line; int number;
		view_menu();
		line = scanner.nextInt();
		view_seat(line);
		System.out.print("번호>>");
		number = scanner.nextInt();
		seat[line-1][number-1].set(0, "");		
		System.out.println("취소가 완료되었습니다.");
	}
	
	public void finish() {	//종료 메소드
		System.out.println("프로그램을 종료합니다.");
	}
	
	public void run() {	//실행 메소드
		while(true) {
			System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
			int menu = scanner.nextInt();
			switch(menu) {
			case 1:
				reserve();
				break;
			case 2:
				check();
				break;
			case 3:
				cancel();
				break;
			case 4:
				finish();
				scanner.close();
				System.exit(0);
				break;
			default:
				System.out.println("잘못 입력하셨습니다.");
				break;	
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println("명품콘서트홀 예약 시스템입니다.");
		Reservation r = new Reservation();
		r.run();	
	}
}