Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This exercise is intended to demonstrate recursive coding, the Java Math class, and exception handling. You are to write a Java program that has at

This exercise is intended to demonstrate recursive coding, the Java Math class, and exception handling.

You are to write a Java program that has at least two functions: main and recursionPower. main will be the entry point/driver and recursionPower will be a function that recursively computes the power of an integer parameter.

The maximum value that can be stored in a Java int is 2,147,483,647. As you compute powers for an integer, the values become very large very quickly and may exceed the maximum value. Oddly, in Java the basic arithmetic operations will simply overflow when that happens without any warning. A program in which that happens is allowed to continue executing with corrupted data.

However, there is a static Java class, Math, which since the advent of Java 1.8 offers a solution to that problem. (You may have already used Math with such functions as Math.abs(), Math.power(), and Math.sqrt()). The functions addExact(), subtractExact(), multiplyExact() duplicate the functions their names imply, and in addition they throw an exception if a result cause an overflow. For instance, instead of multiplying one variable by another with regular operators

a * b

you would use the Math function

Math.multiplyExact(a, b)

If the result of the multiplyExact operation is an overflow, it will throw an ArithmeticException exception.

Your program should recursively compute the powers for the range of integers 1 20 (int). You may hard-code this range. You should use the Math.multiplyExact() function for the power multiplications. If while you are computing a power and there is an overflow, handle the exception, discontinue that computation, and then proceed with the next integer in the range. You should print a clearly labeled, formatted report of your results.

To answer the question: I think its suppose to go through the powers of 2-15

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions