Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7910483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:52:51+00:00 2026-06-03T12:52:51+00:00

I am creating a program that calculates rainfall for the year etc. I had

  • 0

I am creating a program that calculates rainfall for the year etc. I had the first block of code below working with the user input, perfectly. However, I am trying to change the program now, so that the array values are specified (I’m basically trying to eliminate the user input).

Why isn’t the second block of code working? I am getting errors at the bottom for r.getTotalRainFall, r.getAverageRainFall etc.

Please note that I had to introduce the array thisYear (this is required).

CODE BLOCK #1:

import java.util.*;

public class Rainfall {

Scanner in = new Scanner(System.in);
int month = 12;
double total = 0;
double average;
double months[];

public Rainfall() {
    months = new double[12];
}

public void enterMonthData() {
    for (int n = 1; n <= month; n++) {
        System.out.print("Enter the rainfall (in inches) for month #" + n + ": ");
        months[n - 1] = in.nextDouble();

        // Input Validation - Cannot accept a negative number
        while (months[n - 1] < 0) {
            System.out.print("Rainfall must be at least 0. Please enter a new value.");
            months[n - 1] = in.nextDouble();
        }
    }
}

public double getTotalRainFall() {
    total = 0;
    for (int i = 0; i < 12; i++) {
        total = total + months[i];
    }
    return total;
}

public double getAverageRainFall() {
    average = total / 12;
    return average;
}

/**
 * Returns the index of the month with the highest rainfall.
 */
public int getHighestMonth() {
    int highest = 0;
    for (int i = 0; i < 12; i++) {
        if (months[i] > months[highest]) {
            highest = i;
        }
    }
    return highest;
}

/**
 * Returns the index of the month with the lowest rainfall.
 */
public int getLowestMonth() {
    int lowest = 0;
    for (int i = 0; i < 12; i++) {
        if (months[i] < months[lowest]) {
            lowest = i;
        }
    }
    return lowest;
}

public static void main(String[]args) {
    Rainfall r = new Rainfall();
    r.enterMonthData();
    System.out.println("The total rainfall for this year is " + r.getTotalRainFall());
    System.out.println("The average rainfall for this year is " + r.getAverageRainFall());
    int lowest = r.getLowestMonth();
    int highest = r.getHighestMonth();
    System.out.println("The month with the highest amount of rain is " + (highest+1) + " with " + r.months[highest] + " inches");
    System.out.println("The month with the lowest amount of rain is  " + (lowest+1) + " with " + r.months[lowest] + " inches");
}
}

CODE BLOCK #2:

package rain;

public class Rain {

int month = 12;
double total = 0;
double average; 
double getRainAt[];

 public Rain {
    getRainAt = new double[12];
}

double getTotalRainFall() {
    total = 0;
    for (int i = 0; i < 12; i++) {
        total = total + getRainAt[i];
    }
    return total;
}

   double getAverageRainFall() {
    average = total / 12;
    return average;
}

int getHighestMonth() {
    int high = 0;
    for (int i = 0; i < 12; i++) {
        if (getRainAt[i] > getRainAt[high]) {
            high = i;
        }
    }
    return high;
}

int getLowestMonth() {
    int low = 0;
    for (int i = 0; i < 12; i++) {
        if (getRainAt[i] < getRainAt[low]) {
            low = i;
        }
    }
    return low;
}


public static void main(String[] args) {
   // Create an array of rainfall figures.
  double[] thisYear = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7,
                       3.9, 2.6, 2.9, 4.3, 2.4, 3.7 };

  int high;      // The high month
  int low;       // The low month

  // Create a RainFall object initialized with the figures
  // stored in the thisYear array.
  Rainfall r = new Rainfall(thisYear);

  // Display the statistics.
  System.out.println("The total rainfall for this year is " +
                     r.getTotalRainFall();
  System.out.println("The average rainfall for this year is " +
                     r.getAverageRainFall());
  high = r.getHighestMonth();
  System.out.println("The month with the highest amount of rain " +
                     "is " + (high+1) + " with " + r.getRainAt(high) +
                     " inches.");
  low = r.getLowestMonth();
  System.out.println("The month with the lowest amount of rain " +
                     "is " + (low+1) + " with " + r.getRainAt(low) +
                     " inches.");
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:52:54+00:00Added an answer on June 3, 2026 at 12:52 pm

    I have re-factored the 2 classes

    now Rain class only contains the main method whereas all the other logic is contained in the Rainfall class

    Rainfall class has a method – getRainAt() to get rain base don the given month
    in Rainfall class has a constructor that takes a double array as an argument so it has to be instantiated with this argument provided.

    take a look at the classes now and see if this fits your requirement.

    import java.util.*;
    
    public class Rainfall {
    
    Scanner in = new Scanner(System.in);
    int month = 12;
    double total = 0;
    double average;
    double months[];
    
    public Rainfall(double newmonths[]){
        months = newmonths;
    }
    
    public void enterMonthData() {
        for (int n = 1; n <= month; n++) {
            System.out.print("Enter the rainfall (in inches) for month #" + n
                    + ": ");
            months[n - 1] = in.nextDouble();
    
            // Input Validation - Cannot accept a negative number
            while (months[n - 1] < 0) {
                System.out
                        .print("Rainfall must be at least 0. Please enter a new value.");
                months[n - 1] = in.nextDouble();
            }
        }
    }
    
    public double getTotalRainFall() {
        total = 0;
        for (int i = 0; i < 12; i++) {
            total = total + months[i];
        }
        return total;
    }
    
    public double getAverageRainFall() {
        average = total / 12;
        return average;
    }
    
    /**
     * get rain given the month number
     */
    public double getRainAt(int month){
        double rainValue = 0;
        for (int i = 0; i < months.length; i++) {
            if(month == i){
                rainValue = months[i];
                break;
            }
        }
        return rainValue;
    }
    
    /**
     * Returns the index of the month with the highest rainfall.
     */
    public int getHighestMonth() {
        int highest = 0;
        for (int i = 0; i < 12; i++) {
            if (months[i] > months[highest]) {
                highest = i;
            }
        }
        return highest;
    }
    
    /**
     * Returns the index of the month with the lowest rainfall.
     */
    public int getLowestMonth() {
        int lowest = 0;
        for (int i = 0; i < 12; i++) {
            if (months[i] < months[lowest]) {
                lowest = i;
            }
        }
        return lowest;
    }
    }
    

    Rain class only has the main method now

    public class Rain {
    
    public static void main(String[] args) {
        // Create an array of rainfall figures.
        double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3,
                2.4, 3.7 };
    
        int high; // The high month
        int low; // The low month
    
        // Create a RainFall object initialized with the figures
        // stored in the thisYear array.
        Rainfall r = new Rainfall(thisYear);
    
        // Display the statistics.
        System.out.println("The total rainfall for this year is "
                + r.getTotalRainFall());
        System.out.println("The average rainfall for this year is "
                + r.getAverageRainFall());
        high = r.getHighestMonth();
        System.out.println("The month with the highest amount of rain " + "is "
                + (high + 1) + " with " + r.getRainAt(high) + " inches.");
        low = r.getLowestMonth();
        System.out.println("The month with the lowest amount of rain " + "is "
                + (low + 1) + " with " + r.getRainAt(low) + " inches.");
      }
     }
    

    hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that calculates input a user gives, simple numbers. 2, 4,
I'm creating a program that parses a log file for a user's name and
Im creating a program that is supposed have the user enter a student name
I am creating a program that allows the user to select a drive letter
I am creating a program that will run an experiment on a user. It
Im creating a program that prints out the Ip adress of the User. So
I am creating a program that needs to store the user's data in encrypted
I'm creating a program that will do a user-predefined number of tasks, say, 100.
Bassicly im creating a program that allows the user to enter values and if
I'm creating a program that authenticates from a before it runs. I also want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.