Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. private double[] rain;
  2.  
  3. public cadimaggProject5(double r[])
  4. {
  5.  
  6. // Create a new array.
  7. rain = new double[r.length];
  8.  
  9. for (int i = 0; i < r.length; i++) {
  10. rain[i] = r[i];
  11. }
  12. }
  13.  
  14. public double getTotalRainFall() {
  15. return Arrays.stream(rain).sum();
  16. }
  17.  
  18. public double getAverageRainFall() {
  19. return Arrays.stream(rain).average().getAsDouble();
  20. }
  21.  
  22. public int getHighestMonth() {
  23.  
  24. int highest = 0;
  25.  
  26. // Find the element with the highest value.
  27. for (int i = 1; i < rain.length; i++) {
  28. if (rain[i] > rain[highest])
  29. highest = i;
  30. }
  31.  
  32. // Return the element number.
  33. return highest;
  34. }
  35.  
  36. public int getLowestMonth()
  37. {
  38. int lowest = 0;
  39.  
  40. // Find the element with the lowest value.
  41. for (int i = 1; i < rain.length; i++) {
  42. if (rain[i] < rain[lowest])
  43. lowest = i;
  44. }
  45. // Return the element number.
  46. return lowest;
  47. }
  48.  
  49. public double getRainAt(int e)
  50. {
  51. return rain[e];
  52. }
  53.  
  54. public static void main(String[] args)
  55. {
  56.  
  57. // Array representing rainfall figures
  58. // position correlates to the month
  59. double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3,
  60. 2.4, 3.7 };
  61.  
  62. RainfallClass r = new RainfallClass(thisYear);
  63.  
  64. // Display the statistics.
  65. System.out.println("The total rainfall for this year is "
  66. - r.getTotalRainFall());
  67. System.out.println("The average rainfall for this year is "
  68. - r.getAverageRainFall());
  69.  
  70. int high = r.getHighestMonth();
  71. System.out.println("The month with the highest amount of rain " + "is "
  72. - (high + 1) + " with " + r.getRainAt(high) + " inches.");
  73. int low = r.getLowestMonth();
  74. System.out.println("The month with the lowest amount of rain " + "is "
  75. - (low + 1) + " with " + r.getRainAt(low) + " inches.");
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement