1. public static double[] getUserInput() (a) Prompts the user for how many numbe
ID: 3760067 • Letter: 1
Question
1. public static double[] getUserInput()
(a) Prompts the user for how many numbers they will enter, we will cal this size.
(b) Check to make sure that the value of size is 1 or greater.
(c) With the value of size, create a single dimensional array of doubles with the number of elements being the value of size. For example, if size has the value of 4, then your array has 4 elements.
(d) With the created array of doubles, write a loop that reads values in form the keyboard and stores them into the array. If the value of size is 4, you will read in 4 values from the keyboard.(this code is in the slides)
(e) Finally, return the array using the keyword return.
2. public static double arithmeticMean(double[] nums)
This method returns the arithmetic mean of the numbers provided by the
input parameter, nums.
3. public static double geometricMean(double[] nums)
This method returns the geometric mean of the numbers provided by the
input parameter, nums.
4. public static double[] minAndmax(double[] nums)
This method returns the smallest number and the largest number in the array
provided by the input parameter. You dont need to return the location of
the min and max.
5. public static double[] scaleUp(double[] nums, double factor)
This method multiplies every number provided by the rst input parameter
with a scaling factor (the second parameter). Make sure that the factor is a
value greater than or equal to 1.0. If the factor is less than one, just return
from the method.
6.public static double[] scaleDown(double[] nums, double factor)
This method divides every number provided by the rst input parameter with
a scaling factor (the second parameter). Make sure that the factor is a value
greater than or equal to 1.0. If the factor is less than one, just return from
the method.
7. Main method:
In this method,
1.obtain the numbers from the user using the method getUserInput() described above.
2.provide the user with 6 possible choices, each corresponding to one of the methods above and a exit option. Exclude getUserInput as an option.
Option 1 - arithmeticMean
Option 2 - geometricMean
Option 3 - minAndmax
Option 4 - scaleUp
Option 5 - scaleDown
Option 6 - Exit
HINT: A switch statement would be the easiest structure to use for this
menu.
3.prompt the user to pick one. Check the user input for its validity, it has to be a number between 1 and 6.
a.If option 4 (Scale up) or 5 (Scale down) is selected by the userthen prompt the user for a value for factor.
4. perform the requested operation, and output the corresponding statistic(s). If Exit option was selected then terminate the program.
5. loop back to step 2.
Note that this program should only use 1 array for the entire program. DO NOT CREATE a new array for each method.
Explanation / Answer
Solution for above problem is below..Please provoide your feedback so that I can improve if you need any changes ..
import java.util.Scanner;
public class UserChoice
{
public static int size ;
public static double[] getUserInput()
{
int size = 1, i = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter How Many Numbers you need to Entered");
size = in.nextInt();
if(size < 1)
{
System.out.println("Re-Enter How Many Numbers you need to Entered");
size = in.nextInt();
}
double array[] = new double[size];
System.out.println("Enter Double Array Elements");
for( i =0 ; i < size; i++)
{
array[i] = in.nextDouble();
}
return array;
}
public static double arithmeticMean(double[] nums)
{
double sum = 0, average = 0;
int i = 0, count = 0;
for(i = 0; i < nums.length; i++)
{
sum = sum + nums[i];
count++;
}
average = (sum / count);
return average;
}
public static double geometricMean(double[] nums)
{
double product = 1.0, geoMean = 0;
int i = 0;
for(i = 0 ; i < nums.length; i++)
{
product = product * nums[i];
}
geoMean = Math.pow(product, 1.0 / nums.length);
return geoMean;
}
public static double[] minAndmax(double[] nums)
{
double index[] = new double[2];
int i = 0;
double largest = nums[0];
double smallest = nums[0];
for(i=0; i< nums.length; i++)
{
if(nums[i] > largest)
index[1] = nums[i];
else if (nums[i] < smallest)
index[0] = nums[i];
}
return index;
}
public static double[] scaleUp(double[] nums, double factor)
{
int i =0;
if(factor < 1)
{
System.out.println("Re-Enter Factor greater than 1");
Scanner in = new Scanner(System.in);
factor = in.nextDouble();
}
for(i = 0; i< nums.length; i++)
{
nums[i] = nums[i] * factor;
}
return nums;
}
public static double[] scaleDown(double[] nums, double factor)
{
int i =0;
if(factor < 1)
{
System.out.println("Re-Enter Factor greater than 1");
Scanner in = new Scanner(System.in);
factor = in.nextDouble();
}
for(i = 0; i< nums.length; i++)
{
nums[i] = nums[i] / factor;
}
return nums;
}
public static void main(String[] args)
{
int choice;
double temp[] = new double[10];
double temp1=0.0;
double temp2[] = new double[10];
Scanner in = new Scanner(System.in);
System.out.println("Enter Your Choice");
System.out.println("1. arithmeticMean 2. geometricMean 3. minAndmax 4. scaleUp 5. scaleDown 6. Exit");
choice = in.nextInt();
switch(choice)
{
case 1:
temp =new UserChoice().getUserInput();
temp1 = new UserChoice().arithmeticMean(temp);
System.out.println("Arithmetic Mean is: "+temp1);
break;
case 2:
temp =new UserChoice().getUserInput();
temp1 = new UserChoice().geometricMean(temp);
System.out.println("Geometric Mean is: "+temp1);
break;
case 3:
temp =new UserChoice().getUserInput();
temp2 = new UserChoice().minAndmax(temp);
System.out.println("Min Index is: "+temp2[0]+" Max Index is: "+temp2[1]);
break;
case 4:
temp =new UserChoice().getUserInput();
temp2 = new UserChoice().scaleUp(temp, 1.2);
for(int i = 0; i< temp2.length; i++){
System.out.println("Scale Up is: "+temp2[i]);}
break;
case 5:
temp =new UserChoice().getUserInput();
temp2 = new UserChoice().scaleDown(temp, 2);
for(int i = 0; i< temp2.length; i++){
System.out.println("Scale Up is: "+temp2[i]);}
break;
case 6:
System.exit(0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.