You will create a function that validates input. It should accept only non-negat
ID: 3767071 • Letter: Y
Question
You will create a function that validates input. It should accept only non-negative integers. If the input is incorrect the function should throw an exception. The exception will not be caught in the function.
You will create a function that reads in 2 integer values. One is a number that will be raised to the power of the second. So if 5 and 6 are entered the result would be 5 to the 6th power. If an invalid input is received take appropriate action in response to the exception.
You will create a second function that reads in a series of numbers and calculates the average and standard deviation of them. Read in numbers until a -1 is entered. Validate all the numbers and generate an appropriate message in response to eh exception.
Your program should call the power function and the averaging function, prompt the user as appropriate, validate the inputs, calculate and display the results.
Explanation / Answer
import java.util.Scanner;
public class test{
public static int nonNegative() throws Exception{
Scanner in = new Scanner(System.in);
System.out.print("Enter a non negative number: ");
int num = in.nextInt();
if(num < 0) throw new Exception();
return num;
}
public static int power(){
int base = 0, exponent = 0;
try{
base = nonNegative();
exponent = nonNegative();
} catch(Exception e){
System.out.println("Invalid input");
}
return (int) Math.pow(base, exponent);
}
public static double avg(){
// do I need to use the other 2 functions for taking input in this function?
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.