ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Power JAVA 10장 ) 인터페이스와 다형성 - Programming_4
    IT/솔루션) Power Java 2019. 5. 12. 17:58

    <2016. 10. 11. 12:31>

     

    Power JAVA 10장 ) 인터페이스와 다형성 - Programming_4

     

    /* 
     * Person 이라는 클래스를 정의하라.
     * Person은 이름(name)과 키(height)를 필드로 가진다.
     * Person은 본문에 나오는 Comparable 인터페이스를 구현한다.
     * 이 Comparable 인터페이스를 이용하여서 가장 키 큰 사람의 이름을 반환하는
     * 메소드 getMaximum(Person[] array)를 구현하고 테스트하라.
     */
    package Programming_4;
    import java.util.*;
    //Person 이라는 클래스를 정의하라. Person은 본문에 나오는 Comparable 인터페이스를 구현한다.
    class Person implements Comparable {
    	// Person은 이름(name)과 키(height)를 필드로 가진다.
    	private String name;
    	private int height;
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getHeight() {
    		return height;
    	}
    	public void setHeight(int height) {
    		this.height = height;
    	}
    	public Person(String name,int height) {
    		this.name = name;
    		this.height = height;
    	}
    	public int compareTo(Object obj) {
    		Person other = (Person) obj;
    		if (height > other.height)
    		   return 1; else if (height < other.height)
    		   return -1; else
    		   return 0;
    	}
    	public String toString() {
    		return name+","+height;
    	}
    }
    public class PersonTest {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		Person[] people = new Person[5];
    		for (int i=0; i< people.length; i++) {
    			System.out.print(" 이름 입력 : ");
    			people[i] = new Person(input.nextLine(),(int)(Math.random()*30)+160);
    			System.out.println(people[i].toString());
    		}
    		System.out.printf("가장 키가 큰 사람은 %s 입니다.\n",getMaximum(people));
    	}
    	//이 Comparable 인터페이스를 이용하여서 가장 키 큰 사람의 이름을 반환하는 메소드 getMaximum(Person[] array)를 구현하고 테스트하라.
    	public static String getMaximum(Person[] array) {
    		int mostarray = 0;
    		String mostname;
    		for (int i=1; i<array.length; i++) {
    			if(array[i].compareTo(array[mostarray]) == 1) {
    				mostarray = i;
    				mostname = array[i].getName();
    			}
    		}
    		return array[mostarray].getName();
    	}
    }

    댓글

다치지 말고 운동하자.