my professor provided the most confusing problems with no time to do them so if
ID: 3637060 • Letter: M
Question
my professor provided the most confusing problems with no time to do them so if someone could provide the code i'd greatly appreciate it.He provided a test program to run it against to pass.
The problem is to write a test program that prompts the user to enter ten numbers and displays the mean, deviation, the smallest value index. First i have to write a method that to use for the different types of math. then write a main program actually prompting the user to input numbers.
My professor says it has to be set up like this:
----Method Summary----
static double - deviation(double... x)
Compute the standard deviation of a set of values
static int - indexOfSmallestElement(double... numbers)
Find the smallest value in a list and return its index
static double - mean(double[] numbers)
Compute the mean (average) of a set of numbers
----Method Detail----
-deviation
public static double deviation(double... x)
Compute the standard deviation of a set of values
Parameters:
x - the set of values
Returns:
the standard deviation
Throws:
IllegalArgumentException - if there are less than 2 values
-indexOfSmallestElement
public static int indexOfSmallestElement(double... numbers)
Find the smallest value in a list and return its index
Parameters:
numbers - list (array) of values
Returns:
the index of the smallest
Throws:
IllegalArgumentException - if the list is empty
-mean
public static double mean(double[] numbers)
Compute the mean (average) of a set of numbers
Parameters:
numbers - the values
Returns:
the average
Throws:
IllegalArgumentException - if there are no values
and thats all he gave us. i have NO idea how or what to do he doesnt teach anything. please provide the code in detail and as simple as possible. i know i shouldnt try and get answers but im desperate and ill be studying up to figure it all out. THANKYOU!
Explanation / Answer
Hi. This may not be the perfect answer you're looking for but it should help point you in the right direction. I tried to get as close as I could from what I understood. It would be helpful to share the test program your professor provided, it can help get the program closer to what he expects. I held off from doing anything with the exception handling (illegal argument exception) until the test program sheds some more light on what the methods are supposed to have as arguments because this: "double ... x " isn't very program-like :)
I added as many comments as I could for all the methods and perhaps this is the pointer you need to understand the problem and make the final changes. Otherwise, please provide the test program or any more info/clarification you have and we can take another look at the program and fix it, just PM me or comment on my response.
Remeber to rate if it's helpful :)
import java.util.Scanner; // do NOT forget this line!
public class MyProgram {
// the Main method - this is where execution happens
public static void main(String[] args) {
// array/list of size 10 to hold the ten numbers
double[] numbers = new double[10];
// a scanner object to read the user input
Scanner scanner = new Scanner(System.in);
// use a loop that repeats 10 times to read the 10 numbers from user
for (int i = 0; i < 10; i++) {
System.out.print("Please input a number: ");
// get the integer the user typed after then press enter/return
// and save it to the array in position "i"
numbers[i] = scanner.nextDouble();
}
// testing the functions we have
System.out.println("Numbers enterd by the user are:");
for (int i = 0; i < 10; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println(" The index of the smallest number is: " + indexOfSmallestElement(numbers));
System.out.println(" The mean is: " + mean(numbers));
System.out.println(" The standard deviation is: " + deviation(numbers));
}
public static int indexOfSmallestElement(double[] numbers) {
int smallestIndex = 0;
for (int i = 0; i < 10; i++) {
if (numbers[i] < numbers[smallestIndex]) {
smallestIndex = i;
}
}
// return/output the smallest index from the list inputed
return smallestIndex;
}
public static double mean(double[] numbers) {
double mean = 0.0;
double sum = 0.0;
for (int i = 0; i < 10; i++) {
sum = sum + numbers[i];
}
mean = sum / 10.0;
// return/output the mean for the list inputed
return mean;
}
public static double deviation(double[] numbers) {
double deviation = 0.0;
double sum = 0.0;
double[] squared = new double[10];
// step 1 - get the mean
double mean = mean(numbers);
// step 2 - compute deviaitons from the mean
for (int i = 0; i < 10; i++) {
squared[i] = numbers[i] - mean;
}
// step 3 - square each one of them
for (int i = 0; i < 10; i++) {
double square = squared[i] * squared[i];
squared[i] = square;
}
// step 4 - sum the squares
for (int i = 0; i < 10; i++) {
sum = sum + squared[i];
}
// step 5 - divide by total numbers - 1 (10 - 1 = 9)
// the square root of that is the standard deviation
double divided = sum / 9.0;
deviation = Math.sqrt(divided);
// return/output deviation for the list inputed
return deviation;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.