fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main {
  6.  
  7. public static void main (String args[]) {
  8.  
  9. int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6};
  10.  
  11. try {
  12. // Find minimum (lowest) value in array using loop
  13. System.out.println("Minimum Value = " + getMinValue(numbers));
  14.  
  15. // Find maximum (largest) value in array using loop
  16. System.out.println("Maximum Value = " + getMaxValue(numbers));
  17.  
  18. // ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE
  19. // Don't forget to call the avg method below ... call it just like
  20. // the two methods above ...
  21. // Add any challenge method calls here ....
  22. System.out.println("Average Value = " + getAvgValue(numbers));
  23.  
  24. // CHALLENGE 1: CALL THE MEDIAN METHOD
  25. System.out.println("Median Value = " + getMedianValue(numbers));
  26.  
  27. // ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
  28. // Use the built-in array sort method on the array
  29. // ... then add a loop here (no need to call a method) to print
  30. // the now sorted array. See our lecture notes for examples.
  31.  
  32. Arrays.sort(numbers);
  33. System.out.print("Sorted Array = ");
  34. for (int i = 0; i < numbers.length; i++) {
  35. System.out.print(numbers[i] + " ");
  36. }
  37. System.out.println();
  38.  
  39. } catch (ArithmeticException e) {
  40. System.out.println("Arithmetic error: " + e.getMessage());
  41. System.out.println("Array index error: " + e.getMessage());
  42. }
  43.  
  44. } // main
  45.  
  46.  
  47. // Find maximum (largest) value in array using loop
  48. public static int getMaxValue(int[] numbers) {
  49.  
  50. int maxValue = numbers[0]; // set the first array element (index of 0) as the max
  51.  
  52. // ... remember that arrays start with an index of 0
  53. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  54. // AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
  55.  
  56. // The key is to use a loop with the length property of the array
  57. // to access and check every element after the first element. The if statement
  58. // each time will check the current max value against the current
  59. // array value. At the end of the loop, maxValue will contain
  60. // the largest number in the array.
  61.  
  62. // Access each array element starting with the second element (i = 1, not 0)
  63. for (int i = 1; i < numbers.length; i++) {
  64. if (numbers[i] > maxValue) { // current element > current max value ?
  65. maxValue = numbers[i]; // the current element is now the max value
  66. } // end if
  67. } // end for
  68.  
  69. return maxValue; // return the largest array element value
  70. } // getMaxValue
  71.  
  72.  
  73. // Find minimum (lowest) value in array using loop
  74. public static int getMinValue(int[] numbers) {
  75.  
  76. int minValue = numbers[0];
  77.  
  78. // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
  79. // AGAINST THE CURRENT minValue (USE AN IF STATEMENT)
  80.  
  81. // Hint: This is just like the for loop in the max function above,
  82. // just revise to check for min value instead of max value
  83.  
  84. for (int i = 1; i < numbers.length; i++) {
  85. if (numbers[i] < minValue) {
  86. minValue = numbers[i];
  87. }
  88. }
  89.  
  90. return minValue;
  91. } // getMinValue
  92.  
  93.  
  94. // Find the average of an array of integers
  95. public static double getAvgValue(int[] numbers) {
  96.  
  97. // ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND
  98. // AND ANOTHER ONE, average, TO BE USED TO CALCULATE THE AVERAGE OF
  99. // ALL ARRAY ELEMENT VALUES
  100.  
  101. double average = 0;
  102. double sum = 0;
  103.  
  104. // ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
  105.  
  106. // use a loop like in the max function, but instead of an if, add the current
  107. // array element value to sum. This will keep a running total as each element value
  108. // is added to sum as it is accessed in the loop: sum = sum + numbers[i] ;
  109.  
  110. for (int i = 0; i < numbers.length; i++) {
  111. sum += numbers[i];
  112. }
  113.  
  114. // ADD CODE TO COMPUTE THE AVERAGE
  115.  
  116. // The average just takes the sum variable and divides it by the total
  117. // number of array items (i.e., numbers.length)
  118.  
  119. if (numbers.length == 0) {
  120. throw new ArithmeticException("Cannot compute average of empty array.");
  121. }
  122.  
  123. average = sum / numbers.length;
  124. return average;
  125.  
  126. } // getAvgValue
  127.  
  128.  
  129. // CHALLENGE METHOD: Get the median of the array
  130. public static double getMedianValue(int[] numbers) {
  131. // Copy and sort the array without modifying the original
  132. int[] sorted = Arrays.copyOf(numbers, numbers.length);
  133. Arrays.sort(sorted);
  134.  
  135. if (sorted.length == 0) {
  136. throw new ArithmeticException("Cannot compute median of empty array.");
  137. }
  138.  
  139. if (sorted.length % 2 == 1) {
  140. return sorted[sorted.length / 2];
  141. } else {
  142. int mid1 = sorted[(sorted.length / 2) - 1];
  143. int mid2 = sorted[sorted.length / 2];
  144. return (mid1 + mid2) / 2.0;
  145. }
  146. } // getMedianValue
  147.  
  148. // Add any Challenge methods here ...
  149.  
  150. } // Main Class
  151.  
Success #stdin #stdout 0.2s 57892KB
stdin
Standard input is empty
stdout
Minimum Value = -9
Maximum Value = 89
Average Value = 13.4
Median Value = 4.5
Sorted Array = -9 -6 -3 1 4 5 12 18 23 89