본문 바로가기
JAVA

명품 자바 에센셜 4강 실습문제

by IT 정복가 2022. 3. 17.
728x90

1. 아래 실행 결과와 같이 출력하는 다음 main()을 가진 Song 클래스를 작성하라. Song 클래스는 노래 제목 title 필드, 생성자, getTitle() 메소드로 구성된다.

public static void main(String[] args) {
		Song mySong = new Song("Nessun Dorma");
		Song yourSong = new Song("공주는 잠 못 이루고");
		
		System.out.println("내 노래는 " + mySong.getTitle());
		System.out.println("네 노래는 " + yourSong.getTitle());
	}

답:

public class Song {
	String title;
	
	public Song(String title) {
		this.title = title;
	}
	
	public String getTitle() {
		return title;
	}

	public static void main(String[] args) {
		Song mySong = new Song("Nessun Dorma");
		Song yourSong = new Song("공주는 잠 못 이루고");
		
		System.out.println("내 노래는 " + mySong.getTitle());
		System.out.println("네 노래는 " + yourSong.getTitle());
	}
}

 

2. 다음은 이름(name 필드)과 전화번호(tel 필드)를 가진 Phone 클래스이다. 이름과 전화번호를 입력받아 2개의 Phone 객체를 생성하고, 출력하는 main() 메소드를 작성하라.

public class Phone {
	private String name, tel;
	public Phone(String name, String tel) {
		this.name = name;
		this.tel = tel;
	}
	public String getName() { return name; }
	public String getTel() { return tel; }
}

답:

import java.util.Scanner;

public class Phone {
	private String name, tel;
	public Phone(String name, String tel) {
		this.name = name;
		this.tel = tel;
	}
	public String getName() { return name; }
	public String getTel() { return tel; }
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("이름과 전화번호 입력 >>");
		String name1 = sc.next();
		String tel1 = sc.next();
		System.out.print("이름과 전화번호 입력 >>");
		String name2 = sc.next();
		String tel2 = sc.next();
		
		Phone a = new Phone(name1,tel1);
		Phone b = new Phone(name2,tel2);
		
		System.out.println(a.getName()+"의 번호는 "+a.getTel());
		System.out.println(b.getName()+"의 번호는 "+b.getTel());
		
		sc.close();
	}
}

 

3. 사각형을 표현하는 다음 Rect 클래스를 활용하여, Rect 객체 배열을 생성하고, 사용자로부터 4개의 사각형을 입력받아 배열에 저장한 뒤, 배열을 검색하여 사각형 면적의 합을 출력하는 main() 메소드를 가진 RectArray 클래스를 작성하라.

class Rect {
	private int width, height;
	public Rect(int width, int height) {
		this.width = width;
		this.height = height;
	}
	public int getArea() { return width*height; }
}

답:

import java.util.Scanner;

class Rect {
	private int width, height;
	public Rect(int width, int height) {
		this.width = width;
		this.height = height;
	}
	public int getArea() { return width*height; }
}

public class RectArray {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int sum = 0;
		Rect arr[] = new Rect[4];
		
		for(int i = 0; i < arr.length; i++) {
			System.out.print((i+1)+" 너비와 높이 >>");
			int width = sc.nextInt();
			int height = sc.nextInt();
			
			arr[i] = new Rect(width, height);
			sum += arr[i].getArea();
		}	
		System.out.println("저장하였습니다...");
		System.out.println("사각형의 전체 합은 "+sum);
		
		sc.close();
	}
}

 

4. 이름(name)과 전화번호(tel) 필드, 생성자 및 필요한 메소드를 가진 Phone 클래스를 작성하고, 다음 실행 사례와 같이 작동하도록 main()을 가진 PhoneManager 클래스를 작성하라. 한 사람의 전화번호는 하나의 Phone 객체로 다룬다.

답:

import java.util.Scanner;

class Phone{
	public String name;
	public String tel;
	
	public Phone(String name, String tel){
		this.name = name;
		this.tel = tel;
	}
	
	public String getName() {
		return name;
	}
	public String getTel() {
		return tel;
	}
}

public class PhoneManager {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("인원수>>");
		int a = sc.nextInt();
		Phone phone[] = new Phone[a];
		
		for(int i = 0; i < a; i++) {
			System.out.print("이름과 전화번호(번호는 연속적으로 입력)>>");
			String name = sc.next();
			String tel = sc.next();
			
			phone[i] = new Phone(name,tel);
		}
		System.out.println("저장되었습니다...");
		
		while(true) {
			System.out.print("검색할 이름>>");
			String name = sc.next();
			String tel = "";
			
			if(name.equals("exit")) {
				System.out.println("프로그램을 종료합니다."); break;
			}
			for(int i = 0; i < a; i++) {
				if(name.equals(phone[i].getName())) {
					tel = phone[i].getTel();
				} 
			}  
			if(tel == "")
				System.out.println(name+" 이 없습니다.");
			else 
				System.out.println(name+ "의 번호는 "+tel);
		}
		sc.close();
	}

}

 

5. CircleManager는 static 메소드를 가진 클래스이다. StaticTest 클래스는 static 메소드를 활용하는 사례를 보여준다. 실행 결과를 참고하여 코드를 완성하라.

class Circle {
	private int radius;
	public Circle(int radius) { this.radius = radius; }
	public int getRadius() { return this.radius; }
	public void setRadius(int radius) { this.radius = radius; }
}

class CircleManager {
	static void copy(Circle src, Circle dest) {
		dest.setRadius(src.getRadius());
	}
	static boolean equals(Circle a, Circle b) {
		if(a.getRadius() == b.getRadius())
			return true;
		else
			return false;
	}
}
public class StaticTest {
	public static void main(String[] args) {
		Circle pizza = new Circle(5);
		Circle waffle = new Circle(1);
		
		boolean res = CircleManager.equals(pizza, waffle);
		if(res = true)
			System.out.println("pizza와 waffle 크기 같음");
		else
			System.out.println("pizza와 waffle 크기 다름");
		
		CircleManager.copy(pizza, waffle);
		res = CircleManager.equals(pizza, waffle);
		if(res = true)
			System.out.println("pizza와 waffle 크기 같음");
		else
			System.out.println("pizza와 waffle 크기 다름");
	}
}

 

6. 다음은 가로 세로로 구성되는 박스를 표현하는 Box 클래스와 이를 이용하는 코드이다. Box의 draw()는 fill 필드에 지정된 문자로 자신을 그린다. 실행 결과를 보면서, 코드를 완성하라.

답:

public class Box {
	private int width, height;
	private char fillChar;
	public Box() {
		this(10, 1);
	}
	public Box(int width, int height) {
		this.width = width;
		this.height = height;
	}
	public void draw() {
		for(int i=0; i<height; i++) {
			for(int j=0; j<width; j++)
				System.out.print(fillChar);
			System.out.println();
		}
	}
	public void fill(char c) {
		this.fillChar = c;
	}

	public static void main(String[] args) {
		Box a = new Box();
		Box b = new Box(20, 3);
		a.fill('*');
		b.fill('%');
		a.draw();
		b.draw();
	}
}

 

728x90