Created
November 24, 2022 17:23
-
-
Save alwaisy/1c78719ac528abf1de93bea2cd9f36c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Objective | |
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. | |
Task | |
Write a Calculator class with a single method: int power(int,int). The power method takes two integers, and , as parameters and returns the integer result of . If either or 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, , the number of test cases. Each of the subsequent lines describes a test case in space-separated integers that denote and , respectively. | |
Constraints | |
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 lines of output, where each line contains the result of as calculated by your Calculator class' power method. | |
Sample Input | |
4 | |
3 5 | |
2 4 | |
-1 -2 | |
-1 3 | |
Sample Output | |
243 | |
16 | |
n and p should be non-negative | |
n and p should be non-negative | |
Explanation | |
: and are positive, so power returns the result of , which is . | |
: and are positive, so power returns the result of =, which is . | |
: Both inputs ( and ) are negative, so power throws an exception and is printed. | |
: One of the inputs () is negative, so power throws an exception and is printed. | |
*/ | |
// solution - tests 0 to 1 passed | |
process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) { | |
input_stdin += data; | |
}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
main(); | |
}); | |
function readLine() { | |
return input_stdin_array[input_currentline++]; | |
} | |
//Write your code here | |
class Calculator { | |
power(n, p) { | |
try { | |
return n < 0 || p < 0 ? 'n and p should be non-negative' : n**p | |
} catch { | |
return 'n and p should be non-negative' | |
} | |
} | |
} | |
function main(){ | |
var myCalculator=new Calculator(); | |
var T=parseInt(readLine()); | |
while(T-->0){ | |
var num = (readLine().split(" ")); | |
try{ | |
var n=parseInt(num[0]); | |
var p=parseInt(num[1]); | |
var ans=myCalculator.power(n,p); | |
console.log(ans); | |
} | |
catch(e){ | |
console.log(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment