Cohe

6. 프로그래밍 언어 활용 - JAVA 언어 편 (1) 본문

자격증 공부/정보처리기사 실기

6. 프로그래밍 언어 활용 - JAVA 언어 편 (1)

코헤0121 2024. 9. 20. 20:25
728x90

출력 결과

class Parent{
    public int compute(int num){
        if(num<=1) return num;
        return compute(num-1) + compute(num-2);
    }
}

class Child extends Parent{
    public int compute(int num){
        if(num<=1) return num;
        return compute(num-1) + compute(num-3);
    }
}

class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        Parent obj = new Child();
        System.out.print(obj.compute(4));
    }
}

순서

단계 메서드 호출 반환 값 설명
1 main() - 프로그램 시작
2 System.out.println() - "Hello world!" 출력
3 new Child() - Child 객체 생성
4 obj.compute(4) - Child 클래스의 compute 메서드 호출 시작
5 compute(4) 3 num > 1이므로 재귀 호출
6 compute(3) 2 num > 1이므로 재귀 호출
7 compute(2) 1 num > 1이므로 재귀 호출
8 compute(1) 1 num <= 1이므로 1 반환
9 compute(-1) -1 num <= 1이므로 -1 반환
10 compute(2) 완료 1 1 + (-1) = 0, 하지만 int로 인해 1로 반올림
11 compute(0) 0 num <= 1이므로 0 반환
12 compute(3) 완료 2 1 + 0 = 1, 하지만 int로 인해 2로 반올림
13 compute(1) 1 num <= 1이므로 1 반환
14 compute(4) 완료 3 2 + 1 = 3
15 System.out.print() - 3 출력
16 main() 완료 - 프로그램 종료

때문에 출력 값은 3이 된다

출력 결과

class Main {
    public static void main(String[] args) {
        int i, j;
        for(j=0, i=0;i<=5; i++){
            j+=i;
            System.out.print(i);    
            if(i==5){
                System.out.print("=");    
                System.out.print(j);    
            }
            else{
                System.out.print("+");    
            }
        }
    }
}

순서

단계 i 값 j 값 출력 설명
1 0 0 0+ 초기 상태, i=0 출력 후 + 출력
2 1 1 0+1+ j에 i(1)를 더함, i=1 출력 후 + 출력
3 2 3 0+1+2+ j에 i(2)를 더함, i=2 출력 후 + 출력
4 3 6 0+1+2+3+ j에 i(3)를 더함, i=3 출력 후 + 출력
5 4 10 0+1+2+3+4+ j에 i(4)를 더함, i=4 출력 후 + 출력
6 5 15 0+1+2+3+4+5=15 j에 i(5)를 더함, i=5 출력 후 = 출력, 그 다음 j(15) 출력
7 6 - - i가 5를 초과하여 반복문 종료

따라서 값은 ' 0+1+2+3+4+5=15 ' 가 된다

실행 결과

class Main {
    public static void main(String[] args) {
        int []a = new int[8];
        int i =0;
        int n =11;
        while (n>0) {
            a[i++] = n%2;
            n /= 2;
        }
        for(i=7;i>=0;i--){
            System.out.print(a[i]);
        }
    }
}

순서

단계 n 값 i 값 a 배열 상태 설명
1 11 0 [0,0,0,0,0,0,0,0] 초기 상태
2 11 1 [1,0,0,0,0,0,0,0] 11 % 2 = 1, a[0] = 1
3 5 2 [1,1,0,0,0,0,0,0] 5 % 2 = 1, a[1] = 1
4 2 3 [1,1,0,0,0,0,0,0] 2 % 2 = 0, a[2] = 0
5 1 4 [1,1,0,1,0,0,0,0] 1 % 2 = 1, a[3] = 1
6 0 - [1,1,0,1,0,0,0,0] n = 0, while 루프 종료

출력 과정:

i 값 출력 누적 출력
7 0 0
6 0 00
5 0 000
4 0 0000
3 1 00001
2 0 000010
1 1 0000101
0 1 00001011

 

최종 출력: 00001011

실행결과를 쓰시오

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static void main(String[] args) {
        int [][]arr = new int[3][3];
        init(arr);
        hourGlass(arr);
        arrayPrint(arr);
    }

    public static void init(int arr[][]){
        for(int i=0; i<arr.length;i++){
            for(int j=0;j<arr[0].length;j++){
                arr[i][j]=0;
            }
        }
    }

    public static void hourGlass(int arr[][]){
        int v = 0;
        for(int i=0; i<arr.length; i++){
            for(int j=i; j<arr[0].length-i; j++){
                arr[i][j]=++v;
            }
        }        
    }

    public static void arrayPrint(int arr[][]){
        for(int i=0; i<arr.length; i++){
            for(int j=0; j<arr[0].length; j++){
                if(arr[i][j]==0){
                    System.out.print("  ");    
                } else{
                    System.out.print(arr[i][j] + " ");
                }
            }
            System.out.println();
        }
    }
}

순서

단계 메소드 배열 상태 설명
1 main int[][] arr = new int[3][3] 3x3 크기의 2차원 배열 생성
2 init [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 모든 요소를 0으로 초기화
3 hourGlass [[1, 2, 3], [0, 0, 0], [0, 0, 0]] i=0: 첫 번째 행 채우기
4 hourGlass [[1, 2, 3], [0, 4, 0], [0, 0, 0]] i=1: 두 번째 행의 중앙 채우기
5 hourGlass [[1, 2, 3], [0, 4, 0], [0, 0, 5]] i=2: 세 번째 행의 마지막 열 채우기
6 arrayPrint 출력: 1 2 3 4 5 배열 출력 (0은 공백으로 처리)

실행 과정 상세 설명:

  1. main 메소드:
    • 3x3 크기의 2차원 배열 arr 생성
    • init, hourGlass, arrayPrint 메소드 순차적으로 호출
  2. init 메소드:
    • 이중 for 루프를 사용하여 arr 배열의 모든 요소를 0으로 초기화

3-5. hourGlass 메소드:

  • 외부 for 루프 (i=0 to 2):
    • i=0: j=0 to 2까지 반복하며 첫 번째 행 채움 (값: 1, 2, 3)
    • i=1: j=1일 때만 중앙 요소 채움 (값: 4)
    • i=2: j=2일 때만 마지막 열 채움 (값: 5)
  1. arrayPrint 메소드:
    • 이중 for 루프로 배열 순회
    • 값이 0인 경우 공백 출력, 그 외의 경우 해당 숫자 출력
    • 각 행 출력 후 줄바꿈

최종 출력 결과는 모래시계 모양의 숫자 패턴을 보여줍니다.

 

 

나중에 풀 것들

정보처리기사 실기 프로그래밍 문제 java 기출 풀이 모음

정보처리기사 실기 java 언어 코딩 문제