Reference no: EM13165147
You will be writing a Java program that provides simple statistics about the input data. The method stubs are given below. This assignment will give you experience with methods, arrays and exceptionhandlin in Java. As always, include comments in your program whereve appropriate.
1. double[] getUserInput()
This method obtains X numbers from the user, where X is also specified by
the user. Use exception-handling to catch input mismatch errors during the
user input. Store the user provided values into an array of doubles, and return
this array to the main method.
2. double arithmeticMean(double[] nums)
This method returns the arithmetic mean of the numbers provided by the input
parameter. Use exception-handling to catch for arithmetic errors.
3. double geometricMean(double[] nums)
This method returns the geometric mean of the numbers provided by the input
parameter. Use exception-handling to catch for arithmetic errors.
4. double[] minAndmax(double[] nums)
This method returns the smallest number and the largest number in the input
parameter. You don't need to return the location of the min and max.
5. double[] scaleUp(double[] nums, int factor)
This method multiplies every number provided by the first input parameter
with a scaling factor (the second parameter). Use exception-handling to catch
for illegal argument error. Specifically, for valid scaling-up operation the
factor has to be at least 1.
6. double[] scaleDown(double[] nums, int factor)
This method divides every number provided by the first input parameter with
a scaling factor (the second parameter). Use exception-handling to catch for
divide-by-zero error.
7. main method:
In this method, first obtain the numbers from the user, and then provide the
user with 5 possible choices (each corresponding to one of the methods above,
except for getUserInput), and prompt the user to pick one. Check the user
input for its validity, it has to be a number between 1 and 5. Then perform the
requested operation, and output the corresponding statistic(s).
i have this so far, and i only need help with 4,5,6,7.
package simplestatistics;
import java.util.Scanner;
public class SimpleStatistics {
public static double[] getUserInput()
{
double [] array;
Scanner input = new Scanner(System.in);
System.out.println("Enter the how many numbers:");
int num = input.nextInt();
array = new double[num];
if (num < 0){
throw new ArithmeticException("value cannot be less than one");
}
try {
for(int i=0; i < num ;i++){
System.out.println("Enter number:");
double userfinalinput = input.nextDouble();
array [i] = userfinalinput;
}
}
catch(ArithmeticException ex){
System.out.println("Exception:the value"+"cannot be less than one");
}
System.out.println("Execution continues...");
return array;
}
public static double arithmeticMean(double []array)
{
double mean = 0;
for(int i=0; i < array.length;i++){
mean += array[i];
}
mean = mean / array.length;
if (mean < 0){
throw new ArithmeticException("value cannot be negative number");
}
try {
for(int i=0; i < array.length;i++){
mean += array[i];
}
mean= mean/ array.length;
System.out.println("the arithmetic mean value is"+mean);
}
catch(ArithmeticException ex){
System.out.println("Exception:the mean value"+"cannot be negative number");
}
System.out.println("Execution continues...");
return mean;
}
public static double geometricMean (double[]array)
{
double geo_mean = 1;
for(int i=0; i< array.length;i++){
geo_mean *= array[i];
}
geo_mean = Math.pow(geo_mean,1/array.length);
//do try and catch for this
}
public static double minAndmax (double []array)
{
}
public static void main(String[] args) {
double [] array = getUserInput();
}
}
}