Hackerrank “30 Days of Code” Çözümleri – Day 17: More Exceptions

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 17: More Exceptions” sorusunun açıklaması ve çözümü. Bu soruda exception kavramına göz atmaya devam ettik.

► Hackerrank 30 Days of Code Çözümleri – Day 17: More Exceptions: https://www.hackerrank.com/challenges/30-more-exceptions/problem

► Problem açıklaması:


Yesterday’s challenge taught you to manage exceptional situations by using try and catch blocks. In today’s challenge, you will practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video.


Write a Calculator class with a single method: int power(int,int). The power method takes two integers, n and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative.

Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.

Input Format

Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p, respectively.


No Test Case will result in overflow for correctly written code.

Output Format

Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of n^p as calculated by your Calculator class’ power method.

Sample Input


3 5

2 4

-1 -2

-1 3

Sample Output



n and p should be non-negative

n and p should be non-negative

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.