Lecture/객체지향프로그래밍및실습/2007

Retired DISLab
< Lecture | 객체지향프로그래밍및실습
Swpark (토론 | 기여) 사용자의 2013년 12월 17일 (화) 18:22 버전
(비교) ← 이전 판 | 현재 판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 찾기

목차

개요

  • 박상원
  • 월 (1,2) - 213호, 금 (3,4) - 213호, 실습(A반 406호, B반 301호)
  • 조교 : 김도윤 , 장주연 (401호)

평가

  • 중간고사 : 30 %
  • 기말고사 : 30 %
  • 과제 : 30 %
  • 출석 : 10%

교안


과제

comparable과 comparator 이해

  • 제출 기간 : 6월 1일 까지
  • 레포트로 제출
  • 조교에게 실행 결과를 검사 맡을 것(401호 -A반:장주연, B반:김도윤)
  • 하루 늦을 때마다 1점 감점

1. comparable 인터페이스는 자바에서 자주 사용되는 인터페이스이다.

comparable method에서 구현해야 할 method명과 return 타입, method의 parameter를 작성하시오.

API 문서에서 Comparable 인터페이스를 참고하시오.

2. CompareTo 메소드는 두 개의 파라미터를 비교한다. 호출은 a.compareTo(b)와 같은 형태이고, 다음과 같은 결과를 준다.

1 if a is larger than b
-1 if a is smaller than b
0 if a and b are the same

아래 소스를 완성하시오. Car 클래스의 compareTo 메소드를 구현하여 연식를 비교하도록 하시오.

//TreeSetComp.java
import java.util.*;
 
class Car implements Comparable 
{
        private int age=0;
        private String name=null;
 
        public Car() {};
        public Car(String name1, int age1) {
                //내용 구현 1
        };
        public int getAge() {
                //내용 구현 2
        };
        public String getName() {
                //내용 구현 3
        };
        public String toString() {
                return "[name="+name+", age="+age+"]";
        };
 
        public int compareTo(Object obj) {
                /**
                내용구현 4
                두 개의 Car 객체를 비교
                obj는 다른 Car 객체임
                이 Car 객체의 연식이 obj로 들어온 다른 Car 객체의 연식보다 많으면 1을 return
                이 Car 객체의 연식이 obj로 들어온 다른 Car 객체의 연식보다 적으면 -1을 return
                두 Car 객체의 연식이 같으면 0을 return
                */
        }
};
 
class TreeSetComp
{
    public static void main(String[] args) 
    {
        Car cars[] = {
                        new Car("싼타페",1999),
                        new Car("쏘렌토",2005),
                        new Car("아반떼",2007),
                        new Car("렉스턴",2000),
                        new Car("마티즈",2003),
                        new Car("에쿠스",1998), };
 
        Set set = new TreeSet(Arrays.asList(cars));
 
        //System.out.println(set);
 
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
                System.out.println(iter.next());
        }
    }
}

3. Comparable이 this 객체와 인자로 받은 객체를 비교한다는 점만 빼면 Comparator는 comparable과 같다. Comparator를 만들 때는 Comparator interface를 구현하도록 하고, compare() method를 통해서 비교를 한다. 다음의 Person 객체의 나이를 비교하는 compare() method를 구현하시오.

//CompTest.java
import java.util.*;
 
class Person {
        private int age = 0;
        private String name = null;
 
        public Person(int a, String n) {
                //내용 구현 1
        }
        public int getAge() {
                //내용 구현 2
        }
 
        public String toString(){
                return ("(age="+ age + ", name=" + name + ")");
        }
}
 
class PersonComp implements Comparator {
        public int compare(Object obj1, Object obj2) {
                //내용 구현 3
        }
}
 
public class CompTest {
        public static void main(String args[]) {
                Set s = new TreeSet(new PersonComp());
                s.add(new Person(22, "황진이"));
                s.add(new Person(29, "김좌진"));
                s.add(new Person(25, "장보고"));
                s.add(new Person(28, "홍길동"));
                s.add(new Person(21, "이순신"));
                s.add(new Person(23, "김유신"));
 
                System.out.println(s);
        }
}

실습

개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기