Q1

홍길동 씨의 과목별 점수는 다음과 같다. 홍길동 씨의 평균 점수를 구해 보자.

과목점수

국어 80
영어 75
수학 55

 

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

        int KR = 80, EN = 75, MATH = 55;
        int AVG = (KR+EN+MATH) / 3;
        System.out.println(AVG);
        }
    }

 

public class Sample {
    public static void main(String[] args) {
        int a = 80;
        int b = 75;
        int c = 55;
        System.out.println((a + b + c) / 3); // 70 출력
    }
}

Q2

자연수 13이 홀수인지 짝수인지 판별할 수 있는 방법에 대해 말해 보자.

 

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

        int x = 13;
        if (x % 2 == 0) {
            System.out.println("13은 짝수");
        } else {
            System.out.println("13은 홀수");
        }
    }

}

 

public class Sample {
    public static void main(String[] args) {
        System.out.println(1 % 2); // 1 출력
        System.out.println(2 % 2); // 0 출력
        System.out.println(3 % 2); // 1 출력
        System.out.println(4 % 2); // 0 출력
    }
}

Q3

홍길동 씨의 주민등록번호는 881120-1068234이다. 홍길동 씨의 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력해 보자.

※ 문자열의 substring을 사용해 보자.

 

public class Chapter_03 {
    public static void main(String[] args) {
    String RRN = "881120-1068234";
        System.out.println(RRN.substring(0,6));
        System.out.println(RRN.substring(7));
    }

}

 

public class Sample {
    public static void main(String[] args) {
        String pin = "881120-1068234";
        String yyyyMMdd = pin.substring(0, 6);
        String num = pin.substring(7);
        System.out.println(yyyyMMdd);  // 881120 출력
        System.out.println(num);  // 1068234 출력
    }
}

Q4

주민등록번호 뒷자리의 맨 첫 번째 숫자는 성별을 나타낸다. 주민등록번호에서 성별을 나타내는 숫자를 출력해 보자.

>>> pin = "881120-1068234"

※ 문자열 인덱싱을 사용해 보자.

 

public class Chapter_03 {
    public static void main(String[] args) {
    String pin = "881120-1068234";
        System.out.println(pin.substring(7,8));

    }

}

 

public class Sample {
    public static void main(String[] args) {
        String pin = "881120-1068234";
        System.out.println(pin.charAt(7));  // 1이면 남자, 2이면 여자
    }
}

 

Q5

다음과 같은 문자열 a:b:c:d가 있다. 문자열의 replace 함수를 사용하여 a#b#c#d로 바꿔서 출력해 보자.

>>> a = "a:b:c:d"

 

public class Chapter_03 {
    public static void main(String[] args) {
    String a = "a:b:c:d";
        System.out.println(a.replaceAll(":","#"));


    }

}

 

public class Sample {
    public static void main(String[] args) {
        String a = "a:b:c:d";
        String b = a.replaceAll(":", "#");
        System.out.println(b);  // a#b#c#d 출력
    }
}

Q6

다음과 같은 [1, 3, 5, 4, 2] 리스트를 [5, 4, 3, 2, 1]로 만들어 보자.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 3, 5, 4, 2));
        System.out.println(myList); // [1, 3, 5, 4, 2] 출력
    }
}

※ 리스트의 sort 메소드를 사용해 보자.

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Chapter_03 {
    public static void main(String[] args) {
        ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 3, 5, 4, 2));
        myList.sort(Comparator.reverseOrder()); // 내림차순으로 정렬
        System.out.println(myList); // [5, 4, 3, 2, 1] 출력

    }

}

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Sample {
    public static void main(String[] args) {
        ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 3, 5, 4, 2));
        myList.sort(Comparator.reverseOrder());  // 역순으로 정렬
        System.out.println(myList); // [5, 4, 3, 2, 1] 출력
    }
}

Q7

다음과 같은 ['Life', 'is', 'too', 'short'] 리스트를 "Life is too short" 문자열로 만들어 출력해 보자.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<>(Arrays.asList("Life", "is", "too", "short"));
        System.out.println(myList); // [Life, is, too, short] 출력
    }
}

※ 문자열의 join 메소드를 사용해 보자.

 

import java.util.ArrayList;
import java.util.Arrays;

public class Chapter_03 {
    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<>(Arrays.asList("Life", "is", "too", "short"));
        String result = String.join(" ", myList);
        System.out.println(result); // [Life, is, too, short] 출력
    }
}

 

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<>(Arrays.asList("Life", "is", "too", "short"));
        String result = String.join(" ", myList);
        System.out.println(result); // "Life is too short" 출력
    }
}

Q8

다음의 맵 grade에서 "B'에 해당되는 값을 추출해 보자. (추출하고 나면 grade에는 "B"에 해당하는 아이템이 사라져야 한다.)

import java.util.HashMap;

public class Sample {
    public static void main(String[] args) {
        HashMap<String, Integer> grade = new HashMap<>();
        grade.put("A", 90);
        grade.put("B", 80);
        grade.put("C", 70);
    }
}

※ 맵의 remove 메소드를 사용해 보자.

 

import java.util.HashMap;

public class Chapter_03 {
    public static void main(String[] args) {
        HashMap<String, Integer> grade = new HashMap<>();
        grade.put("A", 90);
        grade.put("B", 80);
        grade.put("C", 70);
        System.out.println(grade.remove("B"));
    }
}

 

import java.util.HashMap;

public class Sample {
    public static void main(String[] args) {
        HashMap<String, Integer> grade = new HashMap<>();
        grade.put("A", 90);
        grade.put("B", 80);
        grade.put("C", 70);
        int result = grade.remove("B");
        System.out.println(result);  // 80 출력
        System.out.println(grade);  // {A=90, C=70} 출력
    }
}

Q9

다음의 numbers 리스트에서 중복 숫자를 제거해 보자.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5));
        System.out.println(numbers);  // [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5] 출력
    }
}

※ 집합 자료형의 요솟값이 중복될 수 없다는 특징을 사용해 보자.

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class Chapter_03 {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5));
        HashSet<Integer> numb = new HashSet(numbers);
        ArrayList<Integer> result = new ArrayList<>(numb);
        System.out.println(result);  // [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5] 출력


    }
}

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class Sample {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5));
        HashSet<Integer> temp = new HashSet(numbers);  // List를 Set으로 변경
        ArrayList<Integer> result = new ArrayList<>(temp);  // Set을 다시 List로 변경
        System.out.println(result);  // [1, 2, 3, 4, 5] 출력
    }
}

Q10

다음은 커피의 종류(1: 아메리카노, 2:아이스 아메리카노, 3:카페라떼)를 입력하면 커피의 가격을 알려주는 프로그램이다.

import java.util.HashMap;

public class Sample {
    static void printCoffeePrice(int type) {
        HashMap<Integer, Integer> priceMap = new HashMap<>();
        priceMap.put(1, 3000);  // 1: 아메리카노
        priceMap.put(2, 4000);  // 2: 아이스 아메리카노
        priceMap.put(3, 5000);  // 3: 카페라떼
        int price = priceMap.get(type);
        System.out.println(String.format("가격은 %d원 입니다.", price));
    }

    public static void main(String[] args) {
        printCoffeePrice(1);  // "가격은 3000원 입니다." 출력
        printCoffeePrice(99);  // NullPointerException 발생
    }
}

위 메소드에서 1, 2, 3과 같은 매직넘버(상수로 선언하지 않은 숫자)를 제거하여 프로그램을 개선해보자.

※ Enum을 사용해 보자.

import java.util.HashMap;

public class Chapter_03 {
    enum CoffeeType {
        Americano,
        Iceamericano,
        Cafelatte
    };

    public static void CoffeePrice(CoffeeType type) {
        HashMap<CoffeeType, Integer> priceMap = new HashMap<>();
        priceMap.put(CoffeeType.Americano, 3000);  // 1: 아메리카노
        priceMap.put(CoffeeType.Iceamericano, 4000);  // 2: 아이스 아메리카노
        priceMap.put(CoffeeType.Cafelatte, 5000);  // 3: 카페라떼
        int price = priceMap.get(type);
        System.out.println(String.format("가격은 %d원 입니다.", price));
    }

    public static void main(String[] args) {
        CoffeePrice(CoffeeType.Americano);  // "가격은 3000원 입니다." 출력
    }
}

+ Recent posts