코딩 연습장/JAVA

자바 4장 연습문제(클래스와 객체)-1

Do아 2021. 3. 19. 19:44
728x90

2021/03/14(일)

문제) -- 내 풀이는 밑에

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

public class PracticeProblem01 {
	public static void main(String[] args) {
		Tv myTv = new Tv("LG",2017,32);
		myTv.show();
	}
}

LG에서 만든 2017년형 32인치 TV

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

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개의 점수 입력>> 90 88 96

평균은 91

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

- 노래의 제목을 나타내는 title

- 가수를 나타내는 artist

- 노래가 발표된 연도를 나타내는 year

- 국적을 나타내는 country

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

- 생성자 2개 : 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자

- 노래 정보를 출력하는 show() 메소드

- main() 메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을 Song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.

1978년 스웨덴국적의 ABBA가 부른 Dancing Queen

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

- int 타입의 x, y, width, height 필드 : 사각형을 구성하는 점과 크기 정보

- x, y, width, height 값을 매개변수로 받아 필드를 초기화하는 생성자

- int square() : 사각형 넓이 리턴

- void show() : 사각형의 좌표와 넓이를 화면에 출력

- boolean contains(Rectangle r) : 매개변수로 받은 r이 현 사각형 안에 있으면 true 리턴

- main() 메소드의 코드와 실행 결과는 다음과 같다

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을 포함합니다.");
	}

(2, 2)에서 크기가 8x7인 사각형

s의 면적은 36

t는 r을 포함합니다.

내 풀이)

4-1)

class Tv{
	private String company;
	private int year;
	private int inch;
	
	public Tv(String company, int year, int inch) {
		this.company = company;
		this.year = year;
		this.inch = inch;
	}
	
	public void show() {
		System.out.println(company+"에서 만든 "+year+"년형 "+ inch+"인치 TV");
	}
}
public class PracticeProblem01 {
	public static void main(String[] args) {
		Tv myTv = new Tv("LG",2017,32);
		myTv.show();
	}
}

4-2)

import java.util.Scanner;

class Grade{
	private int math;
	private int science;
	private int english;
	
	public Grade(int math, int science, int english) {
		this.math = math;
		this.science = science;
		this.english = english;
	}
	public int average() {
		return (math+science+english)/3;
	}
	
}
public class PracticeProblem02 {
	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();
	}
}

4-3)

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

4-4)

class Rectangle {
	private int x;
	private int y;
	private int width;
	private int 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 width*height;
	}
	public void show() {
		System.out.println("("+x+", "+y+")에서 크기가 "+width+"x"+height+"인 사각형");
	}
	public boolean contains(Rectangle r) {
		if(x<r.x && x+width>r.x+r.width && y<r.y && y+height > r.y+r.height)
			return true;
		else
			return false;
	}
}
public class PracticeProblem04 {
	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을 포함합니다.");
	}
}

사각형 안에 완전히 포함되는 사각형만 검출

728x90