Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rainfall
- double monthlyAmount[] = new double[12];
- public Rainfall() { }
- public void setMonthlyAmount(int monthNo, double amt) {
- monthlyAmount[monthNo] = amt;
- }
- public double getTotalRainfall() {
- double total = 0.0;
- for (int i = 0; i < monthlyAmount.length; ++i)
- total += monthlyAmount[i];
- return total;
- }
- public double getAverageRainfall() {
- return getTotalRainfall() / (double) monthlyAmount.length;
- }
- public double getLeastRainMonth() {
- int minIdx = 0;
- double minAmt = monthlyAmount[0];
- for (int i = 1; i < monthlyAmount.length; ++i) {
- if (monthlyAmount[i] < minAmt) {
- minAmt = monthlyAmount[i];
- minIdx = i;
- }
- }
- // Can return "minIdx" if they want the month number;
- // I'm assuming they want the amount
- return minAmt;
- }
- public double getMostRainMonth() {
- int maxIdx = 0;
- double maxAmt = monthlyAmount[0];
- for (int i = 1; i < monthlyAmount.length; ++i) {
- if (monthlyAmount[i] > maxAmt) {
- maxAmt = monthlyAmount[i];
- maxIdx = i;
- }
- }
- // Can return "maxIdx" if they want the month number;
- // I'm assuming they want the amount
- return maxAmt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement