Day 17: More Exceptions In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 17: More Exceptions in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use โ€œCtrl+Fโ€ To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A โ€œFindโ€ Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 17: More Exceptions โ€“ Hacker Rank Solution

Day 17: More Exceptions โ€“ Hacker Rank Solution

Problem:

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.

Day 17: More Exceptions โ€“ Hacker Rank Solution
import java.util.Scanner;

/**
 * @author Aditi Kumari
 *
 */
class Calculator {
	int power(int n, int p) throws Exception {

		if (p < 0 || n < 0)
			throw new Exception("n and p should be non-negative");
		return (int) Math.pow(n, p);
	}
}

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

		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while (t-- > 0) {

			int n = in.nextInt();
			int p = in.nextInt();
			Calculator myCalculator = new Calculator();
			try {
				int ans = myCalculator.power(n, p);
				System.out.println(ans);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
		in.close();
	}
}

321 thoughts on “Day 17: More Exceptions In Java | 30 Days Of Code | Hackerrank Programming Solutions”

  1. Sight Care is a daily supplement proven in clinical trials and conclusive science to improve vision by nourishing the body from within. The Sight Care formula claims to reverse issues in eyesight, and every ingredient is completely natural.

    Reply
  2. Cortexi is a completely natural product that promotes healthy hearing, improves memory, and sharpens mental clarity. Cortexi hearing support formula is a combination of high-quality natural components that work together to offer you with a variety of health advantages, particularly for persons in their middle and late years. Cortex not only improves hearing but also decreases inflammation, eliminates brain fog, and gives natural memory protection.

    Reply
  3. Boostaro increases blood flow to the reproductive organs, leading to stronger and more vibrant erections. It provides a powerful boost that can make you feel like you’ve unlocked the secret to firm erections

    Reply
  4. Puravive introduced an innovative approach to weight loss and management that set it apart from other supplements. It enhances the production and storage of brown fat in the body, a stark contrast to the unhealthy white fat that contributes to obesity.

    Reply
  5. EyeFortin is a natural vision support formula crafted with a blend of plant-based compounds and essential minerals. It aims to enhance vision clarity, focus, and moisture balance.

    Reply
  6. The Quietum Plus supplement promotes healthy ears, enables clearer hearing, and combats tinnitus by utilizing only the purest natural ingredients. Supplements are widely used for various reasons, including boosting energy, lowering blood pressure, and boosting metabolism.

    Reply
  7. Kerassentials are natural skin care products with ingredients such as vitamins and plants that help support good health and prevent the appearance of aging skin. Theyโ€™re also 100% natural and safe to use. The manufacturer states that the product has no negative side effects and is safe to take on a daily basis. Kerassentials is a convenient, easy-to-use formula.

    Reply
  8. Gorilla Flow is a non-toxic supplement that was developed by experts to boost prostate health for men. It’s a blend of all-natural nutrients, including Pumpkin Seed Extract Stinging Nettle Extract, Gorilla Cherry and Saw Palmetto, Boron, and Lycopene.

    Reply
  9. Cortexi is an effective hearing health support formula that has gained positive user feedback for its ability to improve hearing ability and memory. This supplement contains natural ingredients and has undergone evaluation to ensure its efficacy and safety. Manufactured in an FDA-registered and GMP-certified facility, Cortexi promotes healthy hearing, enhances mental acuity, and sharpens memory.

    Reply
  10. Sight Care is a daily supplement proven in clinical trials and conclusive science to improve vision by nourishing the body from within. The Sight Care formula claims to reverse issues in eyesight, and every ingredient is completely natural.

    Reply
  11. I’m impressed with your article! The insights are valuable, and I believe incorporating more images in your next articles could make them even more appealing to a broader audience.

    Reply
  12. Excellent piece! ๐Ÿ‘ The information is presented in a compelling manner. Adding more visuals in your future articles could make them even more enjoyable for readers. ๐Ÿ“ท

    Reply
  13. ๐ŸŒŒ Wow, blog ini seperti perjalanan kosmik meluncur ke alam semesta dari kegembiraan! ๐Ÿ’ซ Konten yang menegangkan di sini adalah perjalanan rollercoaster yang mendebarkan bagi pikiran, memicu ketertarikan setiap saat. ๐ŸŒŸ Baik itu inspirasi, blog ini adalah harta karun wawasan yang mendebarkan! #TerpukauPikiran Terjun ke dalam pengalaman menegangkan ini dari pengetahuan dan biarkan imajinasi Anda berkelana! โœจ Jangan hanya membaca, rasakan kegembiraan ini! #MelampauiBiasa ๐Ÿš€ akan bersyukur untuk perjalanan mendebarkan ini melalui ranah keajaiban yang tak berujung! ๐Ÿš€

    Reply
  14. lfchungary.com
    ๋œป๋ฐ–์—๋„ ๊ทธ๋…€์˜ ์˜ค๋น ๊ฐ€ ๊ถ์— ๋“ค์–ด๊ฐ€๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ €์šฐ๊ฐ€ ์ˆ˜์ƒํ•ด ๋ณด์˜€๋‹ค.

    Reply
  15. smcasino-game.com
    ์–ผ๊ตด์— “์ถฉ์„ฑ! “์ด๋ผ๋Š” ๋‘ ๋‹จ์–ด๊ฐ€ ์“ฐ์—ฌ์ง„ ๊ฒƒ์ฒ˜๋Ÿผ ๊ทธ์˜ ์–ผ๊ตด ํ‘œ์ •์€ ์ง„์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค.

    Reply
  16. lfchungary.com
    ์™œ๋ƒํ•˜๋ฉด … ๋…ธ์ธ์€ Wei Guogong์ด ๋ฐ˜๋ž€์„ ๊ณ„ํšํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ์—ˆ๊ณ  ๊ทธ๊ฒƒ์€ ํ—ˆ๊ตฌ์— ๋ถˆ๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค.

    Reply
  17. pragmatic-ko.com
    “๊ฒŒ์ŠคํŠธ ๋‹ด๋‹น์ž, ๋“ค์–ด๊ฐ€์„œ ๋ณด์„ธ์š”. Ben์€ ํ’ˆ์งˆ์ด ์ข‹๊ณ  ์ €๋ ดํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ชป ๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.”

    Reply
  18. crazyslot1.com
    ์ด ๋น„์˜ฅํ•˜์ง€ ์•Š์€ ๋•… ๋•Œ๋ฌธ์— ์ˆ˜๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ํ—ˆ๋ฆฌ๋ ์— ๋จธ๋ฆฌ๋ฅผ ๋ฌถ๊ฒŒ ๋†”๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?

    Reply

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker๐Ÿ™.