ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 쉽게 배우는 자바 프로그래밍 2판 7장 프로그래밍 문제 Part.2 (6번 ~ 9번)
    JAVA/연습문제 2022. 10. 21. 02:51
    반응형

    # 개발 공부하는 초보자가 작성한 답안이니 정답이 아닐 수 있습니다!!!

     

     

     

    6. 다음과 같이 Human 인터페이스와 Human 구현 클래스인 Worker가 있다. 또한 다음과 같이 HumanTest라는 테스트 프로그램을 실행하고자 한다. 이 테스트 프로그램은 Worker 클래스뿐만 아니라 Human 인터페이스를 구현한 Student 클래스를 사용한다. 두 클래스는 모두 print()와 echo() 메서드를 포함한다. 여기서 Worker 클래스는 다른 프로젝트에 이용되고 있으므로 수정할 수 없으며 echo() 메서드는 구현 객체를 생성하지 않고서 호출됨을 유의하라. HumanTest 프로그램의 실행을 위하여 Human 인터페이스를 수정하고, Student 클래스를 정의하라.

    📢주어진 조건값📢
    public class Worker implements Human{
        @Override
        public void eat() {
            System.out.println("빵을 먹습니다.");
        }
    }
    
    public class HumanTest {
        public static void main(String[] args) {
            Human.echo();
    
            Student s = new Student(20);
            s.print();
            s.eat();
    
            Human p = new Worker();
            p.print();
            p.eat();
        }
    }
    📢주어진 결과값📢
    야호!!!
    20세의 학생입니다.
    도시락을 먹습니다.
    인간입니다.
    빵을 먹습니다.
    🧤정답🧤
    public class HumanTest {
        public static void main(String[] args) {
            Human.echo();
    
            Student s = new Student(20);
            s.print();
            s.eat();
    
            Human p = new Worker();
            p.print();
            p.eat();
        }
    }
    
    class Worker implements Human{
        @Override
        public void eat() {
            System.out.println("빵을 먹습니다.");
        }
    }
    
    public class Student extends Worker implements Human{
        int age;
        Student (int age){
            this.age = age;
        }
        public void print(){
            System.out.println(age + "세의 학생입니다.");
        }
    
        @Override
        public void eat(){
            System.out.println("도시락을 먹습니다.");
        }
    }
    
    interface Human {
        static void echo() {
            System.out.println("야호!!!");
        }
    
        //Worker 클래스는 수정할 수 없다는 조건으로 인해 default라는 예약어를 붙여 구현 메서드를 만든다.
        default void print() {
            System.out.println("인간입니다.");
        }
        void eat();
    }

     

     

    7. Flyable 인터페이스와 테스트 프로그램을 실행한 결과가 다음과 같다. Flyable 인터페이스를 지역 클래스로 이용하는 테스트 프로그램을 완성하라.

    📢주어진 조건📢
    interface Flyable {
    	void speed();
        void height();
    }
    
    public class FlyableTest {
        public static void main(String[] args) {
            Flyable f = ______________;  // 한 행 이상의 코드 필요
            f.speed();
            f.height();
        }
    }
    📢주어진 결과값📢
    속도
    높이
    🧤정답🧤 7장 7번 삭제하면 안됑
    public class FlyableTest {
        public static void main(String[] args) {
            Flyable f = new Flyable() {
                @Override
                public void speed() {
                    System.out.println("속도");
                }
    
                @Override
                public void height() {
                    System.out.println("높이");
                }
            };
            f.speed();
            f.height();
        }
    }

     

     

    8. 다음과 같은 Echoer 클래스와 테스트 프로그램, 실행 결과가 있다. 테스트 프로그램은 Echoer 클래스를 이용해 키보드로 입력한 메시지를 화면에 출력한다. 테스트 프로그램을 완성하라.

    📢주어진 조건📢
    abstract class Echor {
        void start() {
            System.out.println("시작합니다.");
        }
        
        abstract void echo();
        
        void stop() {
            System.out.println("종료합니다.");
        }
    }
    
    public class EchoerTest {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Echoer e = _______________;
            e.start();
            e.echo();
            e.stop();
        }
    }
    📢주어진 결과값📢
    시작합니다
    좋은 아침!  //  <==== 입력값
    좋은 아침!
    잘 가세요.  //  <==== 입력값
    잘 가세요.
    끝  //  <==== 입력값
    끝
    종료합니다.
    🧤정답🧤
    abstract class Echor {
        void start() {
            System.out.println("시작합니다.");
        }
    
        abstract void echo();
    
        void stop() {
            System.out.println("종료합니다.");
        }
    }
    
    public class EchoerTest {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Echor e = new Echor() {               // 익명클래스는 한번만 쓰일려고 만듬.
                @Override
                void echo() {
                    String name = "";
                    while (!name.equals("끝")){
                        name = in.nextLine();
                        System.out.println(name);
                    }
                }
            };
            e.start();
            e.echo();
            e.stop();
        }
    }

     

     

    9. 다음과 같은 테스트 프로그램과 실행 결과가 있다. 호텔의 객실 예약과 예약 현황을 나타낼 수 있도록 Hotel 클래스를 작성하라. 호텔에는 10개의 객실이 있으며, 객실 번호는 1~10번까지이다. 객실 번호와 이름으로 객실을 예약하는 add() 메서드, 현재 예약된 객실 번호와 고객 이름을 보여 주는 show() 메서드가 필요하다.

    📢주어진 조건📢
    호텔에는 다수의 객실이 있지만, 객실은 호텔 내부에서만 사용한다. 따라서 객실을 위한 Room 클래스는 Hotel 클래스의 중첩 클래스로 작성하면 편리하다
    📢주어진 결과값📢
    5번 방을 호돌이가 예약했습니다.
    7번 방을 길동이가 예약했습니다.
    🧤정답🧤 팁대로 안한거
    public class HotelTest {
        public static void main(String[] args) {
            Hotel hotel = new Hotel();
            hotel.add(5,"호돌이");
            hotel.add(7,"길동이");
            hotel.show();
        }
    }
    
    public class Hotel {
        String[] room = new String[10];
        void add(int number, String name) {
            if(room[number]==null)
            room[number] = name;
            else System.out.println("현재 예약이 차있는 방입니다.");
        }
        void show() {
            for (int i = 0; i< room.length; i++) {
                if (room[i]!=null) System.out.println(i + "번 방을 " + room[i] + "가 예약했습니다.");
            }
        }
    }
    🧤정답🧤 팁대로 한거
    public class HotelTest {
        public static void main(String[] args) {
            Hotel hotel = new Hotel();
            hotel.add(5,"호돌이");
            hotel.add(7,"길동이");
            hotel.show();
        }
    }
    
    public class Hotel {
        int number;
        String name;
        class Room {
            String[] hotelRoom = new String[10];
            void addReservation(){
                hotelRoom[Hotel.this.number-1] = Hotel.this.name;
            }
            void addReservationShow(){
                for (int i = 0; i<hotelRoom.length; i++){
                    if (hotelRoom[i]!=null) System.out.printf("%d번 방을 %s가 예약했습니다.\n", i+1, hotelRoom[i]);
                }
            }
        }
        Room ReservationSituation = new Room();
            void add(int number, String name) {
                this.number = number;
                this.name = name;
                ReservationSituation.addReservation();
            }
    
            void show() {
                ReservationSituation.addReservationShow();
            }
    }

     

     

     

     

     

     

    반응형

    댓글

Designed by Tistory.