Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
Expert Solution & Answer
Book Icon
Chapter 5, Problem 9R

Explanation of Solution

Non-recursive method for power:

The non-recursive method for power is given below:

Create the method power() that accepts the input parameter as “num” and “pow” to calculate and return the power of number.

//Define the power() method

public static double power(double num, int pow)

{

  //Declare and initialize the variables

  int n = 0;

  double ans = 1;

  //Loop executes until the power value

  while ((1 << n) <= pow)

  //Increment the "n" by "1"

  n++;

  //Loop executes until the power value

  for (int i= n-1; i >= 0; --i)

  {

  //Multiply "ans" with "ans"

  ans *= ans;

  //Check bitwise "i" and "pow" is greater than "0"

...

Blurred answer
Students have asked these similar questions
Implement a Java method with the identifier “SquareRoot” that takes 1parameter of type integer and returns a double value that represents the squareroot of the parameter. Note: you must implement the square root functionyourself by using the bisection algorithm as explained in Appendix 1 and youshould NOT use any java.lang.Math function.
Using java language In voltTest[10] is an encapsulated array that contains 10 voltages. Display how many are ACTIVE (5V), SATURATED (greater than 5V) and CUTOFF (less than 5V). Use the UML diagram as a reference. setValue(int,int,int) – accepts the value of voltActive, voltSaturated and voltCutoff as a parameter, which is the number of active, saturated and cutoff voltages. displayResult() – displays the number of active, saturated and cutoff voltages.
Implement a Java method with the identifier “CubeRoot” that takes 1 parameterof type integer and returns a double value that represents the cube root of theparameter. Note: you must implement the cube root function yourself by usingthe bisection algorithm as explained in Appendix 1 and you should NOT use anyjava.lang.Math function.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education