Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of base exponent (2 pts). For example, integerPower(3, 4) returns 81.

LAB5

#1. Method and loop

Write a method integerPower(base, exponent) that returns the value of baseexponent (2 pts).

For example, integerPower(3, 4) returns 81. Assume thatexponent is a positive nonzero integer, and base is an integer. Method integer should use for or while loop to control the calculation. Do not use any Math library methods. Incorporate this method into a program class and invoke this method with different combinations of input values at least 4 times.

Please use printf() method to generate the multiple outputs from the sequence of the method invocations for integerPower(num1, num2). Remark: The placeholder for boolean value is %b.

System.out.printf("integerPower(%d, %d) returns %b.", 3, 4, integerPower(3, 4));

#2. Method, loop, and random numbers

For each of the following sets of integers, write five statements that will print a number at random from the set (2.5 pts).

a) 2, 4, 6, 8, 10

b) 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

c) -10, -9, -8, -7, .., 0, 1, 2, ..., 9, 10

d) 3, 5, 7, 9, 11, 13

e) 6, 10, 14, 18, 22, 26

Incorporate the statements into the main() method. Please use println or printf statement to generate the multiple outputs from the sequence of the statements.

#3. Method, loop, Scanner tool, and random number

An integer is said to be prime if it is divisible only by 1 and iteself. For example, 2, 3, 5, and 7 are prime, but 4, 6, 8, and 9 are not. Please give the solution to the following sub-questions (3 pts).

a) Write a method, isPrime(), that determines if a number is prime.

b) Use this method in the main() that determines whether user input is prime or not. The main() includes a while loop which continuously reads user input until user types-999 as the sentinel value and checks whether the input value is prime. Within the while loop, please print the input value with the result from the isPrime() method.

c) Unlike the above case, please use Random class to generate random numbers and use 0 as the exit condition of the while loop.

#4. Method, loop, Scanner tool, and random number

Write a method that takes an integer value and prints the number with its digits reversed. For example, given the number 7631, the method prints 1367 (2.5 pts).

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions