프로그래밍 언어/JAVA

<Java> 명품 Java programming 4장 실습문제 1~4 해답

창조적생각 2021. 7. 6. 15:06

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

 

public class practice4_1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
TV myTV = new TV("LG",2017,32);
myTV.show();
}

[실행결과]

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

[풀이]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class TV{
    String name;
    int year, inch;
     TV(String name,int year,int inch) {
         this.name = name;
         this.year = year;
         this.inch = inch;
     }
     void show() {
         System.out.print(this.name+"에서 만든 "+this.year+"년형 "+this.inch+"인치 TV");
    } // show 메소드 작성
}
public class practice4_1 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TV myTV = new TV("LG",2017,32);
        myTV.show();
    }
 
}
 
cs

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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();
    }
 
}
cs

 

[실행결과]

수학, 과학, 영어 순으로 3개의 점수 입력>>90 88 96
평균은91.0

 

[풀이]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
    }
    double average() {
        return (math + science + english)/3;
    }
}
public class practice4_2 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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();
    }
 
}
 
cs

 

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

 

-필드 생략 -

 

[실행결과]

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

 

[풀이]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Song{
    String title;
    String artist;
    int year;
    String country;
    
    Song(){}
    
    Song (String title, String artist, int year, String country){
        this.title = title;
        this.artist = artist;
        this.year = year;
        this.country = country;    
    }
    void show() {
        System.out.print(year + "년 " +country + "국적의 " + artist + "가 부른 " + title);
    }
}
public class practice4_3 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Song mysong = new Song("Dancing Queen","ABBA",1978,"스웨덴");
        mysong.show();
    }
 
}
 
cs

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

 

- 멤 버 생 략 -

 

1
2
3
4
5
6
7
8
9
10
11
12
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle r = new Rectangle(2287);
        Rectangle s = new Rectangle(5566);
        Rectangle t = new Rectangle(111010);
        
        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를 포함합니다.");
    }
 
cs

[실행결과]

 

(2,2)에서 크기가 8x7인 사각형
s의 면적은36
t는 r을 포함합니다.

 

[풀이]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
    }
    int square() {
        return width*height;
    }
    void show() {
        System.out.println("(" +x+","+y+ ")에서 크기가 "+width+"x"+height+"인 사각형");
    }
    boolean contains(Rectangle r) {
        if(r.x + r.width <= width && r.y + r.height <= width)
            return true;
        else return false;
        
    }
    
    
}
 
public class practice4_4 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle r = new Rectangle(2287);
        Rectangle s = new Rectangle(5566);
        Rectangle t = new Rectangle(111010);
        
        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를 포함합니다.");
    }
 
}
 
cs
728x90