Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
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
Tags
more
Archives
Today
Total
관리 메뉴

노승현

능단평 문제 오답노트 본문

JAVA/문제풀이

능단평 문제 오답노트

nsh95 2023. 12. 22. 21:28

1번 문제

class Shape {
	String name;
	double area;

	void draw() {
		System.out.println("모양 그리기");
	}
}

// 다형성이란??? 같은 메서드인데 다른 결과가 나오는 현상

class Circle extends Shape {		
	@Override
	void draw() {
		System.out.println("원 그리기");
	}
}

class Rectangle extends Shape {
	@Override
	void draw() {
		System.out.println("네모 그리기");	
	}
}

public class TEST {
	public static void main(String[] args) {
		Shape[] datas = new Shape[3];
		datas[0] = new Shape();
		datas[1] = new Circle();
		datas[2] = new Rectangle();
		for (Shape v : datas) {
		}
	}
}

 

같은 메서드인데 다른 결과가 나오므로 "다형성"이다.

 

 

 

class Person{
String name;
2
}
class Student extends Person{ }
public class TEST {
public static void main(String[] args) { Student student=new Student();
} }

 

extends 를 받아 출력을 하고 있기 때문에 "상속"이다.

class Book{
private int num;
public int getNum() { return num;
}
public void setNum(int num) {
this.num = num;
} }

 

private 로 선언되어 있지만 set을 사용하고 있기때문에 "캡슐화" 이다.

 

 

abstract class Pokemon{ abstract void hello();
}
class Pikachu extends Pokemon{
@Override void hello() { }
}
class Butterfree extends Pokemon{
@Override void hello() { }
}

 

오버라이딩을 사용하고 있어서 "추상화" 이다.

 

 

2번문제

 

package testReview;

public class Problem2 {
	public static void main(String[] args) {
		int a = 10;
		int b = 11;
		if (++a % 2 == 0 || b++ % 2 == 0) {	// 전위증감식이기 때문 a==11 , b는 후위 증감식이기때문에 여기까지는 b=11 이기에	
											// else로 가고 11 이되고 b 는 12가 된
			System.out.println("로그A");
			System.out.println("a: " + a);
			System.out.println("b: " + b);
		} else {
			System.out.println("로그B");
			System.out.println("a: " + a);
			System.out.println("b: " + b);
		}
		int c = a++ * --b;
		System.out.println("a: " + a);
		System.out.println("b: " + b);
		System.out.println("c: " + c);
	}
}

 

 

전위증감식이기 때문 a==11 , b는 후위 증감식이기때문에 여기까지는 b=11 이기에

 else로 가고 11 이되고 b 는 12가 된다.

c 는  a 가 후위 연산자이기때문에 11  b 는 전위 연산자이어서 11  곱한 결과121이 된다.

 

 

문제3

 

package testReview;

public class Problem3 {

	public static void main(String[] args) {
		int[] datas = { 8, 2, 1, 9, 7 };

		int max = datas[0];
		int maxIndex = 0;
		for (int i = 1; i < datas.length; i++) {
			if (max < datas[i]) {
				max = datas[i];
				maxIndex = i;
			}
		}
		System.out.println("max: " + max);
		System.out.println("maxIndex: " + maxIndex);
	}
}
max maxIndex i i<datas.length max<datas[i]
8 0 1 T F
8 0 2 T F
8 0 3 T T
9 3 4 T F
9 3 5 F  

 

max 9

maxIndex 3

 

 

 

문제4

 

 

package testReview;

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

		ArrayList datas = new ArrayList();

		for (int i = 1; i <= 5; i++) {
			datas.add(i); // [1,2,3,4,5]
		}
		int total = 0;
		for (int v : datas) {
			total += v;
		}
		System.out.println("total: " + total); // total: 15 11 }
	}
}

 

 

문제가 생기는 라인 :

ArrayList의 요소가 실제로 Object 타입으로 처리되고 있으며, 이는 제네릭을 사용하여 타입을 명시하지 않았기에 7번째 줄에서 오류가 난다.

 

코드에 에러를 제거하기 위한 방법:

 

ArrayList<Integer> datas = new ArrayList<Integer>(); 로 수정한다.

 

 

문제 5

 

package testReview;

import java.util.ArrayList;

public class Problem5 {

	public static void main(String[] args) {
		ArrayList<Integer> datas = new ArrayList<Integer>();
		datas.add(-5);
		datas.add(-1);
		datas.add(0);
		datas.add(1);
		datas.add(5);
		System.out.println(datas);
		for (int i = -1; i < 5; i++) {
			try {
				System.out.println(10 / datas.get(i));
			} catch (Exception e) {
				if (i < 0) {
					System.out.println("HELLO");

				} else {
					System.out.println("JAVA");
				}
			}
		}
	}
}

 

i가 -1 이기때문에 -1 0 1 2 3 4 총 6번 반복됩니다.

i 가 -1 일때는  catch로 가져서 HELLO 가 출력 되고 나머지는 나눗셈 결과가 나오고 0일때는 catch로 가고 i가 0보다 작지 않기 때문에 JAVA 가 출력됩니다.

'JAVA > 문제풀이' 카테고리의 다른 글

당근마켓 웹 크롤링  (0) 2023.12.25
능단평 오답노트(2)  (1) 2023.12.22
DTO,DAO 를 이용한 자판기 프로그램  (1) 2023.12.14
MVC 이용한 학생부 프로그램 1단계  (1) 2023.12.11
도서 판매 프로그램(검색)  (0) 2023.12.10