Language/JAVA

[명품 JAVA programming] CHAPTER 4 클래스와 객체(실습문제)

위시리 2023. 3. 19. 23:21

#1

public class TV {
    String company;
    int year;
    int size;

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

 

 

#3

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

  • 노래 제목 : title
  • 가수 : artist
  • 노래 발표년도 : year
  • 국적 : country

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

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

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

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

    public Song(){ // 초기화
        this("","",0,"");
        System.out.println("값을 입력하세요");
    }
    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 Ex4_3 {
    public static void main(String[] args) {
        Song song = new Song("Dancing Queen", "AABA", 1978, "스웨덴");
        song.show();
    }
}

 

 

#5

다음 설명대로 Circle 클래스와 CircleManager 클래스를 완성하라.

다음 실행 결과와 같이 3개의 Circle 객체 배열을 만들고 x,y,radius 값을 읽어 3개의 Circle 객체를 만들고 show( )를 이용하여 이들을 모두 출력한다.

x, y, radius >> 3.0 3.0 5
x, y, radius >> 2.5 2.7 6
(3.0, 3.0)5
(2.5, 2.7)6
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.print("(" + x + ", " + y + ") " + radius);
    }
}

 

import java.util.*;
public class CircleManager {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        Circle [] c = new Circle[3]; // 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();
            System.out.println();
        }
        scan.close();
    }
}

 

 

#6

앞의 5번 문제를 참고하여 Circle 클래스와 CircleManager 클래스를 수정하여 다음 실행 결과처럼 되게하라.

x, y, radius >> 3.0 3.0 5
x, y, radius >> 2.5 2.7 6
x, y, radius >> 5.0 2.0 4
가장 면적이 큰 원은 (2.5, 2.7) 6