Advertisement
MA1779

RainFall.java

Aug 10th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public class Rainfall
  2. double monthlyAmount[] = new double[12];
  3.  
  4. public Rainfall() { }
  5.  
  6. public void setMonthlyAmount(int monthNo, double amt) {
  7. monthlyAmount[monthNo] = amt;
  8. }
  9.  
  10. public double getTotalRainfall() {
  11. double total = 0.0;
  12. for (int i = 0; i < monthlyAmount.length; ++i)
  13. total += monthlyAmount[i];
  14. return total;
  15. }
  16.  
  17. public double getAverageRainfall() {
  18. return getTotalRainfall() / (double) monthlyAmount.length;
  19. }
  20.  
  21. public double getLeastRainMonth() {
  22. int minIdx = 0;
  23. double minAmt = monthlyAmount[0];
  24. for (int i = 1; i < monthlyAmount.length; ++i) {
  25. if (monthlyAmount[i] < minAmt) {
  26. minAmt = monthlyAmount[i];
  27. minIdx = i;
  28. }
  29. }
  30. // Can return "minIdx" if they want the month number;
  31. // I'm assuming they want the amount
  32. return minAmt;
  33. }
  34.  
  35. public double getMostRainMonth() {
  36. int maxIdx = 0;
  37. double maxAmt = monthlyAmount[0];
  38. for (int i = 1; i < monthlyAmount.length; ++i) {
  39. if (monthlyAmount[i] > maxAmt) {
  40. maxAmt = monthlyAmount[i];
  41. maxIdx = i;
  42. }
  43. }
  44. // Can return "maxIdx" if they want the month number;
  45. // I'm assuming they want the amount
  46. return maxAmt;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement