본문 바로가기
Language/Java

[명품 Java Programming]4장 연습문제

by DEV Lee 2020. 7. 11.

이론문제

 

 

 

실습문제

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

public static oid main(String[]args){
	TV myTV=new TV("LG", 2017, 32);//LG에서 만든 2017년 32인치
    myTV.show();
}

>>

class TV{
	String mark;
	int year;
	int inch;
	TV(String mark, int year, int inch){
		this.mark=mark;
		this.year=year;
		this.inch=inch;
	}
	TV(){
		this("",0,0);
	}
	
	void show() {
		System.out.println(this.mark+"에서 만든 "+this.year+"년 "+this.inch+"인치");
	}
}


public class EX01 {
	public static void main(String[] args) {
		TV myTV=new TV("LG",2017,32);
		myTV.show();
	}
}

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

 

>>

import java.util.Scanner;

class Grade{
	int math;
	int science;
	int english;
	Grade(int math, int science, int english){
		this.math=math;
		this.science=science;
		this.english=english;
	}
	Grade(){
		this(0,0,0);
	}
	
	double average() {
		int sum=this.math+this.science+this.english;
		return (double)sum/3;
	}
}

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

 

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

  • 노래의 제목을 나타내는 title
  • 가수를 나타내는 artist
  • 노래가 발표된 연도를 나타내는 year
  • 국적을 나타내는 country

또한 Song 클래스에다음 생성자와 메소드를 작성하라.

  • 생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
  • 노래 정보를 출력하는 show()메소드
  • main()메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을 Song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력

>>

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

 

 

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

 

>>

class Rectangle{
	int x,y,width,height;
	Rectangle(int x,int y,int width,int height){
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
	Rectangle(){
		this(0,0,0,0);
	}
	
	int square() {
		return this.width*this.height;
	}
	boolean contains(Rectangle r) {
		if((r.x>this.x && (r.x+r.width)<(this.x+this.width)) && (r.y>this.y && (r.y+r.height)<(this.y+this.height)))
			return true;
		else
			return false;
	}
	void show() {
		System.out.println("("+this.x+","+this.y+")에서 크기가 "+this.width+"x"+this.height+"인 사각형");
	}
}
public class EX04 {
	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 클래스를 완성하라.

>>

import java.util.Scanner;

class Circle{
	private double x,y;
	private int radius;
	public Circle(double x, double y, int radius) {
		this.x=x;
		this.y=y;
		this.radius=radius;
	}
	public void show() {
		System.out.println("("+this.x+","+this.y+")"+this.radius);
	}
}

public class EX05 {
	public static void main(String[]args) {
		Scanner scan=new Scanner(System.in);
		Circle c[]=new Circle[3];
		for(int i=0;i<c.length;i++) {
			System.out.print("x, y, radius>>");
			double x=scan.nextDouble();
			double y=scan.nextDouble();
			int radius=scan.nextInt();
			c[i]=new Circle(x,y,radius);
		}
		for(int i=0;i<c.length;i++) c[i].show();
		scan.close();
	}
}

 

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

 

>>

 

댓글