11월 15일 - 수업 17일차

수아레스·2022년 11월 15일
post-thumbnail

1. 다음 조건을 만족하는 클래스 Person을 구현하여 테스트하는 프로그램을 작성하시오. (필수) ⭐️

- 클래스 Person은 이름을 저장하는 필드 구성
- 클래스 Person은 상위 클래스 Object의 메소드 equals()를 오버라이딩하여
이름이 같으면 true를 반환하는 메소드 구현
- 다음과 같은 소스로 클래스 Person을 점검

Person p1 = new Person("홍길동");
System.out.println(p1.equals(new Person("홍길동")));
System.out.println(p1.equals(new Person("최명태")));
class PersonE{
	String name;
	
	public PersonE(String name) {
		this.name = name;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof PersonE) {	
			if(this.name == ((PersonE)obj).name)
				return true;
	}
		return false;
}
}

public class PersonTest {
	public static void main(String[] args) {
		PersonE p1 = new PersonE("홍길동");
		System.out.println(p1.equals(new PersonE("홍길동")));
		System.out.println(p1.equals(new PersonE("최명태")));
	}
}

2. 아래를 참고하여, 메소드 equals()와 연산자 == 의 차이를 비교 설명하시오. (필수) ⭐️

	public static void main(String[] args) {
		String s1 = new String("java");
		String s2 = new String("java");
		String s3 = s2;

		System.out.println(s1 == s2);
		System.out.println(s1.equals(s2));
		System.out.println(s2 == s3);
		System.out.println(s2.equals(s3));
		
		if(s1 == s2)
			System.out.println("s1, s2 참조 대상 같다.");
		else
			System.out.println("s1, s2 참조 대상 다르다. ");
		
		if(s2 == s3)
			System.out.println("s2, s3 참조 대상 같다.");
		else
			System.out.println("s2, s3 참조 대상 다르다. ");
	
		if(s1.equals(s2))
			System.out.println("s1, s2 내용 동일하다.");
		else
			System.out.println("s1, s2 내용 다르다.");
		
		if(s2.equals(s3))
			System.out.println("s2, s3 내용 동일하다.");
		else
			System.out.println("s2, s3 내용 다르다.");

== 연산: 주소값 비교
equals: 문자열의 내용 비교

new로 객체 생성을 하면 참조 주소가 저장된다.

== 연산시에는 주소값을 비교하게 된다.

참조 주소가 아닌 문자열의 내용 비교를 위해서는 equals를 오버라이드 해서 사용해야 하지만 이미 String클래스에 equals가 오버라이딩 되어있으므로 그냥 사용하면 된다.

3. 아래의 결과값은 false가 출력이 된다. true가 되도록 INum을짜시오.

INum[] ar1 = new INum[3];
INum[] ar2 = new INum[3];

ar1[0] = new INum(1); ar2[0] = new INum(1);
ar1[1] = new INum(2); ar2[1] = new INum(2);
ar1[2] = new INum(3); ar2[2] = new INum(3);

System.out.println(Arrays.equals(ar1, ar2));

import java.util.Arrays;

class INum2{
	
	int num;
	
	public INum2(int num) {
		this.num = num;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this.num == ((INum2)obj).num) {
			return true;
		}
			return false;
	}
	
}

public class Equals2 {
	public static void main(String[] args) {

		INum[] ar1 = new INum[3];
		INum[] ar2 = new INum[3];

		ar1[0] = new INum(1);
		ar2[0] = new INum(1);
		ar1[1] = new INum(2);
		ar2[1] = new INum(2);
		ar1[2] = new INum(3);
		ar2[2] = new INum(3);

		System.out.println(Arrays.equals(ar1, ar2));
	}
}

4. 제네릭(Generic)이란?

클래스나 메소드에서 사용할 내부 데이터 타입을 컴파일 시에 미리 지정하는 방법

  1. 클래스나 메소드 내부에서 사용되는 객체의 타입 안정성을 높일 수 있다.
  2. 반환값에 대한 타입 변환 및 타입 검사에 들어가는 노력을 줄일 수 있다.

<선언>

class MyArray<T> {

    T element;

    void setElement(T element) { this.element = element; }

    T getElement() { return element; }

}

<생성>

MyArray<Integer> myArr = new MyArray<Integer>();

5. Generic 이전의 코드의 문제 상황들을 예를 들어 설명하시오.

  1. 실수가 컴파일 과정에서 발견되지 않는다.
public static void main(String[] args) {
⁠   Box aBox = new Box();
⁠   Box oBox = new Box();
⁠
⁠   // 아래 두 문장에서는 사과와 오렌지가 아닌 '문자열'을 담았다.
⁠   aBox.set("Apple");
⁠   oBox.set("Orange");
⁠
⁠   // 상자에 과일이 담기지 않았는데 과일을 꺼내려 한다.
⁠   Apple ap = (Apple)aBox.get();
⁠   Orange og = (Orange)oBox.get();
⁠
⁠   System.out.println(ap);
⁠   System.out.println(og);
⁠}
  1. 실수가 실행 과정에서 발견되지 않는다. (오류가 나지 않고 정상적으로 작동됨)
public static void main(String[] args) {
⁠   Box aBox = new Box();
⁠   Box oBox = new Box();
⁠
⁠   // 다음 두 문장은 프로그래머의 실수이다!
⁠   aBox.set("Apple");
⁠   oBox.set("Orange");
⁠
⁠   System.out.println(aBox.get());
⁠   System.out.println(oBox.get());
⁠}

6. 아래가 돌아가도록 DBox를 완성 하시오.

public static void main(String[] args) {
   DBox<String, Integer> box = new DBox<String, Integer>();
   box.set("Apple", 25);
   System.out.println(box);
}

class DBox<T1,T2>{
	private T1 ob1;
	private T2 ob2;
	
	public void set(T1 ob1, T2 ob2) {
		this.ob1 = ob1;
		this.ob2 = ob2;
	}
	
	@Override
	public String toString() {
		return ob1 + "  " + ob2;
	}
}

public class DBoxMain {
	public static void main(String[] args) {
			DBox<String, Integer> box = new DBox<String, Integer>();
	        box.set("Apple", 25);
	        System.out.println(box);
		}
}
profile
띵호와

0개의 댓글