✎NHN Academy | JAVA

NHN Academy - 2024.08.21(Wed)

박순돌 2024. 8. 21. 16:22

Module 04   Statement과 Exception

Statement = 메모리의 상태를 변화(어떤 행동을 완결짓는 행위)

Exception = 단순히 값을 연산해서 그에 대한 결과를 나타내는 것

Statement의 묶음 = Block (Java에서 Block을 브레이스 { } 라고도 한다)

 

Java는 {} 와 ; 으로 컴파일러가 판단한다.(들여쓰기로 문단을 판단하지 않음!)

 

상위 블록의 변수 를 하위 블록의 변수로 가져와서 사용이 O

 

선택문 SELECTION(if , switch) / 반복문 ITERATION(while , do-while , for , for-each) / 분기문 JUMP( goto , break , continue)

break문이 없으면 탈출하지 못하고, 전부 수행하게 됨(결국 입력값이 뭐든지, 결과는 default값이 됨)

 

반복문*****완전 중요*****

boolean 값을 기준으로 내부 문을 수행

 

for(초기값; 종료조건; 증가/감소)        EX> for(;a > 0; ) // 이런 식으로 생략 가능

                                                          EX> for(;;) // 무한 루프

 

hasNext() = boolean 타입으로 반환(True / False)

next() = Iterator에 입력된 타입으로 반환(아무 타입으로 반환 O)

 

for-each : 값을 읽을 때만 사용

 

예외 처리 = 프로그래머가 예기치못한 예외의 발생에 미리 대처하는 코드를 작성하는 것

예외 = 사용자가 예측가능한 오류

 

try-catch문 : catch를 1개 이상 작성 가능 O

EX> try { ... } catch { ... } catch { ... }

EX> try { ... } catch { ... | ... }

 

데이터베이스 커넥션 풀   Database Connection Pool

= 데이터베이스와의 연결(Connection)을 미리 여러 개 생성해 두고, 필요할 때마다 이를 재사용하는 기술

     EX>

     try {

         DB.open();

                ...

 

         DB. close();

     } catch ( ... ) {

            ...

     }

 

Memory Lick = 메모리 누수 (이를 방지하기 위해 사용하는 try-with-resource 구분)

try (Scanner scanner = new Scanner(System.in) {
    int i = Integer.parseInt(scanner.nextLine());
    int j = Integer.parseInt(scanner.nextLine());
    int k = i / j;
}
catch (ArithmeticException e) {
    e.printStackTrace();
}
finally {
    scanner.close();
}

 

윤년의 조건       1. year % 4 == 0

                         2. year % 100 != 0

                         3. year % 400 == 0

 

Bubble Sort = 가장 느린 정렬 방법 / 가장 쉬운 방법

 

array[i] > array[j]   // 오름차순         |            array[i] < array[j]   // 내림차순

public class bubbleSort {
    public static void main(String[] args) {
        int[] array = {6, 3, 2, 4, 3, 1, 9, 10, 13};
        for(int i = 0; i < array.length; i++) {
            for(int j = i; j < array.length; j++) {
                if(array[i] < array[j]) {   // 오름차순 : > , 내림차순 : <
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " / ");
        }
    }
}

 


오른쪽 -> Lap 4-1

 

import java.util.Scanner;

public class Whatday {
    static int[] daysInMonth  = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static int[] daysInLeapMonth  = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static String[] monthNames = {"January", "Feburary", "March", "April",
                                  "May", "June", "July", "August", "Sepetember",
                                  "October", "November", "December"};

    public static void main(String[] args) {
        try {
            System.out.print("Enter year number: ");
            Scanner scanner = new Scanner(System.in);
            String line = scanner.nextLine();
            int yearNum = Integer.parseInt(line);

            boolean isLeapYear = (yearNum % 4 == 0) &&
                                 (yearNum % 100 != 0 || yearNum % 400 == 0);

            int maxDayNum = isLeapYear ? 366: 365;

            System.out.print("Enter a day Number between 1 and " + maxDayNum + ": ");
            line = scanner.nextLine();
            int dayNum = Integer.parseInt(line);

            if (dayNum < 1 || dayNum > maxDayNum) {
                throw new IllegalArgumentException("Out of date range");
            }

            int monthNum = 0;

            if (isLeapYear) {
                for (int days: daysInLeapMonth) {
                    if (dayNum <= days) {
                        break;
                    }
                    else {
                        dayNum -= days;
                        monthNum++;
                    }
                }
            }
            else {
                for (int days: daysInMonth) {
                    if (dayNum <= days) {
                        break;
                    }
                    else {
                        dayNum -= days;
                        monthNum++;
                    }
                }
            }

            String monthName = monthNames[monthNum];

            System.out.printf("%s, %d ", monthName, dayNum);
            scanner.close();
        }
        catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}

 

 

(0,0) ~ (9,9) 까지의 미로로 만들기 - 과제

 

'✎NHN Academy | JAVA' 카테고리의 다른 글

NHN Academy - 2024.08.26(Mon)  (0) 2024.08.26
NHN Academy - 2024.08.23(Fri)  (0) 2024.08.23
NHN Academy - 2024.08.22(Thu)  (0) 2024.08.22
NHN Academy - 2024.08.20(Tue)  (0) 2024.08.20
NHN Academy - 2024.08.19(Mon)  (0) 2024.08.19