Question

LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...

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).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans :)

import java.util.Random;

import java.util.Scanner;

public class Test9 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Test9 t=new Test9();

System.out.println(t.integerPower(0, 4));

//2. Random numbers

int set1[]={2, 4, 6, 8, 10 };

System.out.println("Set1 Randomly :\n");

for(int i=0;i<set1.length;i++){

System.out.print(set1[new Random().nextInt(set1.length)]+"\t");

}

int set2[]={10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

System.out.println("\nSet2 Randomly :\n");

for(int i=0;i<set2.length;i++){

System.out.print(set2[new Random().nextInt(set2.length)]+"\t");

}

int set3[]={-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

System.out.println("\nSet3 Randomly :\n");

for(int i=0;i<set3.length;i++){

System.out.print(set3[new Random().nextInt(set3.length)]+"\t");

}

int set4[]={3, 5, 7, 9, 11, 13 };

System.out.println("\nSet4 Randomly :\n");

for(int i=0;i<set4.length;i++){

System.out.print(set4[new Random().nextInt(set4.length)]+"\t");

}

int set5[]={6, 10, 14, 18, 22, 26 };

System.out.println("\nSet5 Randomly :\n");

for(int i=0;i<set5.length;i++){

System.out.print(set5[new Random().nextInt(set5.length)]+"\t");

}

System.out.println();

//3.(b) to find prime or not

while(true){

System.out.println("Enter a Positive Number and should be 1<:");

Scanner sc=new Scanner(System.in);

int inputNum=sc.nextInt();

if(inputNum==-999)

break;

if(inputNum<0)

System.out.println("The given number negetive :"+inputNum);

else if(inputNum==0)

System.out.println("The given number is zero :"+inputNum);

else if(inputNum==1)

System.out.println("The given number is one(not prime) :"+inputNum);

else

{

String str="is not prime";

if(t.isPrime(inputNum))

str="is prime";

System.out.println("The given number "+str+" :"+inputNum);

}

}

//3.(c) with random numbers to find prime or not

while(true){

// create instance of Random class

Random rand = new Random();

// Generate random integers in range 0 to 99

int rand_int = rand.nextInt(100);

  

if(rand_int==0){

break;

}

  

if(rand_int==1)

System.out.println("The random number is one(not prime) :"+rand_int);

else

{

String str="is not prime";

if(t.isPrime(rand_int))

str="is prime";

System.out.println("The given number "+str+" :"+rand_int);

}

}

System.out.println("Enter a positive Number for reverse:");

Scanner sc=new Scanner(System.in);

int inputNumber=sc.nextInt();

if(inputNumber>0){

System.out.println("The Reverse Number :"+t.reverse(inputNumber));

}

else{

System.out.println("The given number is negetive");

}

}

//1.integerPower()

private long integerPower(int base, int exponent){

long res=0;

if(exponent>=0){

for(int i=0;i<=exponent;i++){

if(i==0){

res=1;

}else{

res=res*base;

}

}

}else{

System.out.println("the exponent value is negative");

}

return res;

}

private boolean isPrime(int num){

boolean status=true;

int temp=0;

for(int i=2;i<=num/2;i++)

{

temp=num%i;

if(temp==0)

{

status=false;

break;

}

}

return status;

}

//4.reverse()

private int reverse(int inputNumber){

int res=0;

int temp=0;

while(inputNumber > 0)

{

temp = inputNumber % 10;

res = res * 10 + temp;

inputNumber = inputNumber / 10;

}

return res;

}

}

Add a comment
Know the answer?
Add Answer to:
LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Write a Simple Program This question is about following the directions below. Failure to do...

    Java Write a Simple Program This question is about following the directions below. Failure to do so, results in lost credit. Write the definition of a method that takes in an integer number, and returns true if the number is prime, otherwise, returns false. A prime number is only divisible by itself and 1. Do not write the code for main here. The code must follow these steps in the order shown: 1. Name the method isPrime, and make it...

  • Please use public class for java. Thank you. 1. (10 points) Write a method that computes...

    Please use public class for java. Thank you. 1. (10 points) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula futurelnvestmentValue numberOfYears 12 investmentAmount X ( monthlyInterestRate) Use the following method header: public static double futurelnvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue 10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • A) (C#) Write a function integerPower(base, exponent) that returns the value of                             &nbsp

    A) (C#) Write a function integerPower(base, exponent) that returns the value of                                         base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. The function integerPower should use for or while to control the calculation. Do not use built-in functions. B) Write a C# console program that calls the function in A) to calculate and display the value of 1+2+4+8+…+210.

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • write a recursive method power( base, exponent )that , when called returns base exponent for example...

    write a recursive method power( base, exponent )that , when called returns base exponent for example , power (3,4) 3*3*3*3*. assume that exponent is an integer greater than or equal to 1. Hint: the resursion step should use the relationship base exponent = basec . base exponent. -1 and the terminating condition occurs when exponent is equal to 1, because base1 = base Incroprate this method into a program that enables the user to enter the base and exponent

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT