Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Average
  4. {
  5. // an array to hold 5 interger values
  6. private int[] data = new int[5];
  7.  
  8. // the average of the data in the array
  9. private double mean;
  10.  
  11. /**
  12. * This constructor sets up the array and calls the
  13. * selectionSort and calculateMean methods
  14. */
  15. public Average()
  16. {
  17. Scanner keyboard = new Scanner(System.in);
  18.  
  19. for (int i = 0; i < data.length; ++i)
  20. {
  21. System.out.println("Please enter an integer value:");
  22. data[i] = keyboard.nextInt();
  23. }
  24.  
  25. selectionSort();
  26. calculateMean();
  27. }
  28.  
  29. /**
  30. * This method calculates the mean of the data stored in the array
  31. */
  32. public void calculateMean()
  33. {
  34. //Put your code here for calculating the mean of all the
  35. //elements in the data array.
  36.  
  37. //I NEED THIS*****
  38. }
  39.  
  40. /**
  41. * This method returns the data in the array in descending order and the
  42. * mean
  43. *
  44. * @return value stored in b and mean
  45. */
  46. public String toString()
  47. {
  48. String b = " ";
  49. for (int i = 0; i < data.length; ++i)
  50. {
  51. b += " " + data[i];
  52. }
  53. return "The numbers in the array are:" + b + " and the mean is: " + mean;
  54. }
  55.  
  56. /**
  57. * This method sorts data in array from highest to lowest
  58. */
  59. public void selectionSort()
  60. {
  61. int maxIndex;
  62. int maxValue;
  63.  
  64. for (int startScan = 0; startScan < data.length - 1; startScan++)
  65. {
  66. maxIndex = startScan;
  67. maxValue = data[startScan];
  68.  
  69. for (int index = startScan + 1; index < data.length; index++)
  70. {
  71. if (data[index] > maxValue)
  72. {
  73. maxValue = data[index];
  74. maxIndex = index;
  75. }
  76. }
  77. data[maxIndex] = data[startScan];
  78. data[startScan] = maxValue;
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement