#9 Java Static Initializer Block

HackerRank/HR-Java 2015. 11. 23. 11:25 by 후뤼한잉여

https://www.hackerrank.com/challenges/java-static-initializer-block


Problem Statement

Static initialization blocks are executed when the class is loaded, and you can initialize static variables in those blocks.

It's time to test your knowledge of Static initialization blocks. You can read about it here.

You are given a class Solution with a main method. Complete the given code so that it outputs the area of a parallelogram with breadth B and height H. You should read the variables from the standard input.

If B0 or H 0, the output should be "java.lang.Exception: Breadth and height must be positive" without quotes.

Input Format

There are two lines of input. The first line contains B: the breadth of the parallelogram. The next line contains H: the height of the parallelogram.

Problem Statement

Static initialization blocks are executed when the class is loaded, and you can initialize static variables in those blocks.

It's time to test your knowledge of Static initialization blocks. You can read about it here.

You are given a class Solution with a main method. Complete the given code so that it outputs the area of a parallelogram with breadth B and height H. You should read the variables from the standard input.

If B0 or H 0, the output should be "java.lang.Exception: Breadth and height must be positive" without quotes.

Input Format

There are two lines of input. The first line contains B: the breadth of the parallelogram. The next line contains H: the height of the parallelogram.

Constraints 
100B100 
100H100

Output Format

If both values are greater than zero, then the main method must output the area of theparallelogram. Otherwise, print "java.lang.Exception: Breadth and height must be positive"without quotes.

Sample input 1

1
3

Sample output 1

3

Sample input 2

-1
2

Sample output 2

java.lang.Exception: Breadth and height must be positive

Copyright © 2015 HackerRank.

static 키워드에 대한 활용을 하는지에 대한 질문으로 Exception에 대해서 Exception으로 처리해야하는지 애를 먹었지만 알고보니 단순 프린트 하라는 거였음. B와 H값은 -100 ~ 100 사이의 정수로 제한된다. 영어 무식자라 처음엔 저 제약(Constraints)하는 부분이 개발자가 처리해야하는 거라 생각 했는데 문제를 풀다보니 테스트 케이스 입력 값을 저 제한된 값으로 보내주겠다는 의미인거 같다.

그래서 그 부분은 신경 안쓰고 에러 메세지에 따라 음수일 경우에만 에러를 출력하도록 했다.

이번 문제는 중간에 부분만 작성하도록 되어있어 그부분만 작성한다.


보낸 답)

    static private int B = 0;

    static private int H = 0;

    static private boolean flag = false;

    static {

        Scanner sc = new Scanner(System.in);

        B = sc.nextInt();

        H = sc.nextInt();


        if( (B <= 0) || (H <= 0)) {

            flag = false;

            System.out.println("java.lang.Exception: Breadth and height must be positive");

        } else {

            flag = true;

        }

    }


'HackerRank > HR-Java' 카테고리의 다른 글

#8 Java End-of-file  (0) 2015.11.23
#6 Java Loops  (0) 2015.11.18
#5 Java Output Formatting  (0) 2015.11.17
#4 Java Stdin and Stdout 2  (0) 2015.11.17
#3 Java If-Else  (0) 2015.11.17
Nav