Write a java program which contains the following static methods. You may not us
ID: 3642527 • Letter: W
Question
Write a java program which contains the following static methods.
You may not use existing methods provided by Java but you may invoke methods in this MyStatistics class. For example, you may use findMax and findMin to derive your range value for the findRange method.
public static int findRange(int[] numbers)
To compute the range of a set of values, simply subtract the lowest value from the highest value in the array named numbers.
public static int findSum(int[] numbers)
This method finds the sum of all values stored in the array named numbers.
public static double findMean(int[] numbers)
The mean of a set of values stored in the array named numbers can be found by adding the values in the array
and dividing the total by the number of values
Explanation / Answer
public class Find {
public static int findRange(int[] numbers) {
int max = numbers[0];
int min = numbers[0];
for(int i = 1; i < numbers.length; ++i) {
if(numbers[i] > max) {
max = numbers[i];
}
if(numbers[i] < min) {
min = numbers[i];
}
}
return max - min;
}
public static int findSum(int[] numbers) {
int sum = 0;
for(int i = 0; i < numbers.length; ++i) {
sum += numbers[i];
}
return sum;
}
public static double findMean(int[] numbers) {
return findSum(numbers) / (numbers.length * 1.0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.