Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class lab04.NumberPower similar to Multiplier. It should read two intege

ID: 3629442 • Letter: W

Question

Write a class lab04.NumberPower similar to Multiplier. It should read two integer values and compute a result, incorporating the following changes:

1.) Use a private helper method getNumber() to read the values. Your getNumber() method should prompt the user to enter one integer and it should either return the number entered or the randomly generated value. (Note: in order to call your getNumber() method from main, you will have to declare it to be static.)
A typical console interaction might look something like this:
Enter first number: 5
Enter second number: boogers
5 ^ 3 = 125.0

2.) If the user enters a non-number for either value, use a random value between 1 and 6 (generated using an instance of the Random class).


Explanation / Answer

import java.util.*;

class NumberPower()
{
  public static void main(String[]args)
{
  int num1 =getNumber();
  int num2 =getNumber();

  System.out.println(num1 + " ^ " + num2 + " = " +Math.pow(num1, num2));
}
  private static int getNumber()
{

  Scanner in =new Scanner(System.in);


  System.out.println("Enter anumber or a non-number:");


  if(in.hasNextInt())
{
return in.nextInt();
}
  else
{

return (int)(Math.random()*6+1);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote