다라다라V
article thumbnail
728x90
반응형

 

 

모든 문제의 패키지명은 변경 가능합니다.

💡 

 

[정답]

package test.ch04;

public class TV {
    private String manufacturer;
    private int year; 
    private int size;

    public TV(String manufacturer, int year, int size) {
        this.manufacturer = manufacturer;
        this.year = year;
        this.size = size;
    }
    
    public void show() {
        System.out.println(manufacturer + "에서 만든 " + year + "년형 " + size + "인치 TV");
    }
}

 

 

💡 

 

[정답] 

package test.ch04;

public 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;
    }

    // 출력 결과가 int 형이므로 int를 반환
    public int average() {
        return (math + science + english) / 3;
    }
}

 

💡 

 

[정답]

import java.util.Scanner;

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 DaraSolution {
    public static void main(String[] args) {
        Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
        song.show();
    }
}

 

💡 

 

[정답]

import java.util.Scanner;
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 + "인 사각형");
    }
    
    // 매개변수로 받은 r이 현 사각형 안에 있으면 true 리턴
    boolean contains(Rectangle r) {
        if (r.x >= x && r.y >= y && r.x + r.width <= x + width && r.y + r.height <= y + height) {
            return true;
        } else {
            return false;
        }
    }
}

public class DaraSolution {
    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("사각형의 넓이는 " + r.square());
        if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
        if(t.contains(s)) System.out.println("t는 s를 포함합니다.");
    }
}

 

 

💡 

 

[정답]

package test.ch04;

import java.util.Scanner;

class Circle {
    private double x, y;
    private int radius;

    // x, y, 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);
    }
}

public class CircleManager {
    public static void main(String[] args) {
        Scanner scanner = 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 = scanner.nextDouble();
            double y = scanner.nextDouble();
            int radius = scanner.nextInt();
            c[i] = new Circle(x, y, radius);
        }
        
        // 객체 출력
        for (int i = 0; i < c.length; i++) {
            c[i].show();
        }
        scanner.close();
    }
}


[해설]

 

 

💡 

 

[정답]

package test.ch04;

import java.util.Scanner;

class Circle {
    private double x, y;
    private int radius;

    // x, y, 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);
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }
}

public class CircleManager {
    public static void main(String[] args) {
        Scanner scanner = 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 = scanner.nextDouble();
            double y = scanner.nextDouble();
            int radius = scanner.nextInt();
            c[i] = new Circle(x, y, radius);
        }

        // 가장 면적이 큰 원을 찾아 출력
        int maxIndex = 0;
        for (int i = 1; i < c.length; i++) {
            if (c[i].getArea() > c[maxIndex].getArea()) {
                maxIndex = i;
            }
        }
        System.out.print("가장 면적이 큰 원은 "+ c[maxIndex].getArea() + "입니다.");
    }
}

 

💡

 

[정답]

package test.ch04;

import java.util.Scanner;

class Day {
    private String work;
    public void set(String work) { this.work = work; }
    public String get() { return work; }
    public void show() {
        if(work == null) System.out.println("없습니다.");
        else System.out.println(work + "입니다.");
    }
}
public class MonthSchedule {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Day 객체 30개 생성
        Day days[] = new Day[30];
        for (int i = 0; i < days.length; i++) {
            days[i] = new Day();
        }
        
        System.out.println("이번달 스케쥴 관리 프로그램.");
        // 조건을 만족할 때까지 실행
        while (true) {
            System.out.print("할일(입력:1, 보기:2, 끝내기:3) >> ");
            int menu = scanner.nextInt();
            if (menu == 1) {
                System.out.print("날짜(1~30)? ");
                int date = scanner.nextInt();
                System.out.print("할일(빈칸없이입력)? ");
                String work = scanner.next();
                days[date - 1].set(work);
            } else if (menu == 2) {
                System.out.print("날짜(1~30)? ");
                int date = scanner.nextInt();
                System.out.print((date) + "일의 할일은 ");
                days[date - 1].show();
            } else if (menu == 3) {
                break;
            }
        }
        scanner.close();
    }
}

💡 

 

[정답]

package test.ch04;

import java.util.Scanner;

class Phone {
    private String name;
    private String tel;

    public Phone(String name, String tel) {
        this.name = name;
        this.tel = tel;
    }

    public String getName() {
        return name;
    }

    public void show() {
        System.out.println(name + "의 번호는 " + tel + "입니다.");
    }
}

public class PhoneBook {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 전화번호부에 저장할 인원수 입력 받고
        System.out.print("인원수>>");
        int num = scanner.nextInt();
        // 그 인원만큼 Phone 객체 생성
        Phone phone[] = new Phone[num];

        // 이름과 전화번호 입력 받아 Phone 객체 생성
        for (int i = 0; i < phone.length; i++) {
            System.out.print("이름과 전화번호(이름과 번호는 빈칸없이 입력)>>");
            String name = scanner.next();
            String tel = scanner.next();
            phone[i] = new Phone(name, tel);
        }

        System.out.println("저장되었습니다...");

        // 검색할 이름이 "그만"일 때까지 반복
        while (true) {
            System.out.print("검색할 이름>>");
            String name = scanner.next();
            if (name.equals("그만")) {
                break;
            }
            
            // 이름이 같은 Phone 객체를 찾아서 출력
            for (int i = 0; i < phone.length; i++) {
                if (name.equals(phone[i].getName())) {
                    phone[i].show();
                    break;
                }
            }
            // 이름이 없으면 "찾는 이름이 없습니다." 출력
            System.out.println(name + " 이(가) 없습니다.");
        }
    }
}

 

💡 

 

[정답] 

package test.ch04;

class ArrayUtil {
    public static int[] contact(int[] a, int[] b) {
        // 배열 a와 b를 연결한 새로운 배열 리턴
        int[] result = new int[a.length + b.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = a[i];
        }
        for (int i = 0; i < b.length; i++) {
            result[a.length + i] = b[i];
        }
        return result;
    }
    public static void print(int[] a) {
        // 배열 a 출력
        System.out.print("[ ");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
            // 마지막 배열 원소를 반환할 때 불필요한 공백 제거
            if (i < a.length - 1) {
                System.out.print(" ");
            }
        }
        System.out.println(" ]");
    }
}
public class StaticEx {
    public static void main(String[] args) {
        int[] array1 = {1, 5, 7, 9};
        int[] array2 = {3, 6, -1, 100, 77};
        int[] array3 = ArrayUtil.contact(array1, array2);
        ArrayUtil.print(array3);
    }
}

 

💡 

 

[정답]

import java.util.Scanner;
class Dictionary {
    private static String[] kor = { "사랑", "아기", "돈", "미래", "희망" };
    private static String[] eng = { "love", "baby", "money", "future", "hope" };

    // 검색 코드
    public static String kor2Eng(String word) {
        for (int i = 0; i < kor.length; i++) {
            if (word.equals(kor[i])) {
                return eng[i];
            }
        }
        return null;
    }
}

public class DaraSolution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("한영 단어 검색 프로그램입니다.");
        
        while (true) {
            System.out.print("한글 단어? ");
            String word = scanner.next();
            
            // 그만을 입력받으면 프로그램 종료
            if (word.equals("그만")) {
                System.out.println("프로그램을 종료합니다.");
                break;
            }
            
            // kor2Eng 메소드 호출
            String result = Dictionary.kor2Eng(word);
            if (result == null) {
                System.out.println(word + "는 저의 사전에 없습니다.");
            } else {
                System.out.println(word + "은 " + result);
            }
        }
        scanner.close();
    }
}

 

💡 

 

[정답]

package test.ch04;

import java.util.Scanner;

class Add {
    private int a;
    private int b;
    public void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int calculate() {
        return a + b;
    }
}
class Sub {
    private int a;
    private int b;
    public void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int calculate() {
        return a - b;
    }
}
class Mul {
    private int a;
    private int b;
    public void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int calculate() {
        return a * b;
    }
}
class Div {
    private int a;
    private int b;
    public void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int calculate() {
        return a / b;
    }
}
public class Calculator {
    public static void main(String[] args) {
        // 정수와 연산자 입력
        Scanner scanner = new Scanner(System.in);
        System.out.print("두 정수와 연산자를 입력하시오>>");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        String op = scanner.next();
        
        // 연산자에 따라 객체 생성
        switch (op) {
            case "+":
                Add add = new Add();
                add.setValue(a, b);
                System.out.println(add.calculate());
                break;
            case "-":
                Sub sub = new Sub();
                sub.setValue(a, b);
                System.out.println(sub.calculate());
                break;
            case "*":
                Mul mul = new Mul();
                mul.setValue(a, b);
                System.out.println(mul.calculate());
                break;
            case "/":
                Div div = new Div();
                div.setValue(a, b);
                System.out.println(div.calculate());
                break;
            default:
                System.out.println("연산자 오류");
        }
        
    }
}

 

 

💡 

 

[정답] 

import java.util.InputMismatchException;
import java.util.Scanner;

// 좌석을 위한 클래스
class Seat {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

// 콘서트 홀
class ReservationSystem {
    // S석, A석, B석
    private Seat[][] seats = new Seat[3][10];
    private Scanner scanner = new Scanner(System.in);

    public void reservation() {
        // 좌석의 타입을 입력받고 상태를 보여줌
        System.out.print("좌석 S(1), A(2), B(3)>> ");
        int type = scanner.nextInt();
        show(type);

        // 이름과 번호를 입력받아 예약
        System.out.print("이름>> ");
        String name = scanner.next();
        System.out.print("번호>> ");
        int num = scanner.nextInt();
        seats[type - 1][num - 1] = new Seat();
        seats[type - 1][num - 1].setName(name);
    }

    public void show(int type) {
        char ch = 'A';
        if (type == 1) {
            ch = 'S';
        } else if (type == 2) {
            ch = 'A';
        } else if (type == 3) {
            ch = 'B';
        }
        System.out.print(ch + ">> ");
        for (int i = 0; i < 10; i++) {
            if (seats[type - 1][i] == null) {
                System.out.print("___ ");
            } else {
                System.out.print(seats[type - 1][i].getName() + " ");
            }
        }
        System.out.println();
    }

    public void showAll() {
        for (int i = 0; i < 3; i++) {
            show(i + 1);
        }
        System.out.println("<<<조회를 완료하였습니다.>>>");
    }

    public void cancel() {
        // 좌석의 타입을 입력받음
        System.out.print("좌석 S(1), A(2), B(3)>> ");
        int type = scanner.nextInt();
        show(type);

        // 이름을 입력받아 취소
        System.out.print("이름>> ");
        String name = scanner.next();
        for (int i = 0; i < 10; i++) {
            if (seats[type - 1][i] != null && seats[type - 1][i].getName().equals(name)) {
                seats[type - 1][i] = null;
                return;
            }
        }
    }

    public void run() {
        while (true) {
            System.out.println("예약:1, 조회:2, 취소:3, 끝내기:4 >> ");
            int menu = scanner.nextInt();
            switch (menu) {
                case 1:
                    reservation();
                    break;
                case 2:
                    showAll();
                    break;
                case 3:
                    cancel();
                    break;
                case 4:
                    return;
            }
        }
    }
}

public class DaraSolution {
    public static void main(String[] args) {
        System.out.println("명품콘서트홀 예약 시스템입니다.");
        new ReservationSystem().run();
    }
}

 

반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!