5 Replies - 10590 Views - Last Post: 06 December 2009 - 08:35 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Rainfall array program

Post icon  Posted 14 August 2009 - 10:10 PM

Hello! I am having a small brainfart. I have to write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:
- The total rainfall for the year
- The average monthly rainfall
- The month with the most rain
- The month with the least rain

Input validation: Do not accept negative numbers for monthly rainfall figures. My mind right now is blank as far as how to make the program calculate and show the month with the most rain. Any help is greatly appreciated! Here's what I have so far:

import java.text.*;
import javax.swing.*;

class Rainfall
{

	public static void main (String[] args) 
		{

		DecimalFormat df = new DecimalFormat("0.00");

		String[] monthName = { "January", "February", "March",
							   "April", "May", "June", "July",
							   "August", "September", "October",
							   "November", "December"  };

		double[]  rainfall = new double[12];

		double[]  quarterAverage = new double[4];

		double	annualAverage,
				  sum,
				  difference;
				  
		sum = 0.0;

		for (int i = 0; i < rainfall.length; i++) 
			{

			rainfall[i] = Double.parseDouble(
								JOptionPane.showInputDialog(null,
										"Rainfall for" + monthName[i]));
			sum += rainfall[i];
			}
 
		annualAverage = sum / 12.0;

		System.out.println( "Total rainfall: " + sum);

		System.out.println( "Average Monthly Rainfall: " + df.format(annualAverage ));
							 
		}
   }




Is This A Good Question/Topic? 0
  • +

Replies To: Rainfall array program

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Rainfall array program

Posted 14 August 2009 - 10:16 PM

To start you should sort it out into methods. For input validation do something like
do {
	// get input here
} while ( input < 0)



For getting the min max you would iterate over the entire loop keeping the min/max variable in a value.
int max = 0;
for(int current : array) {
	if(current > max) max = current;
}


Was This Post Helpful? 0
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Rainfall array program

Posted 15 August 2009 - 08:40 AM

Now I'm trying to get the month with the greatest and lowest rain and I get an error message. Here's what I have so far:

import java.text.*;
import javax.swing.*;

class Rainfall 
{

	public static void main (String[] args) {

		DecimalFormat df = new DecimalFormat("0.00");

		String[] monthName = { "January", "February", "March", "April", "May", "June", "July",
							   "August", "September", "October", "November", "December" };

		double[]  rainfall = new double[12];

		double[]  quarterAverage = new double[4];

		double	annualAverage,
				  sum,
				  difference;
		int i;

		sum = 0.0;
	 
		 double greatest = rainfall[0];
	  
		 for (i = 1; i < rainfall.length; i++)
		 {
		 
			if(rainfall[i] > greatest)
			   greatest = rainfall[i];
		 }	
   	
   // Least method
	
		 double lowest = rainfall[0];
	  
		 for(i = 1; i < rainfall.length; i++)
		 {
			if(rainfall[i] < lowest)
			   lowest = rainfall[i];
		 }
	  
		for (i = 0; i < rainfall.length; i++) 
		{

			rainfall[i] = Double.parseDouble(JOptionPane.showInputDialog(null,"Rainfall for " + monthName[i]));
			sum += rainfall[i];
		
		while (rainfall[i] < 0.0)
		 
			rainfall[i] = Double.parseDouble(JOptionPane.showInputDialog(null,"Rainfall must be at least 0.0 please enter a new value!"));
			
		 }

		annualAverage = sum / 12.0;
		
		System.out.println( "Total rainfall: " + sum);

		System.out.println( "Annual Average Rainfall: " + df.format(annualAverage ));
		
		System.out.println("\nThe greatest amount of rainfall was: \t" +monthName[i] +"with " + (lowest) +"inches");
		 
		System.out.println("\nThe lowest amount of rainfall was: \t" +monthName[i] +"with " + (greatest) +"inches");

		}
   }


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rainfall array program

Posted 15 August 2009 - 08:28 PM

You have it all upside down again :)
What is the purpose of writing a class Rainfall if everyting is done in a main() static method

The static void main() method is to test your class NOT to do the job

It should look like this:

import java.text.*;
import javax.swing.*;

class RainFall 
{
	static final DecimalFormat df = new DecimalFormat("0.00");

	static final String[] monthName = { "January", "February", "March", "April", "May", "June", "July",
						   "August", "September", "October", "November", "December" };

	double[]  rainfall = new double[12];


	RainFall() {	  
	}
	
	void addValue(int month, double quantity) {
		rainfall[month] = quantity;
	}
	
	double greatest() {
		double great = 0.0;
		for(int i = 0; i < rainfall.length; i++) {
			if(rainfall[i] > great)
				great = rainfall[i];
		}
		return great;
	}
	
	void displayPerMonth() {
		for(int i = 0; i < rainfall.length; i++)
			System.out.println(monthName[i] + ": " + rainfall[i]);
	}
	public static void main (String[] args) {

		RainFall rf = new RainFall();
		rf.addValue(0, 10.0);
		rf.addValue(1, 12.0);
		rf.addValue(2, 14.0);
		rf.addValue(3, 16.0);
		rf.addValue(4, 18.0);
		rf.addValue(5, 20.0);
		rf.addValue(6, 18.0);
		rf.addValue(7, 16.0);
		rf.addValue(8, 14.0);
		rf.addValue(9, 12.0);
		rf.addValue(10, 8.0);
		rf.addValue(11, 8.0); 
		
		rf.displayPerMonth();
		System.out.println("Greatest one: " + rf.greatest());

   }
}


Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Rainfall array program

Posted 15 August 2009 - 08:31 PM

Quote

I get an error message.

Could you possibly post this error?
Was This Post Helpful? 0
  • +
  • -

#6 dradford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 27-August 09

Re: Rainfall array program

Posted 06 December 2009 - 08:35 PM

I think the reason you might be getting the out of bounds exception error is because you try to determine the lowest and highest rainfall before you ask the user for input. I think you forgot to put methods into your program. The outofboundsexception is caused when arrays are off usually because people forget that an array starts at an index of zero and not one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1