a method called printPowersOfN that accepts a base and a maximum exponent as arguments. The method prints each power of the base from 0 up to that maximum exponent, inclusive.   For example: printPowersOfN(4, 3);    // should output 1 4 16 64 This calculates 40, 41, 42, and 43.   printPowersOfN(5, 6);    // should output 1 5 25 125 625 3125 15625 This calculates 50, 51, 52, 53, 54, 55, and 56. printPowersOfN(–2, 8);   // should output 1 –2 4 –8 16 -32 64 –128 256   Both arguments are integers. You may assume that the maximum exponent passed to printPowersOfN has a value of 0 or greater.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a method called printPowersOfN that accepts a base and a maximum exponent as arguments. The method prints each power of the base from 0 up to that maximum exponent, inclusive.

 

For example:

printPowersOfN(4, 3);    // should output 1 4 16 64

This calculates 40, 41, 42, and 43.

 

printPowersOfN(5, 6);    // should output 1 5 25 125 625 3125 15625
This calculates 50, 51, 52, 53, 54, 55, and 56.

printPowersOfN(–2, 8);   // should output 1 –2 4 –8 16 -32 64 –128 256

 

Both arguments are integers. You may assume that the maximum exponent passed to printPowersOfN has a value of 0 or greater.

 

The Math class may help you with this problem! If you use it, you may need to cast its results from double to int so that you don’t see a .0 after each number in your output. You may also write this program without using the Math class. Either solution is acceptable so long as it displays the correct output.

 

Please use the three sample calls in your submitted program run. You are welcome to include additional calls, with other values, after these three. Your program must work for other valid integers besides those listed in the sample calls.

 

Rubric:

  • Student name and date is in a comment on the first line of the program: -5 points if fails
  • The three sample calls are included in program output: -3 points if fails
  • printPowersOfN defined with appropriate argument data types: 3 points
  • printPowersOfN loop: 2 points
  • printPowersOfN calculations: 2 points
  • Program structure, program works correctly: 3 point

*please follow rubric

**Description:**

This educational content explains how to implement a method called `printPowersOfN` in a programming context. The method takes two integer arguments: a base and a maximum exponent. It prints all powers of the base from 0 up to the specified maximum exponent.

**Example Implementations:**

1. `printPowersOfN(4, 3);`
   - **Expected Output:** `1 4 16 64`
   - **Explanation:** This calculates \(4^0, 4^1, 4^2,\) and \(4^3\).

2. `printPowersOfN(5, 6);`
   - **Expected Output:** `1 5 25 125 625 3125 15625`
   - **Explanation:** This calculates \(5^0, 5^1, 5^2, 5^3, 5^4, 5^5,\) and \(5^6\).

3. `printPowersOfN(-2, 8);`
   - **Expected Output:** `1 -2 4 -8 16 -32 64 -128 256`
   - **Explanation:** This calculates \((-2)^0, (-2)^1, (-2)^2, ..., (-2)^8\).

**Constraints:**

- Both arguments, base and maximum exponent, must be integers.
- The maximum exponent should be zero or a positive integer.

**Technical Considerations:**

- Utilizing the Math class can aid in calculations, especially for handling powers.
- Care must be taken to convert results from double to integer if using a method that results in decimals.
- An alternative implementation without the Math class is acceptable if it produces accurate output.

**Programming Instructions:**

- Include the three sample method calls in your submitted program.
- Additional test calls with different values are permitted, but ensure the program supports any valid integer inputs.

**Rubric:**

- **Commenting:** Student name and date should be present as a comment on the program's first line. Missing this will result in a deduction of 5 points.
- **Sample Calls:** The inclusion of the three sample calls in the program is mandatory. Failure to do so will incur a 3-point penalty.
- **Method Definition:** Correctly defining `printPowersOfN` with appropriate data types earns 3 points.
- **Loop Implementation:** Proper use
Transcribed Image Text:**Description:** This educational content explains how to implement a method called `printPowersOfN` in a programming context. The method takes two integer arguments: a base and a maximum exponent. It prints all powers of the base from 0 up to the specified maximum exponent. **Example Implementations:** 1. `printPowersOfN(4, 3);` - **Expected Output:** `1 4 16 64` - **Explanation:** This calculates \(4^0, 4^1, 4^2,\) and \(4^3\). 2. `printPowersOfN(5, 6);` - **Expected Output:** `1 5 25 125 625 3125 15625` - **Explanation:** This calculates \(5^0, 5^1, 5^2, 5^3, 5^4, 5^5,\) and \(5^6\). 3. `printPowersOfN(-2, 8);` - **Expected Output:** `1 -2 4 -8 16 -32 64 -128 256` - **Explanation:** This calculates \((-2)^0, (-2)^1, (-2)^2, ..., (-2)^8\). **Constraints:** - Both arguments, base and maximum exponent, must be integers. - The maximum exponent should be zero or a positive integer. **Technical Considerations:** - Utilizing the Math class can aid in calculations, especially for handling powers. - Care must be taken to convert results from double to integer if using a method that results in decimals. - An alternative implementation without the Math class is acceptable if it produces accurate output. **Programming Instructions:** - Include the three sample method calls in your submitted program. - Additional test calls with different values are permitted, but ensure the program supports any valid integer inputs. **Rubric:** - **Commenting:** Student name and date should be present as a comment on the program's first line. Missing this will result in a deduction of 5 points. - **Sample Calls:** The inclusion of the three sample calls in the program is mandatory. Failure to do so will incur a 3-point penalty. - **Method Definition:** Correctly defining `printPowersOfN` with appropriate data types earns 3 points. - **Loop Implementation:** Proper use
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education