관리 메뉴

취미개발 블로그와 마음수양

인터페이스를 이용하여 가장 키가 큰 녀석을 구하기 본문

Language/java소스

인터페이스를 이용하여 가장 키가 큰 녀석을 구하기

아라한사 2014. 4. 14. 11:56
으음...;; 소스 더 이쁘게 안나오나;;

Colored By Color Scripter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package 챕12인터페이스;
 
interface Comparable{
    //이 객체가 다른 객체보다 크면 1 같으면 0 작으면 -1을 반환한다.
    int compareTo(Object object);
}
 
public class Person implements Comparable{
    String name;
    int height;
    static Person a[] = {new Person("홍길동", 2010), new Person("전우치", 170), new Person("아수라", 190), new Person("캡틴아메리카", 200)};
    
    public Person(){
        name = "이름없음";
        height = 100;
    }
    
    public Person(int a){
        name="이름모름";
        height = a;
    }
    public Person(String a, int b){
        name = a;
        height = b;
    }
    
    public int compareTo(Object otherObject){
        
        Person other  = (Person) otherObject;
        if(this.height > other.height)return 1;
        else if(this.height< other.height)return -1;
        else return 0;
        
        
    }
    
    public static int getMaximum(Person[] array){
        int b=0;
        int check;
        for(int i=0;i<array.length;i++){
            check = array[b].compareTo(array[i]);
            if(check==-1)
                b=i;
        }
        
        return b;
    }
    
    public static void main(String[] args) {
        
        System.out.println(a[getMaximum(a)].name);
        
    }
 
}