85.
package day07;
/*
* main() 메서드를 실행하였을때 예시와 같이 출력 되도록 Tv 클래스를 작성하세요.
* 콘솔 출력예) LG에서 만든 2022년형 45인치 TV
*/
class Tv{
String made;
int year;
int inch;
Tv(String made, int year, int inch){
this.made = made;
this.year = year;
this.inch =inch;
}
void show() {
System.out.println(made +"에서 만든 " + year+"년형 " + inch +"인치 TV");
}
}
public class Test85 {
public static void main(String[] args) {
Tv tv = new Tv("LG", 2022,45);
tv.show();
}
}
생성자를 이용하여 푸는 문제.
클래스의 인스턴스 변수들은 접근 지정자를 지정하지 않고 디폴트로 선언했다.
Tv 생성자에 매개변수 세 개를 받아 각각 made, year, inch에 대입하고, show 메서드에서 각 변수를 이용하여 출력해준다.
86.
package day07;
import java.util.Scanner;
/*
Grade 클래스를 작성해보세요.
3과목의 점수를 입력받아 Grade 객체를 생성하고,
성적 평균을 출력하는 main()메서드와 실행예시는 아래와 같습니다.
콘솔 출력예 >>
수학, 과학, 영어 점수를 입력하세요.
수학 >> 90 (<-사용자가 입력)
과학 >> 88
영어 >> 96
평균은 91
*/
class Grade{
int math;
int sci;
int eng;
Grade(int math, int sci, int eng){
this.math = math;
this.sci = sci;
this.eng = eng;
}
double getAvg() {
return ((math+sci+eng)/3.0);
}
}
public class Test86 {
public static void main(String[] args) {
// main 작성 완료 상태
Scanner sc = new Scanner(System.in);
System.out.println("수학, 과학, 영어 점수를 입력하세요.");
System.out.println("수학 >> ");
int math = Integer.parseInt(sc.nextLine());
System.out.println("과학 >> ");
int sci = Integer.parseInt(sc.nextLine());
System.out.println("영어 >> ");
int eng = Integer.parseInt(sc.nextLine());
Grade me = new Grade(math, sci, eng);
System.out.println("평균은 " + me.getAvg());
sc.close();
}
}
Grade 클래스:
각각 수학, 과학, 영어의 점수를 담는 인스턴스 변수들을 먼저 만들어준다.
Grade 변수의 초기화를 위해 매개변수를 받아 실행되는 생성자를 선언한 뒤 평균을 내는 getAvg 메서드를 만든다.
평균은 실수로 나오는 게 더 정확하니 double로 타입을 지정하고 (수학+과학+영어)/ 3.0 한 값을 리턴해준다.
89.
package day07;
import java.util.Scanner;
/*
노래 한곡을 나타내는 Song 클래스를 작성하세요. Song은 다음 필드(변수)로 구성됩니다.
- 노래 제목을 나타내는 title
- 가수를 나타내는 artist
- 노래가 발표된 연도를 나타내는 year
- 국적을 나타내는 country
또한, Song클래스에 다음 생성자와 메서드를 작성하세요.
- 생성자 총 2개 : 기본생성자와 매개변수로 모든 필드를 초기화하는 생성자
- 노래 정보를 출력하는 show() 메서드
그리고 main 메서드에서는 2021년, 한국국적의 이무진이 부른 신호등 을 Song 객체로 생성,
show() 메서드를 이용하여 정보를 아래와 같이 출력하세요.
콘솔 출력예 >> 2021년 한국국적의 이무진이 부른 신호등
*/
class Song {
private String title;
private String artist;
private String country;
private int year;
public String getTitle(){
return title;
}
public void setTitle(String title) {
this.title=title;
}
public String getArtist(){
return artist;
}
public void setArtist(String artist) {
this.artist=artist;
}
public String getCountry(){
return country;
}
public void setCountry(String country) {
this.country=country;
}
public int getYear(){
return year;
}
public void setYear(int year) {
this.year=year;
}
Song(){
this("무제", "미상", "미상", 0);
}
Song(String title, String artist, String country, int year){
setTitle(title);
setArtist(artist);
setCountry(country);
setYear(year);
}
void show() {
System.out.println( year + "년에 "+ country+
" 국적의 " + artist+"이/가 부른 " + title);
}
}
public class Test89 {
public static void main(String[] args) {
Song song = new Song("GEE","소녀시대","한국",2009);
song.show();
}
}
이번 문제는 접근 지정자를 활용하여 풀어보았다.
먼저 Song 클래스 내부에 필요한 변수들을 private 접근 지정자로 만들어준 뒤에 getter setter까지 세팅을 해준다.
기본 생성자의 경우엔 무제, 미상, 미상, 0이 출력되도록 선언을 하고
매개변수를 받는 생성자는 각각의 값이 setter를 통해 대입되도록 작성했다..
마지막 show 메서드는 들어간 값을 출력한다.
90.
package day07;
/*
직사각형을 표현하는 Rectangle 클래스를 작성하세요.
- int 타입의 x, y, width, height 필드 : 사각형을 구성하는 점과 크기 정보
* x,y는 사각형의 왼쪽 위의 점을 말함. (0,0)은 왼쪽 위, x값은 오른쪽으로 증가, y값은 아래로 증가
- x, y, width, height 값을 매개변수로 받아 필드를 초기화하는 생성자
- int squareArea() 메서드 : 사각형의 너비 리턴
- void show() 메서드 : 사각형의 좌표와 너비 출력
- boolean contains(Rectangle r) : 매개변수로 받은 r이 현 사각형 안에 있으면 true리턴
콘솔 출력예 >>
(2,2)에서 크기가 8x7인 사각형
b의 면적은 36
c는 a를 포함합니다.
*/
class Rectangle{
private int x;
private int y;
private int width;
private int height;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
Rectangle(int x, int y, int width, int height){
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
int squareArea() {
return width*height;
}
void show() {
System.out.println("(" +x +","+ y +")"+ "에서 크기가 " +
width +"x"+ height + "인 사각형");
}
/* 내가 짠 코드.. 틀림.
boolean contains(Rectangle r) {
if (r.x >= this.x && r.x <= this.y && r.x >= this.y && r.y <= this.y) {
}
return true;
}*/
boolean contains(Rectangle r) {
if(x < r.x && y < r.y && (x + width) > (r.x + r.width) &&
(y+height) > (r.y + r.height)) {
return true;
}
return false;
}
}
public class Test90 {
public static void main(String[] args) {
Rectangle a = new Rectangle(2, 2, 8, 7);
Rectangle b = new Rectangle(5, 5, 6, 6);
Rectangle c = new Rectangle(1, 1, 10, 10);
a.show();
b.show();
c.show();
System.out.println("a의 면적은 " + a.squareArea());
System.out.println("b의 면적은 " + b.squareArea());
System.out.println("c의 면적은 " + c.squareArea());
if(c.contains(a)) System.out.println("c는 a를 포함합니다.");
if(c.contains(b)) System.out.println("c는 b를 포함합니다.");
}
}
처음엔 좀 막막했는데 막상 해보니까 또 잘 짜지긴 했던... Rectagle 클래스
단 하나 문제는 boolean contains(Rectangle r) 이 부분의 조건 값 계산 실수.