명품 자바 4장 실습문제 (1번 ~ 5번)

틀린 부분이 있다면 댓글 남겨주세요 :) 

 

 

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

 

public class TV {
    private String brand;
    private int year;
    private int inch;

    public TV(String brand, int year, int inch) {
        this.brand = brand;
        this.year = year;
        this.inch = inch;
    }

    public void show() {
        System.out.println(brand + "에서 만든 " + year + "년형 " + inch + "인치 TV");
    }
}
    
public class Practice {
    public static void main(String [] args) {
        TV myTV = new TV("LG", 2017, 32);
        myTV.show();
    }
}

 

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

 

public class Grade {
    private int math;
    private int english;
    private int science;

    public Grade(int math, int science, int english) {
        this.math = math;
        this.science = science;
        this.english = english;
    }

    public int average() {
        int avg = (math + science + english ) / 3; 
        return avg;
    }
}


public class Practice {
    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 클래스를 작성하라.

 

public class Song {
    private int year;
    private String country;
    private String artist;
    private String title;

    public Song() {
        //기본 생성자
    }

    public Song(int year, String country, String artist, String title) {
        this.year = year;
        this.country = country;
        this.artist = artist;
        this.title = title;
    }

    public void show() {
        System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
    }
}

public class Practice {
    public static void main(String [] args) {
        Song song = new Song(1978, "스웨덴", "ABBA", "Dancing Queen");
        song.show();
    }
}

 

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

 

public 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() {
        int area = width * height;
        return area;
    }

    public boolean contains(Rectangle r) {
        if (this.x > r.x && this.y > r.y && this.width + this.x < r.width + r.x && this.height + this.y > r.height + r.y) {
            return true;
        } 
        else {
            return false;
        }
    }

    public void show() {
        System.out.println("(" + x + ", " + y + ")" + "에서 크기가" + width + "x" + height +"인 사각형");
    }
}

public class Practice {
    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;

public class CircleManager {
    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();
    }
}

public 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("(" + x +", " + y + ")" + radius);
    }
}

댓글