<명품 JAVA Programming> - Chapter4 연습문제(1, 2, 3, 4, 5, 6)

1. 자바 클래스를 작성하는 연습을 해보자. 다음 main()메소드를 실행하였을 때 예시와 같이 출력되도록 TV 클래스를 작성하라.

 

package chapter4;

//퍼블릭 클래스명은 TV 대신 Chapter4_1_TV 
public class TV {
		private String name;
		private int year, size;
		
		public TV(String name, int year, int size) {
			this.name = name;
			this.year = year;
			this.size = size;
		}
		public void show() {
			System.out.println(this.name + "에서 만든 " + this.year + "년형 " + this.size + "인치 TV");
		}
		
	public static void main(String[] args) {
		TV myTv = new TV("LG", 2017, 32);
		myTv.show();
	}
}

 


 

2. Grade 클래스를 작성해보자. 3 과목의 점수를 입력받아 Grade 객체를 생성하고 성적 평균을 출력하는 main()과 실행 예시는 다음과 같다.

 

package chapter4;
import java.util.Scanner;

public class Grade {
	private int math, science, english;
	
	public Grade(int math, int science, int english) {
		this.math = math;
		this.science = science;
		this.english = english;
	}
	
	public int average() {
		return (this.math + this.science + this.english) / 3;
	}
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
		int math = scanner.nextInt();
		int science = scanner.nextInt();
		int english = scanner.nextInt();
		Grade me = new Grade(math, science, english);
		System.out.println("평균은 " + me.average());
		
		scanner.close();
	}
}

 


 

3. 노래 한 곡을 나타내는 Song 클래스를 작성하라. Song은 다음 필드로 구성된다.

 

package chapter4;

public class Song {
	private String title, artist, country;
	private int year;
	
	public Song() {};
	public Song(String title, String artist, String country, int year ) {
		this.title = title;
		this.artist = artist;
		this.country = country;
		this.year = year;
	}
	
	public void show() {
		System.out.print(year + "년 ");
		System.out.print(country + "국적의 ");
		System.out.print(artist + "가 부른 ");
		System.out.println(title);
	}
	
	public static void main(String[] args) {
		Song song = new Song("Dancing Queen", "ABBA", "스웨덴", 1978);
		song.show();
	}
}

 


 

4.다음 멤버를 가지고 직사각형을 표현하는 Rectangle 클래스를 작성하라.

 

package chapter4;

public class Rectangle {
	private int x, y, width, height;
	
	public Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public int square() {
		return this.width * this.height;
	}
	
	public void show() {
		System.out.print("(" + this.x + "," + this.y + ") ");
		System.out.println("에서 크기가 " + width + "x" + height + "인 사각형");
	}
	
	public boolean contains(Rectangle r) {
		if(this.x < r.x && this.y < r.x)
			if((this.x + this.width > r.x + r.width) && (this.y + this.height > r.y + r.height))
				return true;
		return false;
	}
	public static void main(String[] args) {
		Rectangle r = new Rectangle(2, 2, 8, 7);
		Rectangle s = new Rectangle(5, 5, 6, 6);
		Rectangle t = new Rectangle(1, 1, 10, 10);
		
		r.show();
		System.out.println("s의 면적은 " + s.square());
		if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
		if(t.contains(s)) System.out.println("t는 s을 포함합니다.");		
	}
}

 


 

5. 다음 설명대로 Circle 클래스와 CircleManager 클래스를 완성하라.

 

package chapter4;

import java.util.Scanner;

class Circle {
	private double x, y;
	private int radius;
	public Circle(double x, double y, int radius) {
		//x, y, radius 초기화
		this.x = x; 
		this.y = y; 
		this.radius = radius;
	}
	public void show() {
		System.out.println("(" + x + "," + y + ") " + radius);
	}
}
public class CircleManager {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Circle c[] = new Circle[3];	//3개의 Circle 배열 선언
		for(int i = 0; i < c.length; i++) {
			System.out.print("x, y, radius >>");
			double x = scanner.nextDouble();	//x값 읽기
			double y = scanner.nextDouble();	//y값 익기
			int r = scanner.nextInt();	//반지름 읽기
			c[i] = new Circle(x, y, r);	//Circle 객체 생성
		}
		for(int i = 0; i < c.length; i++) c[i].show();	//모든 Circle 객체 출력
		scanner.close();
	}
}

 


 

6. 앞의 5번 문제는 정답이 공개되어 있다. 이 정답을 참고하여 Circle 클래스와 Circlemanager 클래스를 수정하여 다음 실행 결과처럼 되게 하라.

 

package chapter4;

import java.util.Scanner;

class Circle2 {
	private double x, y;
	private int radius;
	public Circle2(double x, double y, int radius) {
		//x, y, radius 초기화
		this.x = x; 
		this.y = y; 
		this.radius = radius;
	}
	public void show() {
		System.out.println("가장 면적이 큰 원은 (" + x + "," + y + ") " + radius);
	}
	public int square() {	//면적을 구하는 메소드
		return this.radius * this.radius;
	}
}
public class CircleManager2 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int max, j = 0;
		Circle2 c[] = new Circle2[3];	//3개의 Circle 배열 선언
		for(int i = 0; i < c.length; i++) {
			System.out.print("x, y, radius >>");
			double x = scanner.nextDouble();	//x값 읽기
			double y = scanner.nextDouble();	//y값 익기
			int r = scanner.nextInt();	//반지름 읽기
			c[i] = new Circle2(x, y, r);	//Circle 객체 생성
		}
		max = c[0].square();	//첫 번째 객체를 최대로 가정
		for(int i = 1; i < c.length; i++) {
			if (c[i].square() > max)
				j = i;
		}
		c[j].show();	//면적이 가장 큰 Circle 객체 출력
		scanner.close();
	}
}