ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Week1] 1장 객체지향 모델링 실습
    25 - 2/설계패턴 2025. 9. 24. 15:58

    1. 체크포인트

    public class Main {
        public static void main(String[] args) {
            Person person = new Person("홍길동");
            Phone phone1 = new Phone("010-1234-5678");
            Phone phone2 = new Phone("02-123-1234");
            Phone phone3 = new Phone("010-2345-6789");
            person.addPhone(phone1);
            person.addPhone(phone2);
            person.addPhone(phone3);
        }
    }

    public class Person {
        private String name="";
        Phone[] phones;
        int length=0;

        public Person(String name){
            phones = new Phone[2];
            this.name=name;

        }

        public void addPhone(Phone phone){
            if (length==2){
                System.out.println("더 이상 전화를 추가할 수 없습니다.");
                return;
            }
            phones[length]=phone;
            length++;
            System.out.println(phone.getPhoneNumber()+"이 추가되었습니다.");
        }
    }
    public class Phone {
        String phoneNumber="";

        public Phone(String phoneNumber){
            this.phoneNumber = phoneNumber;
        }

        public String getPhoneNumber(){
            return phoneNumber;
        }

    }

    public class Main {
        public static void main(String[] args) {
            Person person = new Person("홍길동");
            Phone homePhone1 = new Phone("집전화1", "거실", "02-123-1234");
            Phone homePhone2 = new Phone("집전화2", "안방", "02-123-1245");
            Phone officePhone = new Phone("사무실전화", "사무실","02-760-1234");

            person.addHomePhone(homePhone1);
            person.addHomePhone(homePhone2);
            person.addOfficePhone(officePhone);
        }
    }

    import java.util.Vector;

    public class Person {
        private String name;
        private Vector<Phone> homePhones;
        private Phone officePhone;

        public Person(String name){
            this.name=name;
            homePhones = new Vector<>();
        }

        public void addHomePhone(Phone phone){
            System.out.println(phone.getPhoneName()+"이 추가되었습니다.");
            System.out.println(phone.getPhoneLocation()+"에 있습니다.");
            homePhones.add(phone);
        }

        public void addOfficePhone(Phone phone){
            System.out.println(phone.getPhoneName()+"이 추가되었습니다.");
            System.out.println(phone.getPhoneLocation()+"에 있습니다.");
            officePhone = phone;
        }
    }

    public class Phone {
        private String phoneName;
        private String phoneLocation;
        private String phoneNumber;

        public Phone(String phoneName, String phoneLocation, String phoneNumber){
            this.phoneName=phoneName;
            this.phoneLocation=phoneLocation;
            this.phoneNumber=phoneNumber;
        }

        public String getPhoneName(){
            return phoneName;
        }

        public String getPhoneLocation(){
            return phoneLocation;
        }

        public String getPhoneNumber(){
            return phoneNumber;
        }
    }

     

    2. 아래 관계에 대해 코드로 작성하시오

    public class Main {
        public static void main(String[] args) {
            Student student = new Student("홍길동", "250113", "컴퓨터공학");
            Course course1 = new Course("설계패턴", "COM123");
            Course course2 = new Course("웹프로그래밍", "COM345");
            Course course3 = new Course("캡스톤디자인", "COM567");
            student.registerCourse(course1);
            student.registerCourse(course2);
            student.registerCourse(course3);
            student.dropCourse(course3);

        }
    }

    public class Course {
        private String courseName;
        private String courseNumber;

        public Course(String courseName, String courseNumber){
            this.courseName=courseName;
            this.courseNumber=courseNumber;
        }

        public String getCourseName(){
            return courseName;
        }
    }

    import java.util.Vector;

    public class Student {
        private String name;
        private String stdNumber;
        private String major;
        private Vector<Course> courses;

        public Student(String name, String stdNumber, String major){
            this.name=name;
            this.stdNumber=stdNumber;
            this.major=major;
            courses=new Vector<>();
        }

        public void registerCourse(Course course){
            System.out.println(course.getCourseName()+"이 등록되었습니다.");
            courses.add(course);
        }

        public Vector<Course> dropCourse(Course course){
            System.out.println(course.getCourseName()+"을 삭제합니다.");
            courses.remove(course);
            return courses;
        }
    }

     

    3. 아래 관계에 대해 코드로 작성하시오

    public class Main {
        public static void main(String[] args) {
            Bird bird = new Bird();
            Plane plane = new Plane();

            bird.fly();
            plane.fly();
        }
    }

    package org.example;

    public class Bird implements Flyable{
        @Override
        public void fly() {
            System.out.println("새가 날아오릅니다.");
        }
    }

    package org.example;

    public interface Flyable {
        void fly();
    }
    package org.example;

    public class Plane implements Flyable{
        @Override
        public void fly() {
            System.out.println("비행기가 이륙합니다.");
        }
    }

     

Designed by Tistory.