I\'m having a lot trouble understanding this assignment and I really need help w
ID: 3769908 • Letter: I
Question
I'm having a lot trouble understanding this assignment and I really need help with it. Can someone go over it step-by-step?
For this assignment you will be writing a Java program (SimpleStatistics.java) that provides basic statistics about the input data. The method stubs are given below. This assignment will give you experience with methods, arrays, and loops in Java. As always, include comments in your program wherever appropriate. Please name your project SimpleStatistics
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 1 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. (7 points) 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: Use a switch statment
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 user then 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
import java.util.*;
public class SimpleStatistics {
public static double[] getUserInput() {
Scanner sc = new Scanner(System.in);
List<Double> inputList = new ArrayList<Double>();
System.out.println("Please enter how many numbers you will be inputing");
int numberOfInputs = sc.nextInt();
for (int i = 0; i < numberOfInputs; i++) {
System.out.println("Please enter a number");
double userInput = sc.nextDouble();
inputList.add(userInput);
}
sc.close();
double[] arr = new double[inputList.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = inputList.get(i);
}
return arr;
}
public static double arithmeticMean(double[] nums) {
double mean = 0;
double sum = 0;
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
mean = sum / nums.length;
return mean;
}
public static double geometricMean(double[] nums) {
double gm = 1.0;
for (int i = 0; i < nums.length; i++) {
gm *= nums[i];
}
gm = Math.pow(gm, 1.0 / (double) nums.length);
return gm;
}
public static double[] minAndmax(double[] nums) {
double min = nums[0];
double max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
} else if (nums[i] > max) {
max = nums[i];
} else {
}
}
double[] minAndmax = { min, max };
return minAndmax;
}
public static double[] scaleUp(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] *= factor;
}
return nums;
}
public static double[] scaleDown(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] /= factor;
}
return nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] input = { 1, 3.9, 5.6, 200, -5, -5.5 };
boolean exit = false;
while (!exit) {
System.out.println();
System.out.println("1.Arithmetic mean");
System.out.println("2 Geometric mean");
System.out.println("3 minAndmax");
System.out.println("4.Scale Up");
System.out.println("5.Scale Down");
System.out.println("6.Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.println();
switch (choice) {
case 1: {
// Arithmetic mean
System.out.println("Arithmetic mean::");
System.out.println(arithmeticMean(input));
break;
}
case 2: {
// Geometric mean
System.out.println("Geometric mean::");
System.out.println(arithmeticMean(input));
break;
}
case 3: {
// Min and max
System.out.println("Min and Max::");
for (double i : minAndmax(input)) {
System.out.print(i + ", ");
}
break;
}
case 4: {
// Scale Up
System.out.println("Scale Up");
System.out
.print("Enter factor by which you want to scale up ::");
int factor = sc.nextInt();
for (double i : scaleUp(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 5: {
// Scale Down
System.out.println("Scale Down");
System.out
.print("Enter factor by which you want to scale down :: ");
int factor = sc.nextInt();
for (double i : scaleDown(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 6: {
exit = true;
break;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.