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