Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help... Can somebody fix this for me?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help... Can somebody fix this for me?

    Can somebody show me how this one is done. I don not know how to do the rest. I have attached what I have so far. Would highly appreciate it. I would like it to print out an amount.

    Write a rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should should have methods that return the following:
    1. the total rainfall for the year
    2. the average monthly rainfall
    3. the month with the most rain
    4. the month with the least rain

    Demonstrate the class in a complete program

    Input Validation: Do not accept negative numbers for monthly rainfall figures.


    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;
    }
    int most_rain_month = 11;
    for(int i = 0; i < 11; i++) {
    if ( monthlyAmount[i] > monthlyAmount[most_rain_month] )
    most_rain_month = i;

    }

    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;
    }
    }
    return maxAmt;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help... Can somebody fix this for me?

    Please edit your code and wrap it in code tags. See: BB Code List - Java Programming Forums

    Does the program compile without errors? If not, copy and paste them here.
    What does the program currently output? Please copy and paste its output here.

    I don not know how to do the rest.
    Please explain what parts are missing and why you are having problems with them.