자바 프로그래밍 16번째 수업

김형우·2022년 11월 14일
0

Java

목록 보기
15/22

1. throws 에 대하여 설명하시오.

throws ---
: ---Exception이 발생했을때 현재 메서드를 호출한 메서드의 상위 메서드에 Exception의 처리를 맡깁니다.

2.checked 와 unckecked Excetpion 을 설명하시오.

체크 예외(Checked Exception)
체크 예외는 RuntimeException의 하위 클래스가 아니면서 Exception 클래스의 하위 클래스들입니다. 체크 예외의 특징은 반드시 에러 처리를 해야하는 특징(try/catch or throw)을 가지고 있습니다.
존재하지 않는 파일의 이름을 입력(FileNotFoundException)
실수로 클래스의 이름을 잘못 적음(ClassNotFoundException)

언체크 예외(Unchecked Exception)
언체크 예외는 RuntimeException의 하위 클래스들을 의미합니다. 이것은 체크 예외와는 달리 에러 처리를 강제하지 않습니다.
말 그대로 실행 중에(runtime) 발생할 수 있는 예외를 의미합니다.
배열의 범위를 벗어난(ArrayIndexOutOfBoundsException)
값이 null이 참조변수를 참조(NullPointerException)

(수업중 아파서 제대로 못들어서 다른 웹사이트 참조)

3. 아래가 컴파일 에러가 나는 이유에 대하여 설명하시오.

try {
		int num = 6 / 0;
	} catch (Exception e) {
		e.printStackTrace();
	} catch (InputMismatchException e) {
		e.printStackTrace();
	}

4. try - catch - finally 에 대하여 설명하시오.

try : 예외가 날수 있는 부분에 대해 기술

catch : 예외가 발생했을때 대체할 내용을 기술

finally : catch까지 실행한 뒤, 최소 적어도 한번은 실행되는 코드이다.
finally 구문은 필수가 아니기 때문에, 사용하지 않아도 된다.

5.다음을 프로그래밍 하시오.

  • 소스
    Fruit fAry[] = {new Grape(), new Apple(), new Pear());
    for(Fruit f : fAry)
    f.print();

  • 결과
    나는 포도이다.
    나는 사과이다.
    나는 배이다.

소스코드

	
interface Fruits {
	public abstract void print();
}

class Grape implements Fruits {
	public void print() {
		System.out.println("나는 포도다");
	}
}

class Apple implements Fruits {
	public void print() {
		System.out.println("나는 사과다");
	}
}

class Pear implements Fruits {
	public void print() {
		System.out.println("나는 배다");
	}
}

public class Fruit {

	public static void main(String[] args) {
		Fruits fAry[] = { new Grape(), new Apple(), new Pear() };
		for (Fruits fruits : fAry) {
			fruits.print();
		}
	}
}

6. 다음은 도형 구성을 묘사하는 인터페이스이다.

interface Shape {
	final double PI = 3.14; // 상수
	void draw(); // 도형을 그리는 추상 메소드
	
	double getArea(); // 도형의 면적을 리턴하는 추상 메소드
	default public void redraw() { // 디폴트 메소드
		System.out.print("--- 다시 그립니다.");
		draw();
	}
}

다음 main() 메소드와 실행 결과를 참고하여, 인터페이스 Shape을 구현한 클래스 Circle를 작성하고 전체 프로그램을 완성하라.

public class ShapeMain {
	public static void main(String[] args) {
		Shape donut = new Circle(10); // 반지름이 10인 원 객체
		donut.redraw();
		System.out.println("면적은 "+ donut.getArea());
	}
}

소스코드

class Circle implements Shape {
	
	int radius;
	
	Circle(int radius){
		this.radius = radius;
	}
	@Override
	public double getArea() {
		return radius*radius*PI;
	}
	@Override
	public void draw() {
		System.out.println("반지름이"+radius+"인 원입니다.");
	}
}

7.다음 main() 메소드와 실행 결과를 참고하여,

문제 6의 Shape 인터페이스를 구현한 클래스 Oval, Rect를 추가 작성하고 전체 프로그램을 완성하라.

public static void main(String[] args) {
	Shape[] list = new Shape[3]; // Shape을 상속받은 클래스 객체의 레퍼런스 배열
	list[0] = new Circle(10); // 반지름이 10인 원 객체
	list[1] = new Oval(20, 30); // 20x30 사각형에 내접하는 타원
	list[2] = new Rect(10, 40); // 10x40 크기의 사각형
	for(int i=0; i<list.length; i++) list[i].redraw();
	for(int i=0; i<list.length; i++) System.out.println("면적은 "+ list[i].getArea());
}
class Oval implements Shape {

	int a, b;

	Oval(int a, int b) {
		this.a = a;
		this.b = b;
	}

	@Override
	public void draw() {
		System.out.println(a + "x" + b + "사각형에 내접하는 타원입니다.");
	}

	@Override
	public double getArea() {
		return a * b * PI;
	}

}

class Rect implements Shape {

	int x,y;
	
	Rect (int a, int b){
		this.x = a;
		this.y = b;
	}
	
	@Override
	public void draw() {
		System.out.println(x+"x"+y+"크기의 사각형입니다.");
	}

	@Override
	public double getArea() {
		return x*y;
	}
	
}

출력결과

--- 다시 그립니다.반지름이 10인 원입니다.
--- 다시 그립니다.20x30에 내접하는 타원입니다.
--- 다시 그립니다.10x40크기의 사각형 입니다.
면적은 314.0
면적은 1884.0000000000002
면적은 400.0

8. 래퍼 클래스(Wrapper class)란 무엇인가?

기본 자료타입(primitive type)을 객체로 다루기 위해서 사용하는
클래스들을 래퍼 클래스(wrapper class)라고 한다.

9.auto unboxing 이란?

래퍼 클래스를 기본 자료타입형으로 선언된 변수에 대입하는 것을 
auto unboxing이라고 표현한다.

예) 

Integer iObj = 10; (auto boxing)
             // = new Integer(10);과 같은 역할
int num1 = iObj; (auto unboxing)

10.BigInteger 클래스의 용도는?

매우 큰 정수를 표현하기 위해서 사용된다.
profile
개발자 지망생

0개의 댓글