Need to find the Median in JAVA import javautilArrays class
Need to find the Median in JAVA
import java.util.Arrays;
class Main
{
public static void main (String args [ ])
{
int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6};
// Find minimum (lowest) value in array using loop
System.out.println(\"Minimum Value = \" + getMinValue(numbers));
// Find maximum (largest) value in array using loop
System.out.println(\"Maximum Value = \" + getMaxValue(numbers));
// Find average value in array using loop
System.out.println(\"Average Value = \" + getAvgValue(numbers));
// Add any challenge method calls here ....
Arrays.sort(numbers);
// Print the sorted array
System.out.println( \"The sorted array: \");
for (int i = 0; i < numbers.length; i++)
{
System.out.println (numbers[i]);
}
}
// Find maximum (largest) value in array using loop
public static int getMaxValue ( int [ ] numbers ) {
int maxValue = numbers [0];
// Access each array element starting with the second element
for (int i = 1; i < numbers.length; i++){
if (numbers [ i ] > maxValue) { // is the current element greater than the current max value?
maxValue = numbers [ i ]; // the current element is now the max value
} // end if
} // end for
return maxValue; // return the largest array element value
} // getMaxValue
// Find minimum (lowest) value in array using loop
public static int getMinValue( int [ ] numbers) {
int minValue = numbers [ 0 ];
// Access each array element starting with the second element
for (int i = 1; i < numbers.length; i++){
if (numbers [ i ] < minValue) { // is the current element less than the current min value?
minValue = numbers [ i ]; // the current element is now the min value
} // end if
} // end for
return minValue;
} // getMinValue
// Find the average of an array of integers
public static double getAvgValue ( int [ ] numbers) {
double average = 0;
double sum = 0;
//ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
for (int i = 0; i < numbers.length; i++) {
sum = sum + numbers [ i ];
} // end for
// ADD CODE TO COMPUTE THE AVERAGE
average = sum / numbers.length;
return average;
} // getAvgValue
// Add any Challenge methods here ...
} // Main Class
Solution
import java.util.Arrays;
class Main {
public static void main(String args[]) {
int numbers[] = { 1, 5, -9, 12, -3, 89, 18, 23, 4, -6 };
// Find minimum (lowest) value in array using loop
System.out.println(\"Minimum Value = \" + getMinValue(numbers));
// Find maximum (largest) value in array using loop
System.out.println(\"Maximum Value = \" + getMaxValue(numbers));
// Find average value in array using loop
System.out.println(\"Average Value = \" + getAvgValue(numbers));
// Add any challenge method calls here ....
Arrays.sort(numbers);
// Print the sorted array
System.out.println(\"The sorted array: \");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + \" \");
}
System.out.println(\"\ Median of the Array: \" + getMedian(numbers));
}
/**
* method to find median of an array
*
* @param numbers
* @return
*/
public static double getMedian(int[] numbers) {
int middle = numbers.length / 2;
if (numbers.length % 2 == 0) {
int left = numbers[middle - 1];
int right = numbers[middle];
return (left + right) / 2.0;
} else {
return numbers[middle];
}
}
// Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers) {
int maxValue = numbers[0];
// Access each array element starting with the second element
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue) { // is the current element greater than
// the current max value?
maxValue = numbers[i]; // the current element is now the max
// value
} // end if
} // end for
return maxValue; // return the largest array element value
} // getMaxValue
// Find minimum (lowest) value in array using loop
public static int getMinValue(int[] numbers) {
int minValue = numbers[0];
// Access each array element starting with the second element
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) { // is the current element less than the
// current min value?
minValue = numbers[i]; // the current element is now the min
// value
} // end if
} // end for
return minValue;
} // getMinValue
// Find the average of an array of integers
public static double getAvgValue(int[] numbers) {
double average = 0;
double sum = 0;
// ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY
// ELEMENTS
for (int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
} // end for
// ADD CODE TO COMPUTE THE AVERAGE
average = sum / numbers.length;
return average;
} // getAvgValue
// Add any Challenge methods here ...
} // Main Class
OUTPUT:
Minimum Value = -9
Maximum Value = 89
Average Value = 13.4
The sorted array:
-9 -6 -3 1 4 5 12 18 23 89
Median of the Array: 4.5
![Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6 Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6](/WebImages/20/need-to-find-the-median-in-java-import-javautilarrays-class-1043195-1761542245-0.webp)
![Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6 Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6](/WebImages/20/need-to-find-the-median-in-java-import-javautilarrays-class-1043195-1761542245-1.webp)
![Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6 Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6](/WebImages/20/need-to-find-the-median-in-java-import-javautilarrays-class-1043195-1761542245-2.webp)
![Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6 Need to find the Median in JAVA import java.util.Arrays; class Main { public static void main (String args [ ]) { int numbers [ ] = {1,5,-9,12,-3,89, 18,23,4,-6](/WebImages/20/need-to-find-the-median-in-java-import-javautilarrays-class-1043195-1761542245-3.webp)