티스토리 뷰

반응형

 

 

지난 포스팅의 Day 4 강좌 리뷰에 이어

이번 포스팅에서는 Day 4 자바 코드 리뷰를 진행해보자.

 

Day 4 결과

 


오늘도 문제자체는 딱히 어려움은 없었고

기본 코드에서 조건을 잘 설정해주어서

 

강의 소스 코드를 보며 문제를 푸니 딱히 어려움은 없었다.

 

 

바로 코드를 보자.

 

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

public class Person {
    private int age;

    public Person(int initialAge) {
        // Add some more code to run some checks on initialAge
        if(initialAge > 0) {
            age = initialAge;
        } else {
            System.out.println("Age is not valid, setting age to 0.");
            age = 0;
        }
    }

    public void amIOld() {
        // Write code determining if this person's age is old and print the correct statement:
        if(age < 13) {
            System.out.print("You are young.");
        } else if(age >=13 && age < 18) {
            System.out.print("You are a teenager.");
        } else {
            System.out.print("You are old.");
        }
        System.out.println(/*Insert correct print statement here*/);
    }

    public void yearPasses() {
        // Increment this person's age.
        age++;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int age = sc.nextInt();
            Person p = new Person(age);
            p.amIOld();
            for (int j = 0; j < 3; j++) {
                p.yearPasses();
            }
            p.amIOld();
            System.out.println();
        }
        sc.close();
    }
}

 

우리가 작성해야 하는 부분은

  public Person(int initialAge) 메서드와   

public void amIOld() 메서드의

if문 조건만 작성해주면 되는 문제였다.

 

 

문제가 헷갈린다면 번역기를 돌려

해석하고

코드 작성을 못하겠다면 tutorial 강의를 보거나

필자의 이전 포스팅(글자 클릭시 이동)을

참고하면서 문제 풀면 될 것이다.

 

 


그럼 오늘도 열공하자. 😊

반응형