Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please follow all the instructions! The program PowerDemo.java is provided and demonstrates two approaches to computer values raised to a power (exponentiation). Note that it

Please follow all the instructions! The program PowerDemo.java is provided and demonstrates two approaches to computer values raised to a power (exponentiation). Note that it requires the file EasyReader.java that is also included. Answer or do the following:

a. What is the big-O time complexity of the power method in the sample code if N is the exponent? Briefly explain your answer.

b. What is the big-O time complexity of the pow method in the sample code if N is the exponent? Briefly explain your answer.

c. Write a Java program that uses the recursive algorithm and solution from Self-test exercise 13 (provided below along with the original problem). Provide sample output in your report.

d. What is the big-O time complexity of the recursive method in (c) if N is the exponent? Briefly explain your answer. Hint: How many multiplication operations occur?

PowerDemo.java:

import java.io.IOException; //import edu.colorado.io.EasyReader ; public class PowerDemo { public static void main(String[ ] args) { EasyReader stdin = new EasyReader(System.in); int n; double x; System.out.println ("I can calculate the value of x raised to an integer power."); do { x = stdin.doubleQuery("The value of x: "); n = stdin.intQuery("The integer power: "); System.out.println("Answer using the power method: " + power(x,n)); System.out.println("Answer using the pow method: " + pow(x,n)); } while (stdin.query("Do you want to try other values?")); System.out.println("Thank you beary much."); } public static double power(double x, int n) { double product; // The product of x with itself n times int count; if (x == 0 && n <= 0) throw new IllegalArgumentException("x is zero and n=" + n); if (n >= 0) { product = 1; for (count = 1; count <= n; count++) product = product * x; return product; } else return 1/power(x, -n); } public static double pow(double x, int n) { if (x == 0 && n <= 0) throw new IllegalArgumentException("x is zero and n=" + n); else if (n == 0) return 1; else if (n > 0) return x * pow(x, n-1); else // x is nonzero, and n is negative. return 1/pow(x, -n); } } 

EasyReader.java:

import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.PushbackReader; public class EasyReader extends PushbackReader { final static int EOF_VALUE = -1; // Returned by read( ) at EOF final static char ZERO_CHAR = '

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions